How to Connect Data Points on Boxplot with Lines in R? - GeeksforGeeks

10 Oct, 2021
In this article, we will discuss how to connect paired points in box plot in ggplot2 in R Programming Language.
Boxplots with data points help us to visualize the summary information between distributions. For example, we may have two quantitative variables corresponding to two different categories and would like to connect those data points by lines. So to do this we have to load the tidyverse library
Syntax:
library(tidyverse)
Here is a basic box plot with lines joining paired points.
R
# load tidyverselibrary(tidyverse)  # create dataframesample_data < - data.frame(value=c(1, 2, 3, 4, 4, 5, 6,                                   7, 9, 11, 1.5, 2.3, 2.5, 3.4,                                   4.5, 5.5, 6.5, 7.5, 9.5, 12.5),                             category=c('A', 'B', 'A', 'B', 'A',                                      'B', 'A', 'B', 'A', 'B',                                      'A', 'B', 'A', 'B', 'A',                                      'B', 'A', 'B', 'A', 'B'),                             paired=c(0, 0, 1, 1, 2, 2, 3, 3, 4, 4,                                    5, 5, 6, 6, 7, 7, 8, 8, 9, 9))  # create plot using ggplot() and geom_boxplot() functionsggplot(sample_data, aes(category, value, fill=category)) +geom_boxplot()+  # geom_point() is used to make points at data valuesgeom_point()+  # geom_line() joins the paired datapointsgeom_line(aes(group=paired))
Output:
We can change the color and width of the joining segment by using the color and size property of geom_line().  
Syntax:
ggplot(aes( x, y )) + geom_boxplot()+geom_point( aes( fill ), size, shape ) + geom_line( aes( group ), size, color, alpha )
Parameters:
group: the variable that has pairs to be joined.
color: the variable that categorizes points
alpha: determines the transparency
size: determines the size
shape: determines the shape
fill: determines the fill color
Example:
R
# load tidyverselibrary(tidyverse)  # create dataframesample_data <- data.frame( value = c(1,2,3,4,4,5,6,                                     7,9,11,1.5,2.3,2.5,3.4,                                     4.5,5.5,6.5,7.5,9.5,12.5),                                                      category = c('A','B','A','B','A',                                        'B','A','B','A','B',                                        'A','B','A','B','A',                                        'B','A','B','A','B'),                                                      paired = c(0,0,1,1,2,2,3,3,4,4,                                     5,5,6,6,7,7,8,8,9,9))    # create plot using ggplot() and geom_boxplot() functionsggplot(sample_data, aes(category,value, fill=category)) +  geom_boxplot()+    # geom_line() joins the paired datapoints  # color and size parameters are used to customize line  geom_line(aes(group = paired), size=2, color='gray', alpha=0.6)+     # geom_point() is used to make points at data values  # fill and size parameters are used to customize point  geom_point(aes(fill=category,group=paired),size=5,shape=21)
Output:
We can convert the joining line into ridged line by using linetype property. Every property that works on a line plot also works in this line.
Syntax:
ggplot(aes( x, y )) + geom_boxplot()+geom_point() + geom_line( aes( group ), linetype )
Parameters
group: the variable that has pairs to be joined.
linetype: determines the type of line
Example:
R
# load tidyverselibrary(tidyverse)  # create dataframesample_data <- data.frame( value = c(1,2,3,4,4,5,6,                                     7,9,11,1.5,2.3,2.5,3.4,                                     4.5,5.5,6.5,7.5,9.5,12.5),                                                      category = c('A','B','A','B','A',                                        'B','A','B','A','B',                                        'A','B','A','B','A',                                        'B','A','B','A','B'),                                                      paired = c(0,0,1,1,2,2,3,3,4,4,                                     5,5,6,6,7,7,8,8,9,9))    # create plot using ggplot() and geom_boxplot() functionggplot(sample_data, aes(category,value, fill=category)) +  geom_boxplot()+  # linetype parameter is used to customize the joining line  geom_line(aes(group = paired), linetype=2, size=1.3)+  # geom_point() is used to plot data points on boxplotgeom_point(aes(fill=category,group=paired),size=5,shape=21)
Output:
Picked
R-ggplot
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
How to filter R dataframe by multiple conditions?
R - if statement
How to import an Excel File into R ?
Time Series Analysis in R