How to Connect Paired Points with Lines in Scatterplot in ggplot2 in R? - GeeksforGeeks

05 Oct, 2021
In this article, we will discuss how to connect paired points in scatter plot in ggplot2 in R Programming Language.
Scatter plots help us to visualize the change in two more categorical clusters of data. Sometimes, we need to work with paired quantitative variables and try to visualize their relationship. For example, we may have two quantitative variables corresponding to two different categories and would like to connect those data points by lines.
Syntax: ggplot(aes( x, y )) + geom_point( aes( color ) ) + geom_line( aes( group ) )
Where: 
group: the variable that has pairs to be joined.
color: the variable that categorizes points
Here is a basic scatter plot with lines joining paired points.
R
# create dataframesample_data <- data.frame( value1 = c(31,12,15,28,45,                                      21,22,34,56,10),                                                      value2 = c(1,2,4,8,16,3,9,                                     7,5,11),                                                      category1 = c('A','B','A','B','A',                                        'B','A','B','A','B'),                                                      category2 = c(0,0,1,1,2,2,3,3,4,4))# load tidyverselibrary(tidyverse)  # create plotggplot(sample_data, aes(value1,value2)) +  geom_point(aes(color=category1, size=3)) +  geom_line(aes(group = category2))
Output:
We can change the color and width of the joining segment by using the color and size property of geom_line().                         
R
# create dataframesample_data < - data.frame(value1=c(31, 12, 15, 28, 45, 21,                                    22, 34, 56, 10),                           value2=c(1, 2, 4, 8, 16, 3, 9, 7, 5, 11),                             category1=c('A', 'B', 'A', 'B', 'A',                                       'B', 'A', 'B', 'A', 'B'),                             category2=c(0, 0, 1, 1, 2, 2, 3, 3, 4, 4))# load tidyverselibrary(tidyverse)  # create plotggplot(sample_data, aes(value1, value2)) +geom_line(aes(group=category2), color="grey",          size=2, alpha=0.5) +geom_point(aes(color=category1, size=7))
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.
R
# create dataframesample_data < - data.frame(value1=c(31, 12, 15, 28, 45, 21,                                    22, 34, 56, 10),                             value2=c(1, 2, 4, 8, 16, 3, 9, 7, 5, 11),                             category1=c('A', 'B', 'A', 'B', 'A', 'B',                                       'A', 'B', 'A', 'B'),                             category2=c(0, 0, 1, 1, 2, 2, 3, 3, 4, 4))# load tidyverselibrary(tidyverse)  # create plot with ridged lineggplot(sample_data, aes(value1, value2)) +geom_line(aes(group=category2), linetype=2) +geom_point(aes(color=category1, size=3))
Output:
Picked
R-ggplot
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
R - if statement
Time Series Analysis in R
Plot mean and standard deviation using ggplot2 in R