Bubble Chart in R Programming

In this article, we will talk about another type of chart in R programming, i.e., the Bubble chart.

A bubble chart is a type of data visualization chart that helps display multiple bubbles or circles in a 2-dimensional plot, just like a scatter plot.

The bubble chart is mostly used when we have to depict and show the relationship between numeric variables.

We can plot a bubble chart by using the built-in function geom_point() with ggplot2.

Syntax of Bubble chart:

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

To plot a bubble chart, at least 3 must be provided to aes(), which are x, y, and size. And the bubble chart will automatically be displayed by ggplot2.

geom_point() is the in-built function.

size refers to one of the attributes based on the value, i.e., the size of bubbles will differ.

Let’s try to create a simple bubble chart.

Example:

CODE:

# Bubble chart
# The first step is to import the ggplot2 library
library(ggplot2)	


# Then create data set columns
x <- c(56,23,33,71,68,58,44,72,58,114,29,52,47,39,16,9,80)
y <- c(12,24,34,46,54,63,73,61,78,83,92,134,46,88,57,31,43)
r <- c(1,4,12,7,11,2,3,15,6,39,22,44,75,7,6,40,22)


# Once you are done creating data set columns, we will create a 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)

When we try to execute the above code, we get the following result:

OUTPUT:

BUBBLE CHART

We can add colors to our bubble chart to differentiate one bubble from another. We can see from the above example that all the bubbles in our chart are of the same color. Hence, it makes the graph very hard to interpret.

Example: In this example, we will add colors to the bubble chart.

CODE:

# Bubble chart with different colors.
# The first step is to import ggplot2 library.
library(ggplot2)


# Then create data set columns
x <- c(56,23,33,71,68,58,44,72,58,114,29,52,47,39,16,9,80)
y <- c(12,24,34,46,54,63,73,61,78,83,92,134,46,88,57,31,43)
r <- c(1,4,12,7,11,2,3,15,6,39,22,44,75,7,6,40,22)


# Adding color
color <- c(rep("color1", 1), rep("color2", 2),
           rep("Color3", 3), rep("color4", 4),
           rep("color5", 5))


# Once you are done creating data set columns, we will create a 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)

When we execute the above code, we get following the result:

OUTPUT:

BUBBLE CHART

We can customize the color palette. We can change colors with the RColorBrewer palette and scale_color_brewer() of ggplot2.

Syntax of changing color:

Scale_color_brewer(palette=<Palette-Name>)

Example: In this example, we can customize the colors of the bubble chart in R programming.

CODE:

# Bubble Chart
#Here, we are creating data set columns
x <- c(56,23,33,71,68,58,44,72,58,114,29,52,47,39,16,9,80)
y <- c(12,24,34,46,54,63,73,61,78,83,92,134,46,88,57,31,43)
r <- c(1,4,12,7,11,2,3,15,6,39,22,44,75,7,6,40,22)


colors <- c(1,2,3,1,2,3,1,1,2,3,1,2,2,3,3)


# Now, we will be creating the dataframe from the above column’s
data <- data.frame(x, y, r, colors)


# importing the ggplot2 library and RColorBrewer library


library(RColorBrewer)
library(ggplot2)


# Now, we will 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")

When we execute the above code, we get the following output:

OUTPUT:

BUBBLE CHART

 Changing Size of Bubble Chart:

To change the size of the bubble chart, we use scale_size(), i.e. giving the range of sizes from the smallest to the biggest bubble.

Syntax:

Scale_size(range=<range-vector)

Example: In this example, we will change the size of the bubble chart.

CODE:

# creating  the data set columns
x <- c(56,23,33,71,68,58,44,72,58,114,29,52,47,39,16,9,80)
y <- c(12,24,34,46,54,63,73,61,78,83,92,134,46,88,57,31,43)
r <- c(1,4,12,7,11,2,3,15,6,39,22,44,75,7,6,40,22)


color <- c(rep("color1", 1), rep("color2", 2),
           rep("Color3", 3), rep("color4", 4),
           rep("color5", 5))
sizeRange <- c(2,18)


# Now we are creating the dataframe from the above column’s
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")

When we execute the above code, we get the following output:

OUTPUT

BUBBLE CHART

Adding a theme to the bubble chart:

When we want to change the legend function in ggplot2, we can add theme() to our basic ggplot2

Syntax:

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

Example: Let’s look at an example to have a better understanding.

CODE:

# Here, we are creating data set columns
x <- c(56,23,33,71,68,58,44,72,58,114,29,52,47,39,16,9,80)
y <- c(12,24,34,46,54,63,73,61,78,83,92,134,46,88,57,31,43)
r <- c(1,4,12,7,11,2,3,15,6,39,22,44,75,7,6,40,22)


# Let's import RColorBrewer
library("RColorBrewer")
color <- brewer.pal(n = 5, name = "RdBu")


# Now we will create the dataframe from the above column’s
data <- data.frame(x, y, r, color)


# Let's import the ggplot2 library into our R studio.
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")

When we execute the above code, we get the following result.

OUTPUT:

BUBBLE CHART

So, this was all about plotting the various types of bubble charts in R programming.


Related Topics

Binomial Distribution in R Programming

In this article, we will talk about the Binomial distribution in R programming. The binomial distribution is a type of probability distribution. As it is a discrete distribution, it will...

3 minutes read.

R Data Types

Data types are used to define the size and type of the variable. In R, there is no need to declare a variable as some data types. The variables are...

4 minutes read.

Linear Regression in R Programming

Linear Regression is an unsupervised machine learning algorithm in R programming. Linear Regression is a type of regression analysis that shows the relationship between two or more variables. And one...

3 minutes read.

R If Else Statement

If statement It is the most simple decision-making statement. It checks the condition if the condition is true then only it will execute the block of statements written in the ‘if...

2 minutes read.

R Variables, Constants and Reserved Words

R Variables Variables are very similar to an open box, where you can put any value of your wish. We can change the values as well. Variables are used to store...

4 minutes read.

Normal Distribution in R Programming

In statistics, the normal distribution is a type of probability function. Normal distribution tells the user about the distribution of data values in the dataset. It is a very important...

3 minutes read.

Scatter Plot in R Programming

A Scatter plot is a type of dispersion graph built to represent the various data points of variables. A Scatter plot is also known as a scatter graph or scattergram.  A scatter plot is used...

4 minutes read.

R Date and Time

R provides very large range of capabilities to deal with times and dates. Generally, dates are internally stored as integers, and depending on the operations we will need, we could...

3 minutes read.

Decision Tree in R Programming

The decision tree uses the branching method to show every possible output for the specific input. We can draw a decision tree by hand or create it using specialized software...

2 minutes read.

Pie Charts in R Programming

R programming has various libraries to plot various types of charts and graphs. A pie chart is a circular statistical chart or graph, which is divided into slices to represent the numerical...

4 minutes read.

R Pie Charts

R programming language provides many numbers of libraries to create charts and graphs. A pie-chart is a representation of data as slices of a circle with different colors representing counts...

2 minutes read.

Analysis of Covariance in R Programming

Analysis of Covariance can also be named ANCOVA. We know that we use the concept of regression analysis for creating models which can explain the effect of the variation in...

2 minutes read.

R Switch Statement

It is an alternate way to a nested if…else statement. A switch statement permits an expression to be tested against a list of case values. In short, we can say...

1 minute read.

R Tutorial

Introduction to R Programming R is nothing but a programming language, also a free software environment. This software used for predictive analysis, statistical analysis, graphical representation, data modeling, and reporting. R...

9 minutes read.

R Excel Files

Microsoft Excel is the spreadsheet program which stores data in the .xls and .xlsx format. R has the facility to read directly from these files using some excel specific packages....

2 minutes read.

R Inheritance

One the main concept of object oriented programming is inheritance which allows us to define a new class of existing classes. This means that we can derive new classes from...

5 minutes read.

R JSON File

JSON stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data. JSON file is often used when data is sent from a server to a...

2 minutes read.

R Operators

A symbol that tells the compiler to perform specific mathematical or logical operations is called operator. R language supports mainly 5 different types of operators, which are listed below: Arithmetic...

3 minutes read.

Introduction to R Programming

What is R Programming? R is a programming language which provides an environment for software used for statistical analysis, graphics representation and reporting. It possesses an extensive catalog of...

4 minutes read.

How to create Histograms in R

R Histograms A Histogram is the graphical representation of the distribution of numeric data. It takes only one numeric variable as input. The variable is cut into several bars (also called...

2 minutes read.