How to change Colors in ggplot2 Line Plot in R ?
Last Updated :
29 Jul, 2021
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:
Similar Reads
Change Theme Color in ggplot2 Plot in R A theme in ggplot2 is a collection of settings that control the non-data elements of the plot. These settings include things like background colors, grid lines, axis labels, and text sizes. we can use various theme-related functions to customize the appearance of your plots, including changing theme
4 min read
How to change line width in ggplot2? ggplot2 is a data visualization package for the statistical programming language R. This article discusses how can we change line width in ggplot2. For this, only the size parameter in the geom_line() function has to be initialized to the required value. Syntax: geom_line(mapping=NULL, data=NULL, st
1 min read
How to annotate a plot in ggplot2 in R ? In this article, we will discuss how to annotate functions in R Programming Language in ggplot2 and also read the use cases of annotate. What is annotate?An annotate function in R can help the readability of a plot. It allows adding text to a plot or highlighting a specific portion of the curve. Th
4 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
How To Change facet_wrap() Box Color in ggplot2 in R? In this article, we will discuss how to change facet_wrap() box color in ggplot2 in R Programming language. Facet plots, where one subsets the data based on a categorical variable and makes a series of similar plots with the same scale. Facetting helps us to show the relationship between more than t
3 min read