简体   繁体   中英

Removing stat_summary symbols from legend categories in ggplot2

I have the following code that has created two violin plots:

ggplot(both,aes(x=levelsname,y=loginc,fill=levelsname))+
geom_violin() +
  stat_summary(fun.y = mean,
               aes(shape="Mean"),
               colour='black',
               geom="point",
               size=3)+
    scale_shape_manual("Summary Statistics", values=c("Mean"="+"))+  
  scale_fill_manual(values=c('gray70','orange','red'))+
  scale_x_discrete(name="Site Category")+
  scale_y_continuous(name = "Log(Incidence/100,000")+
  guides(fill=guide_legend(title = "Site Category"))+
  facet_grid(~ANA)+
  theme_classic()+
  theme(axis.text.x=element_blank())

小提琴情节

Everything is correct for these plots apart from the legend. I am attempting to remove the black circles from the legend under site category and replace them with the + symbol. I also would like to move the + and mean legend symbol underneath the site category legend items, such that it looks like one legend.

You shouldn't have shape="Mean" within the aes call. It is not an aesthetic mapping! Having it in the aes makes ggplot think you are setting shape to be mapped to a character variable that always takes the value "mean" . Hence the weird legend.

You can just use shape="+" as an argument in the stat_summary call to get the effect you want. You'll probably have to take out the scale_shape_manual("Summary Statistics", values=c("Mean"="+")) line as well, because there is no longer a shape scale.

To answer the last part of your question, if you want to have a separate "mean" line for your legend you can add an extra level "Mean" to the variable mapped to the fill aesthetic (then manually set its fill to transparent). See below:

d <- data.frame(x=factor(c(1,2)), y=rnorm(100))

ggplot(d, aes(x,y, group=x, fill=x)) + 
   geom_violin() + 
   stat_summary(shape="+", fun="mean", aes(fill="Mean"), geom="point", size=3) +
   scale_fill_manual(values=c("blue", "red", "#00000000"), limits=c(1,2,"Mean"))

在此处输入图片说明

Edit: I found a way to get rid of the box around the + in the Mean line of the legend but it's a horrible hack. You need two stat_summary layers, one with color set to transparent with an aesthetic mapping (so that the legend box is transparent, but this makes the legend "+" transparent also) and then second with color="black" directly which replaces the "+" in the legend but not the box.

ggplot(d, aes(x,y, group=x, fill=x, color=x)) + 
   geom_violin() + 
   stat_summary(shape="+", fun="mean", aes(fill="Mean",color="Mean"), geom="point", size=3)+
   stat_summary(shape="+", fun="mean",color="black", geom="point", size=3) +
   theme_classic() + 
   scale_fill_manual(values=c("lightblue", "red", "#00000000"), limits=c(1,2,"Mean"))+  
   scale_color_manual(values=c("black", "black", "#00000000"), limits=c(1,2,"Mean"))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM