How can I add a legend for an object I have added to my plot with stat_summary
?
Here is an example:
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_violin(trim=FALSE)
data_summary <- function(x) {
m <- mean(x)
ymin <- m-sd(x)
ymax <- m+sd(x)
return(c(y=m,ymin=ymin,ymax=ymax))
}
p + stat_summary(fun.data=data_summary)
### Code from http://www.sthda.com/english/wiki/ggplot2-violin-plot-quick-start-guide-r-software-and-data-visualization
I want to add a descriptive legend which explains what the line and the dot in the center of each violin plot represents.
According to the RELATED topic below, I am under the impression that this can be achieved by defining aes(shape="")
in stat_summary
and then adding scale_shape_manual("", values=c("?"))
. But I have not had any success.
RELATED. ggplot2 legend for stat_summary