简体   繁体   中英

ggplot2 legend for stat_summary

How can I create a legend informing that the red cross is the mean?

ggplot(results, aes(x=factor, y=proportionPositive)) +
geom_boxplot() +
stat_summary(fun.data = "mean_cl_normal", colour = "red", shape=4)

在此处输入图片说明

Here is one way of doing it:

  1. Map an aesthetic to a shape, ie aes(shape="mean")
  2. Create a manual shape scale, ie scale_shape_manual()
 # Create dummy data results <- data.frame( factor=factor(rep(1:10, 100)), proportionPositive=rnorm(1000)) # Plot results ggplot(results, aes(x=factor, y=proportionPositive)) + geom_boxplot() + stat_summary(fun.data = "mean_cl_normal", aes(shape="mean"), colour = "red", geom="point") + scale_shape_manual("", values=c("mean"="x")) 

在此输入图像描述

for me option "show.legend=TRUE" simply did work out:

ggplot(aes(x=stimulus, y=EPN, fill=strategy))+ 
  stat_summary(fun.data=mean_se, show.legend=TRUE, geom="bar", position="dodge", colour="black", linetype="solid", size=0.3)

To make it appear like a default legend (borrowing from @Andrie code):

ggplot(results, aes(x=factor, y=proportionPositive)) +
      geom_boxplot() +
      stat_summary(fun.data = "mean_cl_normal", 
              aes(shape=""), # Leave empty
              colour = "red",
              geom="point") +
      scale_shape_manual("mean", values= "") # Will show mean on top of the line

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