如何在 ggplot2 中合并图例? (保持形状、颜色和标签)
How to merge legends in ggplot2? (keep shape, color and label)
我找不到合并两个图例的方法:
ggplot(datapoidsmono, aes(x = time, y = weight)) +
stat_summary(aes(color = group), fun.data="mean_sdl", fun.args = list(mult=1), geom="errorbar", position = "identity", size=0.5, width=0.2) +
stat_summary(fun.y = "mean", geom = "point", size=3, aes(shape=group,colour=group)) +
scale_x_discrete(name = "Days after injection") +
scale_y_continuous(name = "Weight (g)", limits=c(0, 4000), breaks = seq(0, 4000,500)) +
theme(axis.line.x = element_line(size = 0.5, colour = "black"),axis.text.x = element_text(colour="black", size = 12),axis.line.y = element_line(size = 0.5, colour = "black"),axis.text.y = element_text(colour="black", size = 12),axis.title = element_text(size =15, face="bold"),plot.title = element_text(size =20, face = "bold"),panel.grid.major = element_line(colour = "#F1F1F1"),panel.grid.minor = element_blank(), panel.background = element_blank()) +
scale_color_manual(values=c("green", "blue", "red"), name="Treatment", labels=c("A","B","C")) +
ggtitle("Weight variation over time") + theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position = "right") +
theme(legend.background = element_rect(size=0.5, linetype="solid", colour ="black", fill="white"))
我只想保留一个图例,合并颜色、形状和标签。
我一直在苦苦挣扎,但 none 这些解决方案对我有用:
guides(fill = guide_legend(title = "Treatment", title.position = "left"))
scale_colour_discrete(name ="Treatment", breaks=c("A", "B", "C"), labels=c("A", "B", "C"))
scale_shape_discrete(name ="Treatment", breaks=c("A", "B", "C"), labels=c("A", "B", "C"))
scale_shape_manual(name ="Treatment", breaks=c("A", "B", "C"), labels=c("A", "B", "C"))
labs(fill = "Treatment")
labs(names=c("A", "B", "C"))
scale_fill_discrete(name="Treatment", labels = c("A", "B", "C"))
scale_fill_manual(name="Treatment", labels=c("A", "B", "C"))
我的数据框是:
> dput(head(datapoidsmono, 40))
structure(list(weight = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 17,
19, 45, 51, 57, 90, 102, 114, 20, 22, 25, 60, 66, 75, 120, 132,
150, 30, 34, 36, 90, 102, 108, 180, 204, 216, 40, 50, 47, 120
), group = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L), .Label = c("1",
"2", "3"), class = "factor"), time = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L), .Label = c("1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12", "13", "14", "15"), class = "factor")), row.names = c(NA,
-40L), class = c("tbl_df", "tbl", "data.frame"))
感谢任何建议!
感谢 Ronak Shah,我找到了这个:the R cook book
If you use both colour and shape, they both need to be given scale
specifications. Otherwise there will be two two separate legends.
这给了我这个:
ggplot(datapoidsmono, aes(x = time, y = weight)) +
stat_summary(aes(color = group), fun.data="mean_sdl", fun.args = list(mult=1), geom="errorbar", position = "identity", size=0.5, width=0.2) +
stat_summary(fun.y = "mean", geom = "point", size=3, aes(shape=group,color=group)) +
scale_x_discrete(name = "Days after injection") +
scale_y_continuous(name = "Weight (g)", limits=c(0, 4000), breaks = seq(0, 4000,500)) +
theme(axis.line.x = element_line(size = 0.5, color = "black"),axis.text.x = element_text(color="black", size = 12),axis.line.y = element_line(size = 0.5, color = "black"),axis.text.y = element_text(color="black", size = 12),axis.title = element_text(size =15, face="bold"),plot.title = element_text(size =20, face = "bold"),panel.grid.major = element_line(color = "#F1F1F1"),panel.grid.minor = element_blank(), panel.background = element_blank()) +
scale_color_manual(values=c("green", "blue", "red"), name="Treatment", labels=c("A","B","C")) +
scale_shape_manual(values=c(15,16,17), name ="Treatment", labels=c("A", "B", "C")) +
ggtitle("Weight variation over time") + theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position = "right") + #on peut choisir l'endroit précis en remplaçan par c(0.15,0.80)) ou supprimer la légende en remplaçant par none
theme(legend.background = element_rect(size=0.5, linetype="solid", color ="black", fill="white"))
我找不到合并两个图例的方法:
ggplot(datapoidsmono, aes(x = time, y = weight)) +
stat_summary(aes(color = group), fun.data="mean_sdl", fun.args = list(mult=1), geom="errorbar", position = "identity", size=0.5, width=0.2) +
stat_summary(fun.y = "mean", geom = "point", size=3, aes(shape=group,colour=group)) +
scale_x_discrete(name = "Days after injection") +
scale_y_continuous(name = "Weight (g)", limits=c(0, 4000), breaks = seq(0, 4000,500)) +
theme(axis.line.x = element_line(size = 0.5, colour = "black"),axis.text.x = element_text(colour="black", size = 12),axis.line.y = element_line(size = 0.5, colour = "black"),axis.text.y = element_text(colour="black", size = 12),axis.title = element_text(size =15, face="bold"),plot.title = element_text(size =20, face = "bold"),panel.grid.major = element_line(colour = "#F1F1F1"),panel.grid.minor = element_blank(), panel.background = element_blank()) +
scale_color_manual(values=c("green", "blue", "red"), name="Treatment", labels=c("A","B","C")) +
ggtitle("Weight variation over time") + theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position = "right") +
theme(legend.background = element_rect(size=0.5, linetype="solid", colour ="black", fill="white"))
我只想保留一个图例,合并颜色、形状和标签。 我一直在苦苦挣扎,但 none 这些解决方案对我有用:
guides(fill = guide_legend(title = "Treatment", title.position = "left"))
scale_colour_discrete(name ="Treatment", breaks=c("A", "B", "C"), labels=c("A", "B", "C"))
scale_shape_discrete(name ="Treatment", breaks=c("A", "B", "C"), labels=c("A", "B", "C"))
scale_shape_manual(name ="Treatment", breaks=c("A", "B", "C"), labels=c("A", "B", "C"))
labs(fill = "Treatment")
labs(names=c("A", "B", "C"))
scale_fill_discrete(name="Treatment", labels = c("A", "B", "C"))
scale_fill_manual(name="Treatment", labels=c("A", "B", "C"))
我的数据框是:
> dput(head(datapoidsmono, 40))
structure(list(weight = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 17,
19, 45, 51, 57, 90, 102, 114, 20, 22, 25, 60, 66, 75, 120, 132,
150, 30, 34, 36, 90, 102, 108, 180, 204, 216, 40, 50, 47, 120
), group = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L), .Label = c("1",
"2", "3"), class = "factor"), time = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L), .Label = c("1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12", "13", "14", "15"), class = "factor")), row.names = c(NA,
-40L), class = c("tbl_df", "tbl", "data.frame"))
感谢任何建议!
感谢 Ronak Shah,我找到了这个:the R cook book
If you use both colour and shape, they both need to be given scale specifications. Otherwise there will be two two separate legends.
这给了我这个:
ggplot(datapoidsmono, aes(x = time, y = weight)) +
stat_summary(aes(color = group), fun.data="mean_sdl", fun.args = list(mult=1), geom="errorbar", position = "identity", size=0.5, width=0.2) +
stat_summary(fun.y = "mean", geom = "point", size=3, aes(shape=group,color=group)) +
scale_x_discrete(name = "Days after injection") +
scale_y_continuous(name = "Weight (g)", limits=c(0, 4000), breaks = seq(0, 4000,500)) +
theme(axis.line.x = element_line(size = 0.5, color = "black"),axis.text.x = element_text(color="black", size = 12),axis.line.y = element_line(size = 0.5, color = "black"),axis.text.y = element_text(color="black", size = 12),axis.title = element_text(size =15, face="bold"),plot.title = element_text(size =20, face = "bold"),panel.grid.major = element_line(color = "#F1F1F1"),panel.grid.minor = element_blank(), panel.background = element_blank()) +
scale_color_manual(values=c("green", "blue", "red"), name="Treatment", labels=c("A","B","C")) +
scale_shape_manual(values=c(15,16,17), name ="Treatment", labels=c("A", "B", "C")) +
ggtitle("Weight variation over time") + theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position = "right") + #on peut choisir l'endroit précis en remplaçan par c(0.15,0.80)) ou supprimer la légende en remplaçant par none
theme(legend.background = element_rect(size=0.5, linetype="solid", color ="black", fill="white"))