Lineplot legend + ABLINE ggplot

522 views Asked by At

I have the following ggplot and trying to add the legend and a geom_abline at median.

purple line is for 2013 prods and red is for 2014

This is what I did to generate the plot:

ggplot(prods[Year==2013,], aes(x = Date, y = Prod, group = SOM)) + 
 geom_line(lwd = 1.3, colour = "purple") +
 ylab("Actual Productivity") + theme(axis.title.x=element_blank()) +
 geom_line(data=prods[Year==2014,], aes(x = Date, y = Prod, group = SOM),lwd = 1.3, colour = "red") +
 geom_abline(data = prods,h=median(Prod))+
 scale_color_manual("Period", values = c("purple","red"), labels = c("2013","2014")) +
 facet_wrap(~ SOM)

I am not getting any error but there is no legend nor abline is popping up on the image. Plot looks like

this:

enter image description here

any help would be highly appreciated.

regards,

1

There are 1 answers

0
Shery On

As per aosmith's advice:

I did the following and was able to get the following plot:

ggplot(data=prods,aes(Date)) + 
  geom_line(data=prods[Year==2013,],aes(y=Prod, colour="red"),lwd = 1.3,) +    
  geom_line(data=prods[Year==2014,],aes(y=Prod, colour="blue"),lwd = 1.3) +   
  geom_hline(data=prods, aes(yintercept = median(Prod))) +
  scale_colour_manual(name="Period",values=c("blue","red", "black"), labels = c("2014","2013", "Median")) + 
  ylab("Actual Prod") + xlab(" ") + 
  theme(axis.title.y = element_text(size = 15, vjust=0.3)) +
  facet_wrap(~ SOM)

Plot looks like this:

enter image description here