Open In App

How to change Colors in ggplot2 Line Plot in R ?

Improve
Improve
Like Article
Like
Save
Share
Report

A line graph is a chart that is used to display information in the form of series of data points. It utilizes points and lines to represent change over time. Line graphs are drawn by plotting different points on their X coordinates and Y coordinates, then by joining them together through a line from beginning to end. However, sometimes it becomes a necessity to change the colors of the lines as there may be more than one line in a single graph. In this article, we will see how to change the color of the line chart in R Programming Language.

Let us look at one example to depict what the color of the line graph is by default.

Example:

R




library("ggplot2")
 
year<-c(2000,2001,2002,2003,2004)
winner<-c('A','B','B','A','B')
score<-c(9,7,9,8,8)
 
df<-data.frame(year,winner,score)
 
ggplot(df,aes(x=year,y=score,group=winner))+
geom_line()+geom_point()


 Output:

The color of the line graph can be changed in various ways. For this simply value for color attribute as the name of the column on which the values will be distinguished. With reference to this column, different colors will be assigned to values by default.
 

Example:

R




library("ggplot2")
 
year<-c(2000,2001,2002,2003,2004)
winner<-c('A','B','B','A','B')
score<-c(9,7,9,8,8)
 
df<-data.frame(year,winner,score)
 
ggplot(df,aes(x=year,y=score,group=winner))+
geom_line(aes(color=winner))+geom_point()


Output:

A custom color palette can also be used to differentiate among different line graphs. For this scale_color_manual() function is used to which a list of color values is passed.

Syntax:

scale_color_manual(values=c(color1, color2,  …. , color n))

Example:

R




library("ggplot2")
 
year<-c(2000,2001,2002,2003,2004)
winner<-c('A','B','B','A','B')
score<-c(9,7,9,8,8)
 
df<-data.frame(year,winner,score)
 
ggplot(df,aes(x=year,y=score,group=winner))+
geom_line(aes(color=winner))+geom_point()+
scale_color_manual(values=c('Green','Yellow'))


Output:
 

Custom colors can also be passed through brewer color palette, for that add scale_color_brewer() function with appropriate name of the palette to be used.

Syntax:

scale_color_brewer(palette=palette_name)

Example:

R




library("ggplot2")
 
year<-c(2000,2001,2002,2003,2004)
winner<-c('A','B','B','A','B')
score<-c(9,7,9,8,8)
 
df<-data.frame(year,winner,score)
 
ggplot(df,aes(x=year,y=score,group=winner))+
geom_line(aes(color=winner))+geom_point()+
scale_color_brewer(palette="Accent")


 Output:

A grayscale can also be used to give different colors to lines. For this scale_color_grey() function is used.
 

Example:

R




library("ggplot2")
 
year<-c(2000,2001,2002,2003,2004)
winner<-c('A','B','B','A','B')
score<-c(9,7,9,8,8)
 
df<-data.frame(year,winner,score)
 
ggplot(df,aes(x=year,y=score,group=winner))+
geom_line(aes(color=winner))+
geom_point()+scale_color_grey()


Output:



Last Updated : 29 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads