Adding Legend to Multiple Line Plots with ggplot in R
Last Updated :
23 Aug, 2021
In this article, we are going to see how can we add a legend to multiple line plots with ggplot in the R programming language.
For a plot that contains more than one line plot, a legend is created by default if the col attribute is used. All the changes made in the appearance of the line plots will also reflect in the legend.
Syntax: ggplot(df, aes(x, y, col="name of the column to differentiate on the basis of"))
Let us first visualize how the curve will appear as default.
Example: Default plot
R
library("ggplot2")
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2","function3","function4"))
)
ggplot(df,aes(x,values,col=fun))+geom_line()
Output:
Various changes made to the lines will appear in the legends as well, let's see how:
Changing color
To change color by default, the above plot will be produced, now suppose we manually want to add colors to the line, for that any of the given functions- scale_color_manual(), scale_color_brewer(), and scale_color_grey().
Example: changing color of the lines
R
library("ggplot2")
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2",
"function3","function4"))
)
ggplot(df,aes(x,values,col=fun))+geom_line()+
scale_color_manual(values=c("green","yellow","red","grey"))
Output:

Changing line thickness
For this, the size attribute is used with a specific value. The changes will be reflected in the legend too.
Example:
R
library("ggplot2")
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2",
"function3","function4"))
)
ggplot(df,aes(x,values,col=fun))+geom_line(size=3)
Output:

Changing line type
Line type of the plots can be changed using any of the given functions- scale_linetype_manual(), or by default through the use of linetype keyword.
Example:
R
library("ggplot2")
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2",
"function3","function4"))
)
ggplot(df,aes(x,values, group=fun, color=fun, linetype=fun))+
geom_line(size=1)+
scale_linetype_manual(values = c("solid","dotted","dashed","twodash"))+
scale_color_manual(values=c("red","green","blue","black"))
Output:
Similar Reads
Add legend for multiple lines in R using ggplot2 In this article, we are going to see how to add legends for multiple line plots in R Programming Language using ggplot2. First, you need to install the ggplot2 package if it is not previously installed in R Studio. The functions used to create the line plots are : geom_line( ) : To plot the line and
3 min read
How to create a plot using ggplot2 with Multiple Lines in R ? In this article, we will discuss how to create a plot using ggplot2 with multiple lines in the R programming language. Method 1: Using geom_line() function In this approach to create a ggplot with multiple lines, the user need to first install and import the ggplot2 package in the R console and then
3 min read
Annotate Multiple Lines of Text to ggplot2 Plot in R In this article, we will see how to annotate Multiple Lines of text to ggplot2 Plot in R programming language. Let us first create a regular plot so that the difference is apparent, Example: R # Load Package library("ggplot2") # Create a DataFrame DF <- data.frame(X = runif(100, min=0, max=100),
2 min read
How to Combine Multiple ggplot2 Plots in R? In this article, we will discuss how to combine multiple ggplot2 plots in the R programming language. Combining multiple ggplot2 plots using '+' sign to the final plot In this method to combine multiple plots, here the user can add different types of plots or plots with different data to a single p
2 min read
Multiple Line Plots or Time Series Plots with ggplot2 in R A multiple line plot or time series plot shows how values change over time, like crime rates or sales across years. In R, we use the ggplot2 package with the geom_line() function to create these plots. By adding a third variable, we can draw separate lines for different groups, making it easy to com
3 min read
How to Add Vertical Lines By a Variable in Multiple Density Plots with ggplot2 in R In this article, we will discuss how to add vertical lines by a variable in multiple density plots with ggplot2 package in the R  Programming language. To do so first we will create multiple density plots colored by group and then add the line as a separate element. Basic Multiple Density Plot: To
3 min read