# How to 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')``` 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')``` 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.