Search code examples
rggplot2violin-plot

Add legend for a custom stat_summary fun.data


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

enter image description here

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


Solution

  • It sounds like you have the gist of how this works, mapping a constant to some aesthetic and then using scale_*_manual() to clean the legend up.

    In scale_shape_manual() I think remove the legend name, and add a second box to the legend by changing the limits. I used c("Mean", "1 SD") but these can be whatever you want.

    The number of shapes needed is dictated by the number of legend boxes so I give two to values, using NA for the second since the second box in the legend should be a line with no point.

    Finally, I use override.aes() in guide_legend() to remove the line from the first box.

    p + stat_summary(fun.data=data_summary, aes(shape = "Mean")) +
         scale_shape_manual(name = NULL, 
                            limits = c("Mean", "1 SD"),
                            values = c(19, NA),
                            guide = guide_legend(override.aes = list(linetype = c(0, 1))))
    

    enter image description here