Anthony
Anthony

Reputation: 49

ggplot line effects cancelled by another line

library(ggplot2)
library(tidyr)

year1 = c(5,6,4,5)
year2 = c(6,5,5,6)
year3 = c(3,4,3,4)
date = c("a", "b", "c", "d")
data = data.frame(date, year1, year2, year3)
data2 = gather(data , "year", "temp", -date)

I'm having an issue with ggplot. I have labels and aesthetics for 3 lines that I want to change like this :

ggplot(data2, aes(x=date, y=temp, group= year, color= year))+ 
  geom_line()+
  scale_color_manual(values = c("darkred", "darkgreen", "darkblue"))+
  scale_color_discrete(name = "Années", labels = c("2004", "2005", "2006"))

But when I add scale_color_discrete(), it cancels the previous line and sets the colors by default. Do you have any idea why, and how to solve the problem?

Upvotes: 1

Views: 30

Answers (1)

TarJae
TarJae

Reputation: 78917

You don't need scale_color_discrete

ggplot(data2, aes(x=date, y=temp, group= year, color= year))+ 
  geom_line()+
  scale_color_manual(name = "Années", values = c("darkred", "darkgreen", "darkblue"), labels = c("2004", "2005", "2006"))

enter image description here

Upvotes: 1

Related Questions