How to Make a Bubble Plot in R

Channel:
Subscribers:
53,300
Published on ● Video Link: https://www.youtube.com/watch?v=ZMmZPznXtmw



Category:
Guide
Duration: 2:53
10,753 views
185


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.







Tags:
bubble plot in r
bubble plot in ggplot2
bubble plot in plotly
r plot aesthetics
r plotting basics
r plotting tutorial
bubble plot
r plotting
r data visualization
ggplot2
tutorial
data visualization
bubble chart
bubble plot ggplot2
ggplot2 bubble plot
ggplot bubble plot
bubble plot ggplot
r bubble plot
bubble plot in ggplot
how to make a bubble plot
bubble
plot
r bubble graph
r bubble chart
bubble plot r
bubble graph r
bubble chart r