How can I create a bubble chart in R? 2

How can I create a bubble chart in R?

A bubble chart is a type of data visualization that uses circles or bubbles to represent numerical data. It is a useful tool for displaying relationships between different sets of data. To create a bubble chart in R, you can use the ggplot2 package, which provides a simple and efficient way to generate high-quality plots. First, you will need to load the ggplot2 library into your R session. Then, you can use the geom_point() function to create the bubbles, specifying the data, the x and y coordinates, and the size of the bubbles. You can also customize the appearance of the bubbles by adding color, transparency, and labels. By following these steps, you can easily generate a bubble chart in R to effectively visualize your data.

Create a Bubble Chart in R


A bubble chart is a type of chart that allows you to visualize three variables in a dataset at once.

The first two variables are used as (x,y) coordinates on a scatterplot and the third variable is used to depict size.

You can use the following basic syntax to create a bubble chart in R:

library(ggplot2)

#create bubble chart
ggplot(df, aes(x=x_var, y=y_var, size=size_var)) +
  geom_point(alpha=0.5) +
  scale_size(range=c(2, 10), name='Legend Name')

The following example shows how to use this syntax to create a bubble chart in practice.

Note: The alpha argument specifies that the circles in the chart should be partially transparent. The range argument allows you to set the minimum and maximum radius values for the circles in the chart.

Example: Create a Bubble Chart in R

Suppose we have the following data frame in R that contains information about various basketball players:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'),
                 points=c(8, 11, 13, 13, 15, 18, 22, 27, 32),
                 assists=c(4, 3, 6, 5, 4, 7, 8, 11, 6),
                 minutes=c(9, 12, 15, 20, 36, 30, 31, 40, 43))

#view data frame
df

  team points assists minutes
1    A      8       4       9
2    A     11       3      12
3    A     13       6      15
4    B     13       5      20
5    B     15       4      36
6    B     18       7      30
7    C     22       8      31
8    C     27      11      40
9    C     32       6      43

We can use the following syntax to create a bubble chart that displays assists on the x-axis, points on the y-axis, and uses minutes to determine the size of the circles:

library(ggplot2)

#create bubble chart
ggplot(df, aes(x=assists, y=points, size=minutes)) +
  geom_point(alpha=0.5) +
  scale_size(range=c(2, 10), name='Minutes Played')

You can change the color of all of the circles by using the color argument within the geom_point() function:

library(ggplot2)

#create bubble chart with blue circles
ggplot(df, aes(x=assists, y=points, size=minutes)) +
  geom_point(alpha=0.5, color='steelblue')  +
  scale_size(range=c(2, 10), name='Minutes Played')

bubble chart in R

Alternatively, you can use the color argument within aes() to make the color of each circle based on the value of another variable in the data frame:

library(ggplot2)

#create bubble chart and color circles based on value of team variable
ggplot(df, aes(x=assists, y=points, size=minutes, color=team)) +
  geom_point(alpha=0.5)  +
  scale_size(range=c(2, 10), name='Minutes Played')

bubble chart in R with color based on condition

The color of each circle in the plot is now dependent on the value for the team variable.

Note: Feel free to play around with the minimum and maximum values in the range argument to increase or decrease the size of the circles in the plot.

The following tutorials explain how to create other common charts in R:

Cite this article

stats writer (2024). How can I create a bubble chart in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-bubble-chart-in-r/

stats writer. "How can I create a bubble chart in R?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-bubble-chart-in-r/.

stats writer. "How can I create a bubble chart in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-bubble-chart-in-r/.

stats writer (2024) 'How can I create a bubble chart in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-bubble-chart-in-r/.

[1] stats writer, "How can I create a bubble chart in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I create a bubble chart in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top