plotly Bubble Chart in R (3 Examples)

 

Hello! This tutorial will show you how to create bubble plots in plotly in the R programming language.

First, though, here is a quick overview of this tutorial:

Let’s dive into the R code!

 

Install & Load plotly & dplyr

If you do not have the plotly and dplyr libraries already installed in your R programming environment, then run the lines of code below in either R Studio or in any other code editor that can run R to install and load both plotly and dplyr; otherwise, you can skip to the next section.

# install plotly and dplyr
install.packages(c("plotly", "dplyr")) 
 
# load plotly and dplyr
library(plotly)
library(dplyr)

The R dplyr library is the foremost library for data manipulation and analysis in R programming, and it enables us to run several lines of code together as a chunk using the pipe operator.
 

Gapminder Dataset

In this tutorial, we will use the gapminder dataset to build a bubble plot. In R Studio, you can import the gapminder dataset after loading the library by running install.packages("gapminder") and library(gapminder). Then you can call the dataset by its name. Alternatively, you can download the dataset from online dataset repositories.

Once the data is imported, we will filter the data by year and get the data for the year 2002.

data_2002 <- gapminder |> 
  filter(year == 2002)

Then we can preview the first 10 rows of the data as follows.

data_2002 |> head(10)
 
 
 #   country    continent  year lifeExp     pop   gdpPercap
 #1 Afghanistan Asia       2002    42.1  25268405      727.
 #2 Albania     Europe     2002    75.7   3508512     4604.
 #3 Algeria     Africa     2002    71.0  31287142     5288.
 #4 Angola      Africa     2002    41.0  10866106     2773.
 #5 Argentina   Americas   2002    74.3  38331121     8798.
 #6 Australia   Oceania    2002    80.4  19546792    30688.
 #7 Austria     Europe     2002    79.0   8148312    32418.
 #8 Bahrain     Asia       2002    74.8    656397    23404.
 #9 Bangladesh  Asia       2002    62.0 135656790     1136.
#10 Belgium     Europe     2002    78.3  10311970    30486.

As seen, the dataset shows some life standard measurements and the population per country.

 

Example 1: Simple Bubble Plot

In this first example, we will build a simple bubble plot. However, we will plot the first 50 rows of the dataset; hence the plot doesn’t get too congested.

fig <- data_2002 |> head(50) |>
  plot_ly(x = ~lifeExp,
          y = ~gdpPercap,
          text = ~continent,
          type = "scatter",
          mode = "markers",
          marker = list(size = 60,opacity = 0.7)) |> 
  layout(title = "Life Expectancy Vs GDP",
         xaxis = list(showgrid = FALSE),
         yaxis = list(showgrid = FALSE))
 
 
fig


In the plot_ly() function, we specified that we wanted the hover text to be the names of the continents, by defining the argument text = ~continent. We also set the bubble size to 60 and opacity to 0.7.

 

Example 2: Mapping Colors by Categorical Variable

In this second example, we will color the bubbles by the continents.

fig <- data_2002 |> head(50) |> 
  plot_ly(x = ~lifeExp,
          y = ~gdpPercap,
          text = ~continent,
          type = "scatter",
          color = ~continent,
          mode = "markers",
          marker = list(size = 60,opacity = 0.7)) |> 
  layout(title = "Life Expectancy Vs GDP",
         xaxis = list(showgrid = FALSE),
         yaxis = list(showgrid = FALSE))
 
fig


We colored the bubbles by parsing ~continent to the color = argument. Now, you can select continents to visualize by toggling the legend keys.
 

Example 3: Mapping Colors by Continuous Variable

In this third and final example, we will color the bubbles by the population variable.

fig <- data_2002 |> head(50) |> 
  plot_ly(x = ~lifeExp,
          y = ~gdpPercap,
          text = ~continent,
          color = ~pop,
          colors = "Set3",
          type = "scatter",
          mode = "markers",
          marker = list(size = 60,opacity = 0.3)) |> 
  layout(title = "Life Expectancy Vs GDP",
         xaxis = list(showgrid = FALSE),
         yaxis = list(showgrid = FALSE))
 
 
fig


In the code above, we colored the bubbles by parsing ~pop to the color = argument, and also gave the "Set3" color scale to the colors = argument, which created the bubble plot’s color gradient.

So, that is how to make a plotly bubble plot in the R programming language. You can build bubble plots of other kinds of datasets as well.

I hope you found this tutorial helpful!

 

Video, Further Resources & Summary

Do you need more explanations on how to make plotly bubble plots in R? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to make plotly bubble plots in R.

 

The YouTube video will be added soon.

I hope this tutorial has helped you learn how to make plotly bubble plots in R. If you would like to learn more about using plotly in the R programming language, then be sure to check out other interesting plotly in R tutorials on Statistics Globe:

 

This post has shown how to make plotly bubble plot in R. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top