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

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)`

### Example 1:  Creating a Boxplot with lines joining paired points

Here is a basic box plot with lines joining paired points.

## R

 `# load tidyverse ` `library``(tidyverse) ` ` `  `# create dataframe ` `sample_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() functions ` `ggplot``(sample_data, ``aes``(category, value, fill=category)) + ` `geom_boxplot``()+ ` ` `  `# geom_point() is used to make points at data values ` `geom_point``()+ ` ` `  `# geom_line() joins the paired datapoints ` `geom_line``(``aes``(group=paired)) `

Output:

## Color Customization:

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 tidyverse ` `library``(tidyverse) ` ` `  `# create dataframe ` `sample_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() functions ` `ggplot``(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:

## Line Customization

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 tidyverse ` `library``(tidyverse) ` ` `  `# create dataframe ` `sample_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() function ` `ggplot``(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 boxplot ` `geom_point``(``aes``(fill=category,group=paired),size=5,shape=21)`

Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next