How to Make a Bubble Plot in R @DataDaft
How to Make a Bubble Plot in R  @DataDaft
Uploaded November 2019 | Updated November 2026, 3 hours ago
Bubble plots are scatter plots with a third numeric variable mapped to the size of the points on the plot. You can create bubble plots using the ggplot2 package by creating a scatterplot and mapping a third variable to the side aesthetic.

Code used in this code clip:

library(tidyverse)
library(plotly)

data <- mtcars %>% mutate(cyl = factor(cyl),
Model = rownames(mtcars))

# Basic bubble plot in ggplot2

plot1 <- data %>% ggplot(aes(x = wt, y = mpg, size = hp)) +
geom_point(alpha = 0.5)

plot1

# Bubble plot with color and custom size

plot2 <- data %>% ggplot(aes(x = wt, y = mpg, size = hp,
color = cyl, label = Model)) +
geom_point(alpha = 0.5) +
scale_size(range = c(.1, 15))

plot2

# Convert ggplot into plotly plot
p <- ggplotly(plot2, width=500, height=500) %>%
layout(xaxis = list(range = c(1, 6)),
yaxis = list(range = c(8, 35)),
legend = list(x = 0.825, y = .975))

p


Code Clips are basic code explanations in 3 minutes or less. They are intended to be short reference guides that provide quick breakdowns and copy/paste access to code needed to accomplish common data science tasks. Think Stack Overflow with a video explanation.


* Note: YouTube does not allow greater than or less than symbols in the text description, so the code above may not be exactly the same as the code shown in the video! For R that means I may use = for assignment and the special Unicode large < and > symbols in place of the standard sized ones for dplyr pipes and comparisons. These special symbols should work as expected for R code on Windows, but may need to be replaced with standard greater than and less than symbols for other operating systems.
How to Make a Bubble Plot in Rstringr: String InterpolationHow to Sort a Data Frame by a Column in PandasPython Programming Practice: LeetCode #13   Roman to IntegerPython Programming Practice: LeetCode #58   Length of Last WordAlgorithms Explained: Memory ComplexityHow To Use applymap() In Pandas (Python)Songs of the 70s: All #1 HitsHow to Make a Donut Chart in RHow to Add a Row To a Data Frame in Pandas (Python)How to Make Pie Charts in RHow to Rename Columns in Pandas (Python)
DataDaft |

How to Make a Bubble Plot in R

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER