How To Make Boxplots with Text as Points in R using ggplot2?
Last Updated :
03 Dec, 2021
In this article, we will discuss how to make boxplots with text as points using the ggplot2 package in the R Programming language.
A box plot is a chart that shows data from a five-number summary including one of the measures of central tendency. These five summary numbers are Minimum, First Quartile, Median, Third Quartile, and Maximum which helps us in analyzing different statistical measures through visual representation.
To import & install ggplot2 package, we need to follow the below syntax:
install.package('ggplot2') # To install
import('ggplot2') # To import
Create Basic Boxplot
We can create a basic boxplot by using the geom_boxplot() function of the ggplot2 package in the R Language.
Syntax:
ggplot(dataframe, aes( x, y, color ) ) + geom_boxplot()
Example:
In this example, a basic boxplot is made using the geom_boxplot function of the ggplot2 package.
The CSV file used in the example can be downloaded here.
R
# Load library ggplot2
library(ggplot2)
# read sample_data from csv as a dataframe
sample_data <- read.csv("df.csv")
# use sample_data to plot a boxplot
# Color the plot by group using color parameter
ggplot(sample_data, aes(x=group, y=value, color=group))+
geom_boxplot()
Output:

Adding Data Points as Overlay:
To add jittered data points as an overlay to the boxplot, we will use the geom_jitter() function of the ggplot2 package. This function adds a layer over the boxplot with actual points plotted over it.
Syntax:
ggplot(dataframe, aes( x, y, color ) ) + geom_boxplot() + geom_jitter()
Parameters:
- x is categorical variable
- y is quantitative variable
- z is categorical variable used in the color plot by the group.
Example:
In this example, a boxplot is made using the geom_boxplot function of the ggplot2 package. The actual data points are overlayed to boxplot using geom_jitter() function.
R
# Load library ggplot2
library(ggplot2)
# read sample_data from csv as a dataframe
sample_data <- read.csv("df.csv")
# use sample_data to plot a boxplot
# Add jitter points to boxplot using
# geom_jitter() function
ggplot(sample_data, aes(x=group, y=value, color=group))+
geom_boxplot()+
geom_jitter()
Output:

Replacing data points with labels
Now to analyze the data we will replace the data points with their respective labels using the geom_text() function with parameter position. The geom_text() function replaces the data points with data labels but all labels come in a straight line. To make it jittered, we use the position parameter as position_jitter().
Syntax:
ggplot(dataframe, aes( x, y, color ) ) + geom_boxplot() + geom_text( position= position_jitter() )
Example:
Here, The actual data points are overlayed to boxplot as the label text using geom_text() function.
R
# Load library tidyverse
library(tidyverse)
# read sample_data from csv as a dataframe
sample_data <- read.csv("df.csv")
# use sample_data to plot a boxplot
# Add jitter points to boxplot using geom_jitter()
# function
ggplot(sample_data, aes(x=group,y=value, label = Label, color=group))+
geom_boxplot()+
geom_text(check_overlap = TRUE, position=position_jitter(width=0.15))
Output:
Similar Reads
How to plot means inside boxplot using ggplot2 in R? In this article, we are going to see how to plot means inside boxplot using ggplot in R programming language. A box plot in base R is used to summarise the distribution of a continuous variable. It can also be used to display the mean of each group. Means or medians can also be computed using a box
4 min read
How to Make Grouped Boxplots with ggplot2 in R? In this article, we will discuss how to make a grouped boxplot in the R Programming Language using the ggplot2 package. Boxplot helps us to visualize the distribution of quantitative data comparing different continuous or categorical variables. Boxplots consist of a five-number summary which helps i
3 min read
How To Show Mean Value in Boxplots with ggplot2? In this article, we will discuss how to show mean value in Boxplot with ggplot2 using R programming language. Firstly, we will create a basic boxplot using the geom_boxplot() function of the ggplot2 package and then do the needful, so that the difference is apparent. Syntax: ggplot() + geom_boxplot(
2 min read
How To Reorder Boxplots in R with ggplot2? In this article, we will discuss how to reorder the boxplot with ggplot2 in R Programming Language. To reorder the boxplot we will use reorder() function of ggplot2. Syntax: ggplot(sample_data, aes(x=reorder(name,value),y=value)) By default, ggplot2 orders the groups in alphabetical order. But for b
2 min read
How to Make Grouped Boxplot with Jittered Data Points in ggplot2 in R In this article, we will see how to make use of ggplot2 package in R Programming Language to plot grouped boxplots with jittered data points. Grouped Boxplots help us visualize two or more features/variables in a single plot using the grouping variable in ggplot2. The jittered points are data points
3 min read
How to save a plot using ggplot2 in R? In this article, we are going to see how to save GGPlot in R Programming language. ggplot2 is a plotting package in R that is used to create complex plots from data specified in a data frame. It provides a more programmatic interface for specifying which variables to plot on to the graphical device,
3 min read