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
ggplot two-factor legend
6 messages · sibyiie@stoeckii m@iii@g oii gmx@ch, Jeff Newmiller, Rui Barradas
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 at 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 at 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.
Thanks Jeff I removed the group parameter in the fp1<-ggplot () line. It doesn't change anything. I suppose I got two legends as in the ggplot () line I have color=Bio & linetype=Bio. However, when removing linetype = Bio I just geht red and green. For black and white printing I would like the additionally differentiate the two lines (groups) in the linetype. Sibylle -----Original Message----- From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us> Sent: Thursday, July 18, 2024 6:13 PM To: sibylle.stoeckli at gmx.ch; SIBYLLE ST?CKLI via R-help <r-help at r-project.org>; r-help at r-project.org Subject: Re: [R] ggplot two-factor legend 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 at 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 at 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.
?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 at 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
Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
?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 at 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
Thanks a lot Rui and Jeff Yes including labels=c() in scale_linetype_manual() was the hint. Sibylle -----Original Message----- From: Rui Barradas <ruipbarradas at sapo.pt> Sent: Thursday, July 18, 2024 6:50 PM To: sibylle.stoeckli at gmx.ch; r-help at r-project.org Subject: Re: [R] ggplot two-factor legend ?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 at 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