0

I can't find a way to merge the two legends that I get with:

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

I would like to keep only one legend, with color, shape and label merged. I have been struggling around but none of these solutions worked for me:

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

My dataframe is:

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

Thanks for any suggestion!

3

1 Answer 1

3

Thanks to Ronak Shah, I found this: 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.

Which gave me this:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.