How to Connect Paired Points with Lines in Scatterplot in ggplot2 in R?
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 ) )
- group: the variable that has pairs to be joined.
- color: the variable that categorizes points
Example 1: Creating a Scatter plot with lines joining paired points
Here is a basic scatter plot with lines joining paired points.
We can change the color and width of the joining segment by using the color and size property of geom_line().
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.