[R] ggplot two-factor legend

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Jul 18 18:50:12 CEST 2024


Às 17:43 de 18/07/2024, Rui Barradas escreveu:
> Às 16:27 de 18/07/2024, SIBYLLE STÖCKLI via R-help escreveu:
>> 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.
> Hello,
> 
> To have one legend only, the labels must be the same. Try using
> 
> labels=c("ÖLN", "BIO")
> 
> in
> 
> scale_linetype_manual(values=c("dashed", "solid"), labels=c("ÖLN", "BIO"))
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> 
Hello,

Here is a more complete an answer with the built-in data set mtcars.
Note that the group aesthetic is not used. This is because linetype is 
categorical (after mutate) and there's no need to group again by the 
same variable (am).

Remove labels from scale_linetype_manual and there are two legends but 
with the same labels the legends merge.


library(ggplot2)
library(dplyr)

mtcars %>%
   # linetype must be categorical
   mutate(am = factor(am)) %>%
   ggplot(aes(hp, disp, color = am, linetype = am)) +
   geom_line() +
   scale_color_manual(
     values = c("red","dark green"),
     labels = c("ÖLN", "BIO")
   ) +
   scale_linetype_manual(
     values = c("dashed", "solid"),
     labels = c("ÖLN", "BIO")
   ) +
   theme_bw()


Hope this helps,

Rui Barradas



-- 
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença de vírus.
www.avg.com



More information about the R-help mailing list