如何在ggplot2中合并图例? (保留形状,颜色和标签)

时间:2019-06-08 14:26:50

标签: r ggplot2 legend legend-properties

我找不到合并我得到的两个图例的方法:

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"))

How to merge these legends

我只想保留一个图例,将颜色,形状和标签合并。 我一直在苦苦挣扎,但这些解决方案都不适合我:

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"))

感谢您的任何建议!

2 个答案:

答案 0 :(得分:1)

感谢罗纳克·沙(Ronak Shah),我发现了这一点:the R cook book

  

如果您同时使用颜色和形状,则都需要指定比例尺   规格。否则将有两个两个单独的图例。

这是给我的:

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"))

enter image description here

答案 1 :(得分:0)

这是一个解决方案:

df <- data.frame(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 = factor(c(1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 
                       1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 3, 
                       3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 2), levels= c("1","2", "3")),
                 time = factor(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 
                                           3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 
                                           4, 5, 5, 5, 5), levels = c("1", "2", "3", "4", "5", "6", "7", "8", "9",
                                                                      "10", "11", "12", "13", "14", "15")))

ggplot(df, 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, 400), breaks = seq(0, 400, 50)) +
  scale_color_manual(values=c("green", "blue", "red"), name="Treatment", labels=c("A","B","C")) +
  scale_shape_manual(values=c(1, 2, 3), 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")) +
  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())

灵感来自:

Combine legends for color and shape into a single legend