Reputation: 27
I would like to add a legend specifying the difference between the two linetype I've used. I'm pretty sure it's something very basic to do but I cant make it work despite trying different things...
You'll find below an reprex of my code.
x<-c(1:10)
y<-c(runif(10,min=50,max=100))
z<-c(runif(10,min=50,max=100))
group=c("A","A","B","C","B","C","C","B","A","B")
session=c(rep("S1",5),rep("S2",5))
test<-data.frame(x=x, Serie1=y,Serie2=z,group=group,session=session)
library(ggplot2)
ggplot(test)+
geom_line(aes(x=x,y=Serie1,group=group,col=group,pch=group),linetype=1)+
geom_line(aes(x=x,y=Serie2,group=group,col=group),linetype=2)+
labs(x="x axis",y="yaxis")+
facet_grid(.~session)
#> Warning: Ignoring unknown aesthetics: shape
Does anyone have an idea on how I could add a legend for the linetype to specify that serie1 is represented with the solid line and serie2 with the dashed line?
Also, does anyone know why there is the following warning when I run the previous code : "Warning: Ignoring unknown aesthetics: shape" ?
Thank you all for your help!
Upvotes: 0
Views: 648
Reputation: 206401
If you want a legend for something, it needs to be inside the aes()
. Move linetype=
into the aes()
and you can give the lines names when you so that. Also the error message about shape was coming from the pch=
which isn't a valid parameter for lines.
library(ggplot2)
ggplot(test)+
geom_line(aes(x=x,y=Serie1,group=group,col=group,linetype="series1"))+
geom_line(aes(x=x,y=Serie2,group=group,col=group,linetype="series2"))+
labs(x="x axis",y="yaxis")+
facet_grid(.~session)
Upvotes: 2