Bubble plot with ggplot2 in R

• Difficulty Level : Expert
• Last Updated : 18 Jul, 2021

A bubble plot is a data visualization that helps to displays multiple circles (bubbles) in a two-dimensional plot as same in a scatter plot. A bubble plot is primarily used to depict and show relationships between numeric variables.

With ggplot2, bubble plots can be built using geom_point() function. At least three variables must be provided to aes() that are x, y, and size. The legend will automatically be displayed by ggplot2.

Syntax:

ggplot(data, aes(x, y, size)) + geom_point()

Size in the above syntax, will take in the name of one of the attributes based on whose values the size of the bubbles will differ.

Example: Creating bubble plot

R

 `# importing the ggplot2 library``library``(ggplot2)`` ` `# creating data set columns``x <- ``c``(12,23,43,61,78,54,34,76,58,103,39,46,52,33,11,9,60)``y <- ``c``(12,54,34,76,54,23,43,61,78,23,12,34,56,98,67,36,54)``r <- ``c``(1,5,13,8,12,3,2,16,7,40,23,45,76,8,7,41,23)`` ` `# creating the dataframe from the above columns``data <- ``data.frame``(x, y, r)`` ` `ggplot``(data, ``aes``(x = x, y = y,size = r))+ ``geom_point``(alpha = 0.7)`

Output:

In the above example, all the bubbles appear in the same color, hence making the graph hard to interpret. The colors can be added to plot to make the bubbles differ from one another.

Example: Adding colors to the bubble plot

R

 `# importing the ggplot2 library``library``(ggplot2)`` ` `# creating data set columns``x <- ``c``(12,23,43,61,78,54,34,76,58,103,39,46,52,33,11)``y <- ``c``(12,54,34,76,54,23,43,61,78,23,12,34,56,98,67)``r <- ``c``(1,5,13,8,12,3,2,16,7,40,23,45,76,8,7)`` ` `color <- ``c``(``rep``(``"color1"``, 1), ``rep``(``"color2"``, 2),``           ``rep``(``"Color3"``, 3), ``rep``(``"color4"``, 4),``           ``rep``(``"color5"``, 5))``# creating the dataframe from the above columns``data <- ``data.frame``(x, y, r, color)`` ` `ggplot``(data, ``aes``(x = x, y = y,size = r, color=color))+``geom_point``(alpha = 0.7)`

Output:

Color can be customized using the palette. To change colors with RColorBrewer palette add scale_color_brewer() to the plot code of ggplot2

Syntax:

scale_color_brewer(palette=<Palette-Name>)

Example: Customizing colors of bubble plot in R

R

 `# creating data set columns``x <- ``c``(12,23,43,61,78,54,34,76,58,103,39,46,52,33,11)``y <- ``c``(12,54,34,76,54,23,43,61,78,23,12,34,56,98,67)``r <- ``c``(1,5,13,8,12,3,2,16,7,40,23,45,76,8,7)`` ` `colors <- ``c``(1,2,3,1,2,3,1,1,2,3,1,2,2,3,3)`` ` `# creating the dataframe from the ``# above columns``data <- ``data.frame``(x, y, r, colors)`` ` `# importing the ggplot2 library and``# RColorBrewer``library``(RColorBrewer)``library``(ggplot2)`` ` `# Draw plot``ggplot``(data, ``aes``(x = x, y = y,size = r, color=``as.factor``(colors))) + ``geom_point``(alpha = 0.7)+ ``scale_color_brewer``(palette=``"Spectral"``)`

Output:

To change the size of bubbles in the plot i.e. giving the range of sizes from smallest bubble to biggest bubble, we use scale_size(). This allows setting the size of the smallest and the biggest circles using the range argument.

Syntax:

scale_size(range =< range-vector>)

Example: Changing size of the bubble plot

R

 `# creating data set columns``x <- ``c``(12,23,43,61,78,54,34,76,58,103,39,46,52,33,11)``y <- ``c``(12,54,34,76,54,23,43,61,78,23,12,34,56,98,67)``r <- ``c``(1,5,13,8,12,3,2,16,7,40,23,45,76,8,7)`` ` `color <- ``c``(``rep``(``"color1"``, 1), ``rep``(``"color2"``, 2),``           ``rep``(``"Color3"``, 3), ``rep``(``"color4"``, 4),``           ``rep``(``"color5"``, 5))``sizeRange <- ``c``(2,18)`` ` `# creating the dataframe from the above ``# columns``data <- ``data.frame``(x, y, r, color)`` ` `# importing the ggplot2 library``library``(ggplot2)`` ` `ggplot``(data, ``aes``(x = x, y = y,size = r, color=color)) ``                ``+ ``geom_point``(alpha = 0.7)``                ``+ ``scale_size``(range = sizeRange, name=``"index"``)`

Output:

To alter the labels on the axis, add the code +labs(y= “y axis name”, x = “x axis name”) to your line of basic ggplot code.

Example: Altering labels of bubble plot in R

R

 `# creating data set columns``x <- ``c``(12,23,43,61,78,54,34,76,58,103,39,46,52,33,11)``y <- ``c``(12,54,34,76,54,23,43,61,78,23,12,34,56,98,67)``r <- ``c``(1,5,13,8,12,3,2,16,7,40,23,45,76,8,7)`` ` `library``(``"RColorBrewer"``)``color <- ``brewer.pal``(n = 5, name = ``"RdBu"``)`` ` `# creating the dataframe from the above columns``data <- ``data.frame``(x, y, r, color)`` ` `# importing the ggplot2 library``library``(ggplot2)``ggplot``(data, ``aes``(x = x, y = y,size = r, color=color)) +``geom_point``(alpha = 0.7) + ``labs``(title= ``"Title of Graph"``, y=``"Y-Axis label"``, x = ``"X-Axis Label"``)`

Output:

To change legend position in ggplot2 add theme() to basic ggplot2 code.

Syntax:

theme(legend.position=<desired-position>)

Example: Changing legend position of bubble plot

R

 `# creating data set columns``x <- ``c``(12,23,43,61,78,54,34,76,58,103,39,46,52,33,11)``y <- ``c``(12,54,34,76,54,23,43,61,78,23,12,34,56,98,67)``r <- ``c``(1,5,13,8,12,3,2,16,7,40,23,45,76,8,7)`` ` `library``(``"RColorBrewer"``)``color <- ``brewer.pal``(n = 5, name = ``"RdBu"``)`` ` `# creating the dataframe from the above columns``data <- ``data.frame``(x, y, r, color)`` ` `# importing the ggplot2 library``library``(ggplot2)`` ` `ggplot``(data, ``aes``(x = x, y = y,size = r, color=color)) + ``geom_point``(alpha = 0.7) + ``labs``(title= ``"Title of Graph"``,y=``"Y-Axis label"``, x = ``"X-Axis Label"``) +``theme``(legend.position=``"left"``)`

Output:

My Personal Notes arrow_drop_up