Creating Bubble Charts in R

A bubble chart is a type of data visualization that shows the relationship between three variables at the same time. It looks like a regular scatter plot with X and Y coordinates, but it also uses the size of the bubbles to show an extra variable. This makes bubble charts very useful for visualizing data with multiple dimensions.

How to Set Shape Types

You can create a bubble chart using the geom_point function from the ggplot2 package. Use the shape option to set different patterns for the (x, y) coordinates. Shapes for circles are 1, 16, and 21, each used for different purposes. For more details on shape patterns, check out the previous post linked below.

geom_point shape 설정하기

shape=1 is a circle with just an outline and no fill. shape=16 is a filled circle. shape=21 lets you use the fill option to change the inside color separately.

With shape=1, you get an empty circle. Also, I set the point size to 30, color to blue, and border thickness to 3. When using geom_point, setting the size can change the axis ranges, so I limited them to 0 to 2.

tb1 = tibble(x = 1, y = 1)

ggplot(tb1) +
  geom_point(aes(x,y), size = 30, 
             colour = "blue", 
             shape = 1, stroke = 3) +
  scale_x_continuous(limits = c(0,2)) +
  scale_y_continuous(limits = c(0,2))

When shape=16, the shape is a solid circle. You can only specify the color using the colour option. The other options remain the same as the graph above.

ggplot(tb1) +
  geom_point(aes(x,y), size = 30, 
             colour = "blue",
             shape = 16, stroke = 3) +
  scale_x_continuous(limits = c(0,2)) +
  scale_y_continuous(limits = c(0,2))

When shape=21, you can set different colors for the circle’s outline and fill. I made the outline blue and the fill skyblue. While you can map values to the outline color for data visualization, it’s often used more for design purposes in graphs.

ggplot(tb1) +
  geom_point(aes(x,y), size = 30, 
             colour = "blue", fill = "skyblue",
             shape = 21, stroke = 3) +
  scale_x_continuous(limits = c(0,2)) +
  scale_y_continuous(limits = c(0,2))

Finally, I created a bubble chart with random data. The size of the points is mapped to the data s, and the color is based on s as well. I set the transparency (alpha) to 0.6 to avoid obscuring overlapping points. I used the Viridis color palette for the color theme and set the size scale from 10 to 30. If you don’t specify the size range with scale_size_continuous, the points might appear too small, so it’s important to adjust the range to find an appropriate size.

Applying Colors to ggplot Graphs (viridis, brewer scales)

ggplot(tibble(x = sample(1:20,20), y = sample(1:20,20), s = sample(1:50, 20))) +
  geom_point(aes(x,y, size = s, colour = s), 
             alpha = 0.6, shape = 16) +
  scale_colour_viridis_c(option = "D", begin = 0.3, end = 1) +
  scale_size_continuous(range = c(10, 30)) +
  scale_x_continuous(limits = c(0,20)) +
  scale_y_continuous(limits = c(0,20))


See also