- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change the color of lines for a line chart using ggplot2 in R?
When we create line chart with each of the lines having different color, we might want to change the color of lines if the colors we used at the first time are not making the chart attractive. This can be done by manually setting the color of the lines in the chart with the help of scale_color_manual function.
Example
Consider the below data frame −
> set.seed(2) > Group<-rep(c(1:5),times=4) > Time<-rep(c("Time1","Time2"),each=) > Frequency<-rpois(20,5) > df<-data.frame(Group,Time,Frequency) > df Group Time Frequency 1 1 Time1 3 2 2 Time2 6 3 3 Time1 5 4 4 Time2 3 5 5 Time1 9 6 1 Time2 9 7 2 Time1 3 8 3 Time2 7 9 4 Time1 5 10 5 Time2 5 11 1 Time1 5 12 2 Time2 3 13 3 Time1 6 14 4 Time2 3 15 5 Time1 4 16 1 Time2 7 17 2 Time1 10 18 3 Time2 3 19 4 Time1 5 20 5 Time2 2 > library(ggplot2) > ggplot(df,aes(x=Group,y=Frequency,colour=Time))+ + geom_line()
Output
Changing the color of the lines to Green and Black −
> ggplot(df,aes(x=Group,y=Frequency,colour=Time))+ + geom_line()+ + scale_color_manual(values=c("Green","Black"))
Output
- Related Articles
- How to create line chart for categories with grey color palette using ggplot2 in R?
- How to change the color of points for ggplot2 scatterplot using color brewer in R?
- Change the color of X-axis line for a graph using ggplot2.
- How to create a line chart using ggplot2 with a vertical line in R?
- How to create a line chart for a subset of a data frame using ggplot2 in R?
- Change the outline color for histogram bars using ggplot2 in R.
- How change the color of facet title using ggplot2 in R?
- How to change the color of points in a scatterplot using ggplot2 in R?
- How to display count on Y-axis for line chart using ggplot2 in R?
- How to create the bar chart with ggplot2 using color brewer in R?
- How to join points in a point chart with lines using ggplot2 in R?
- How to change the color of X-axis label using ggplot2 in R?
- How to create a line chart using ggplot2 that touches the edge in R?
- How to change the width of whisker lines in a boxplot using ggplot2 in R?
- How to change the color of bars of a bar plot using ggplot2 in R?

Advertisements