Question

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)
Était-ce utile?

La 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

Autres conseils

If you want to remove the shape from the boxplot legend and add it as a new legend to the plot then you can use:

ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group))+ 
geom_boxplot() + 
stat_summary(fun.y=mean, aes(colour="mean"), geom="point", shape=18, size=3)+
scale_color_manual(name="",values = "darkred")+
scale_fill_nejm(name="Group",guide=guide_legend(override.aes = list(shape="")))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top