Search code examples
rggplot2boxplotlegend-properties

Prevent inclusion of mean in boxplot legend


In ggplot2 boxplot with added mean, is there a way to prevent the mean from being included with the legend? I must use a large point size and find its inclusion in the legend distracting. The conceptually closest problem I could find, for removing the slash from the legend of outlined bar charts, is at

http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/#modifying-the-legend-box

That solution uses geom_bar twice to overlay one plot on another, the second, outlined bar chart, without a legend. But is there a solution for preventing the mean from appearing in the boxplot legend?

ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
stat_summary(fun.y=mean, colour="darkred", geom="point", shape=18, size=3) + 
#  idea from above website
geom_boxplot(show_guide=FALSE)

Solution

  • You can add argument show_guide=FALSE inside the stat_summary() call to prevent placing of points inside the legend.

    ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
      stat_summary(fun.y=mean, colour="darkred", geom="point", 
                             shape=18, size=3,show_guide = FALSE)
    

    enter image description here