[R] ggplot two-factor legend

sibyiie@stoeckii m@iii@g oii gmx@ch sibyiie@stoeckii m@iii@g oii gmx@ch
Thu Jul 18 17:27:05 CEST 2024


Hi

I am using ggplot to visualise y for a two-factorial group (Bio: 0 and 1) x
= 6 years. I was able to adapt the colour of the lines (green and red) and
the linetype (solid and dashed).
Challenge: my code produces now two legends. One with the colors for the
group and one with the linetype for the group. Does somebody have a hint how
to adapt the code to produce one legend? Group 0 = red and dashed, Group 1 =
green and solid?


MS1<- MS %>% filter(QI_A!="NA") %>% droplevels()
dev.new(width=4, height=2.75)
par(mar = c(0,6,0,0))
p1<-ggplot(data = MS1, aes(x= Jahr, y= QI_A,group=Bio,color=Bio,
linetype=Bio)) + 
    	geom_smooth(aes(fill=Bio) , method = "lm" , formula = y ~ x +
I(x^2),linewidth=1) +
	theme(panel.background = element_blank())+
	theme(axis.line = element_line(colour = "black"))+
  theme(axis.text=element_text(size=18))+
  theme(axis.title=element_text(size=20))+
	ylab("Anteil BFF an LN [%]") +xlab("Jahr")+
	scale_color_manual(values=c("red","dark green"), labels=c("ÖLN",
"BIO"))+
	scale_fill_manual(values=c("red","dark green"), labels= c("ÖLN",
"BIO"))+
	theme(legend.title = element_blank())+
  theme(legend.text=element_text(size=20))+
  scale_linetype_manual(values=c("dashed", "solid"))
p1<-p1 + expand_limits(y=c(0, 30))

kind regards
Sibylle



More information about the R-help mailing list