15.1 C
London
Friday, July 5, 2024
HomeStatistics TutorialRHow to Create a Bubble Chart in R

How to Create a Bubble Chart in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 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.

Additional Resources

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

How to Create a Pareto Chart in R
How to Plot Multiple Lines in One Chart in R
How to Plot Multiple Boxplots in One Chart in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories