How to Create a Bubble Chart in R

Spread the love

A Bubble Chart is a type of chart that displays three dimensions of data. Each entity with its triplet (v1, v2, v3) of associated data is plotted as a bubble. Here, v1 and v2 could be visualized as the coordinates of the data on the x and y axes, respectively, while v3 could be visualized as the size of the bubble.

Bubble Charts are very useful when we want to compare and visualize the relationships among categorized circles, by the use of positioning and proportions. They can represent data with three or more dimensions, allowing for the comparison of entities in terms of their relative positions and sizes.

R, being a powerful language for statistical analysis, provides several ways to create a bubble chart. In this article, we will explore how to create a bubble chart using two popular methods in R: using the base R symbols() function and using the ggplot2 package.

Creating a Bubble Chart Using Base R

The base R function symbols() allows us to plot symbols on a chart. We can use it to create a bubble chart by defining the x and y coordinates and the size of the symbols.

Here are the steps involved:

1. Creating a Dataset: First, let’s create a simple dataset.

# Create a dataset
set.seed(123)
df <- data.frame(x = rnorm(10), 
                 y = rnorm(10), 
                 size = rnorm(10, mean = 20, sd = 5))

In this dataset, x and y are the coordinates of each bubble, and size determines the size of the bubble.

2. Creating the Bubble Chart: We use the symbols() function to create the bubble chart.

# Create a bubble chart
symbols(df$x, df$y, circles = df$size, inches = 0.5, fg = "darkgreen", bg = "lightgreen",
        xlab = "X-Axis", ylab = "Y-Axis", main = "Bubble Chart")

In this code, symbols() creates the bubble chart with the x and y coordinates and the size of the bubbles. The circles argument defines the variable to be used for the size of the bubbles, inches controls the size of the bubbles, fg sets the color of the borders of the bubbles, bg sets the fill color of the bubbles, and xlab, ylab, and main add labels to the x-axis, y-axis, and the chart title, respectively.

Creating a Bubble Chart Using ggplot2

ggplot2 is a powerful and flexible package for creating plots in R. We can create a bubble chart using the ggplot() function combined with the geom_point() function.

Here are the steps involved:

1. Installing and Loading the ggplot2 Package: First, we need to install and load the ggplot2 package.

# Install the package
install.packages("ggplot2")

# Load the package
library(ggplot2)

2. Creating a Dataset: The step is similar to the base R approach.

# Create a dataset
set.seed(123)
df <- data.frame(x = rnorm(10), 
                 y = rnorm(10), 
                 size = rnorm(10, mean = 20, sd = 5))

3. Creating the Bubble Chart: We use the ggplot() function combined with the geom_point() function to create the bubble chart.

# Create a bubble chart
ggplot(df, aes(x = x, y = y)) +
  geom_point(aes(size = size), alpha = 0.5, color = "darkgreen", fill = "lightgreen") +
  scale_size_continuous(range = c(1, 20)) +
  labs(title = "Bubble Chart", x = "X-Axis", y = "Y-Axis") +
  theme_minimal()

In this code, ggplot() initializes the plot with the data frame and the x and y aesthetics. geom_point() adds the bubbles to the plot with the size aesthetic mapped to the size variable. The alpha argument controls the transparency of the bubbles, and color and fill set the color of the borders and the fill color of the bubbles, respectively. The scale_size_continuous() function allows us to adjust the size range of the bubbles. Finally, labs() adds labels to the x-axis, y-axis, and the chart title, and theme_minimal() sets the theme of the plot.

Conclusion

A Bubble Chart is a multi-variable graph that is a cross between a Scatterplot and a Proportional Area chart. Its data points are replaced with bubbles, and the size of the bubble represents the third numeric characteristic. It’s an effective means of displaying three-dimensional data, and R offers several ways to create these charts. This article demonstrated two methods of creating a Bubble Chart in R, one using base R functions and the other using the ggplot2 package.

Posted in RTagged

Leave a Reply