Bubble Chart in R-ggplot & Plotly

[This article was first published on Methods – finnstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Bubble Chart in R, A bubble chart is majorly used to show the relationships between numeric variables and it can visualize two to four dimensions.

The first two dimensions for coordinates, the third dimension is for color and the fourth as size.

Bubble chart is an enhancement of the normal scatter plot instead of traditional dots or points in the scatter plot are replaced by circles or bubbles.

In this article will describe how to create bubble chart based on ggplot2 and plotly.

Bubble Chart in R

First, load ggplot2 package in R

Method 1:-

In ggplot2 allows to render almost all type of charts and graphs. In this article approach is like load the library, create a data frame and display plot.

library(ggplot2)

Load the data from mtcars

data("mtcars")
df <- mtcars

Let’s convert cyl as a grouping variable

QQ-plots in R: Quantile-Quantile Plots-Quick Start Guide »

df$cyl <- as.factor(df$cyl)

Let’s see the data frame

head(df[, c("wt", "disp", "cyl", "qsec")])
                   wt disp cyl  qsec
Mazda RX4         2.620  160   6 16.46
Mazda RX4 Wag     2.875  160   6 17.02
Datsun 710        2.320  108   4 18.61
Hornet 4 Drive    3.215  258   6 19.44
Hornet Sportabout 3.440  360   8 17.02
Valiant           3.460  225   6 20.22

Points size is controlled by a continuous variable, in this case qsec. argument alpha is used to control color transparency and should lies between 0 and 1.

ggplot(df, aes(x = wt, y = disp)) +
  geom_point(aes(color = cyl, size = qsec), alpha = 0.5) +
  scale_color_manual(values = c("#AA4371", "#E7B800", "#FC4E07")) +
  scale_size(range = c(1, 13)) + # Adjust the range of points size
  theme_set(theme_bw() +theme(legend.position = "bottom"))
Create a bubble chart in R ggplot2

Correlation Analysis Different Types of Plots in R »

Method 2:-

library(plotly)
bubbleplot <- plot_ly(df, x = ~wt, y = ~disp,
                      text = ~cyl, size = ~qsec,
                      color = ~cyl, sizes = c(10, 50),
                      marker =
                        list(opacity = 0.7,
                             sizemode = "diameter"))
bubbleplot <- bubbleplot%>%layout
bubbleplot
Bubble chart in R Plotly

Enjoyed this article? Don’t forget to show your love, Please Subscribe the Newsletter and COMMENT below!

Funnel Chart in R-Interactive Funnel Plot »

The post Bubble Chart in R-ggplot & Plotly appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Methods – finnstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)