How to make an Interactive Bubble Chart?

This graph is needed in order to display the dependence of two numerical variables, graphically displayed various additional features.

loading data

For this example, I will use the built-in dataset on arrests in the United States, which we have worked with many times during the classes.

library(ggplot2)
library(dplyr)
library(plotly)
data("USArrests")

sample_data <- 
  tibble(State = state.name,
         Region = state.region) %>%
  bind_cols(USArrests)

visualization

To start, I will build a scatter plot for “Murder” and “Assault”. To set the size equal to variables, displaying the “Rape” variable using the point size (size in aes() + variable ranging in the scale_size_continuous().

sample_data %>% 
  ggplot(aes(x=Murder,y=Assault, size=Rape)) +
  geom_point(alpha=0.6)+
  scale_size_continuous(range = c(1, 7))+
  labs(x="Murder", y="Assault", size="Rape")

Next, I will add state names using function “label” in aes() and variable “UrbanPop”, displayed with color.

sample_data %>% 
  ggplot(aes(x=Murder,y=Assault, size=Rape, color=UrbanPop, label=State)) +
  geom_point(alpha=0.8)+
geom_text(aes(label = State), hjust = 0, vjust = 0)+
  scale_size_continuous(range = c(1, 7))+
  labs(x="Murder", y="Assault", size="Rape", color="UrbanPop")

To move on, I will save the graph into one variable.

b = sample_data %>% 
  ggplot(aes(x=Murder,y=Assault, size=Rape, color=UrbanPop, label=State)) +
  geom_point(alpha=0.8)+
geom_text(aes(label = State), hjust = 0, vjust = 0)+
  scale_size_continuous(range = c(1, 7))+
  labs(x="Murder", y="Assault", size="Rape", color="UrbanPop")

interactive graph

To create an interactive plot, I will use the plotly package. First, I will create a variable with a description of each point in the “info” variable, then I will use plotly_build and the style function to connect the image and information about the points.

info = paste("State: ", sample_data$State, "\n" , 
             "Murder: ", sample_data$Murder, "\n", 
             "Assault: ", sample_data$Assault, "\n",
             "Rape: ", sample_data$Rape, "\n",
             "UrbanPop: ", sample_data$UrbanPop,  sep="")    
plotb=plotly_build(b)   
style(plotb, text=info, hoverinfo = "text", traces = c(1))

Thank you for attention!

Sources: https://www.r-graph-gallery.com/bubble-chart/