[R] ggplot two-factor legend
Jeff Newmiller
jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Thu Jul 18 18:12:54 CEST 2024
If I follow your question, you want redundant aesthetics. Ggplot normally notices correlated aesthetic mapping variables and merges the legends, so the most likely answer is that your data are not fully correlated in all rows. I have also seen this where data are drawn from different dataframes for different layers since it is hard to merge factors, but I don't see that here.
You are using the group parameter... try removing that? The group parameter overrides the automatic group determination. There might be a syntax for specifying correlated grouping, but I don't know it... I normally just verify that my data meets the requirements to be automatically identified as correlated if that is my goal, since that is a prerequisite anyway.
On July 18, 2024 8:27:05 AM PDT, "SIBYLLE STÖCKLI via R-help" <r-help using r-project.org> wrote:
>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
>
>______________________________________________
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
--
Sent from my phone. Please excuse my brevity.
More information about the R-help
mailing list