Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coloring a line plot based on a third factor in ggplot

Tags:

r

ggplot2

I am having a hard time with a coloring scheme in ggplot. If someone could help me out or send me to another question that would be fantastic.

I have data that look along the lines of

day=rep(1:10, 5)
year=rep(1992:1996, each=10)
state=rep(c("A","B"), each=25)
set.seed(4)
y=runif(50, 5.0, 7.5)
df=data.frame(year,day,state,y)
> head(df)
  year day state        y
1 1992   1     A 6.464501
2 1992   2     A 5.022364
3 1992   3     A 5.734349
4 1992   4     A 5.693437
5 1992   5     A 7.033936
6 1992   6     A 5.651069

I want to create a plot similar to the below. Using the code:

library(ggplot2)
p = ggplot(df, aes(day, y))
p = p + geom_line(aes(colour = factor(year)))
print(p)

Line Plot

I want the coloring to be based off of the state variable. I would like the years that are in state 'A' to be one color and the years in state 'B' to be another.

Thank you

like image 443
Chloee Robertson Avatar asked May 06 '15 16:05

Chloee Robertson


1 Answers

If you want it separated by years but colored by state the key is to use the group= argument:

ggplot(data=df, aes(x=day, y=y, group=year, colour=state)) +
  geom_line() +
  geom_point()
like image 157
Forrest R. Stevens Avatar answered Oct 23 '22 20:10

Forrest R. Stevens