1
votes

I am using GGPLOT2 to make a forest plot and since I have added in a line of code to change one of the points shape to a diamond, I am getting a double legend ( pic below )

Forest plot image

Anyone know how I can keep the colour coded legend, but with the diamond shape from the black legend? And get rid of the black legend!

p2 = ggplot(data=Forestplot,
           aes(x = Group,y = RiskRatio, ymin = LowerLimit, 
               ymax = UpperLimit, shape = Group ))+
  geom_pointrange(aes(col=Group, shape = Group))+
  scale_shape_manual(values = c(5, 20, 20, 20, 20)) +
  geom_hline(aes(fill=Group),yintercept =0, linetype=2)+
  xlab('Trait')+ ylab("Effect Size (95% Confidence Interval)")+
  geom_errorbar(aes(ymin=LowerLimit, ymax=UpperLimit,col=Group),width=0.5,cex=1)+ 
  facet_wrap(~Condition,strip.position="left",nrow=9,scales = "free_y") +
  theme(plot.title=element_text(size=16,face="bold"),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        axis.text.x=element_text(face="bold"),
        axis.title=element_text(size=12,face="bold"),
        strip.text.y = element_text(hjust=0,vjust = 1,angle=180,face="bold"))+
  coord_flip() + guides(col = guide_legend(reverse = TRUE))
p2

This is my first time posting, please let me know if there is more information needed.

1
You need to use identical names and labels for each legend: this post applies. Welcome to SO, but please check to see if your question has been asked before.Limey
Thanks @Limey, I tried fixing it with the answers from this question but it didn't work.Rebecca Raynal

1 Answers

1
votes

You need to apply the reverse order to both color and shape aesthetics. Check the exemple below:

library(ggplot2)

ggplot(iris) +
  aes(Sepal.Length, Sepal.Width, color = Species, shape = Species) +
  geom_point() + 
  guides(color = guide_legend(reverse = TRUE))

ggplot(iris) +
  aes(Sepal.Length, Sepal.Width, color = Species, shape = Species) +
  geom_point() + 
  guides(color = guide_legend(reverse = TRUE), shape = guide_legend(reverse = TRUE))