rggplot2bubble-chart

Bubble plot control breaks, scale, and size based on counts


I have been exploring ggplot in R and the scale_size. However, I cannot figure out how to specify the number of breaks or the range.

I would like to break the count data by 5 categories with a maximum value of 150,000. It is important that the size of the dot reflects the appropriate scale differences. For example, in the code below, the 100,000 is about twice as big as the 50,000. This post has a good example using dots and America's economy (https://blog.fastfedora.com/2011/01/2011-state-of-the-union-visualizations.html). Also, from what I read, it is a bad idea to use scale_radius().

x<- c("Extremely Low", "Extremely Low", "Extremely Low", "Very Low", "Very Low", "Very Low", "Very Low", "Very Low", "Low", "Low", "Low", "Low", "Moderate", "Moderate", "Moderate", "High", "High", "Very High", "Very High")
y <- c("Very Low", "Low", "Moderate", "Very Low", "Low", "Moderate", "High", "Very High", "Low", "Moderate", "High", "Very High", "Moderate", "High", "Very High", "High", "Very High", "High", "Very High")
z <- c(63156, 904, 39, 54364, 143902, 24666, 1234, 199, 17545, 22159, 14604, 4574, 1457, 21561, 29808, 6451, 51217, 177, 47150)
x_name <- "X_Category"
y_name <- "Y_Category"
z_name <- "Counts"

df <- data.frame(x,y,z)
names(df) <- c(x_name,y_name,z_name)
print(df)
ggplot(df, aes(x=X_Category, y=Y_Category, size = Counts)) +
  geom_point(alpha=0.5) +
  scale_size(range = c(.1, 24), name="Count") 

Solution

  • If you want the size of the points to be proportional to the count size, you can use scale_size_area. Just set a max_size and specify the breaks you want. Remember to include limits between 0 and 150,000.

    Note that you should also specify the x and y categories as factor variables to get them in the correct order:

    df$X_Category <- factor(df$X_Category, c("Extremely Low", "Very Low", "Low",
                                             "Moderate", "High", "Very High"))
    
    df$Y_Category <- factor(df$Y_Category, c("Very Low", "Low", "Moderate", "High",
                                             "Very High"))
    

    So the plot is just

    ggplot(df, aes(x = X_Category, y = Y_Category, size = Counts)) +
      geom_point(alpha = 0.8, color = "orangered") +
      scale_size_area(name = "Count",
                      max_size = 24,
                      breaks = c(25000, 50000, 75000, 100000, 150000),
                      limits = c(0, 1500000)) +
      theme_minimal(16)
    

    enter image description here