Bubble plot is a type of scatter plot which not only uses cartesian coordinates to display the relation between two variables but also considers a third numerical variable represented by the size of the dots. Hence, we need three numerical variables, 2 for the x and y axis and 1 for the size of the dot.
Plotly has been actively developed and supported by it's community.
This recipe demonstrates how to plot a bubble plot in R using plotly package.
Dataset description: It is the basic data about the customers going to the supermarket mall. The variable that we are interested in: Annual.Income (which is in 1000s), Spending Score and age
# Data manipulation package library(dplyr) library(tidyverse) # reading a dataset customer_seg = read.csv('R_129_Mall_Customers.csv') # selecting the required variables using the select() function customer_seg_var = select(customer_seg, Age, Annual.Income..k..,Spending.Score..1.100.) # summary of the selected variables glimpse(customer_seg_var)
Observations: 200 Variables: 3 $ Age
19, 21, 20, 23, 31, 22, 35, 23, 64, 30, 67, 35… $ Annual.Income..k.. 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 19, 19… $ Spending.Score..1.100. 39, 81, 6, 77, 40, 76, 6, 94, 3, 72, 14, 99, 1…
We use the plot_ly() function to plot a bubble plot between annual income and spending score variables using Age as a dot size
Syntax: plot_ly( data = , x = , y = , type = "scatter", mode = "markers", marker = list(size = , opacity = ))
fig <- plot_ly(x = ~Annual.Income..k..[1:20], y = ~Spending.Score..1.100.[1:20], data = customer_seg, type = "scatter", mode = "markers", marker = list(size = ~Age, opacity = 0.5)) %>% layout(title = 'Bubble Plot using Plotly') embed_notebook(fig)