简体   繁体   中英

Change stat_summary colours based on group and add text to the label in ggplot2 boxplot

Following this question: Add number of observations per group in ggplot2 boxplot , how do I change the colours of stat_summary?

Below is the example code:

give.n <- function(x){
      return(c(y = median(x)*1.05, label = length(x))) 
    }    


p <- ggplot(mtcars, aes(factor(vs), mpg, colour = factor(am))) + 
      geom_boxplot() +
      stat_summary(fun.data = give.n, geom = "text", fun.y = median,
                   position = position_dodge(width = 0.75))
p

So when I do the below to change the colours, it doesn't work (it gives me only 1 combined number)

stat_summary(fun.data = give.n, geom = "text", fun.y = median,
                   position = position_dodge(width = 0.75), colour =   c("black", "red"))

And when I do the below to add the text "n=" it doesn't work either. (I try to add the "n=" in the function itself, by doing the below:

give.n <- function(x){
      return(c(y = median(x)*1.05, label = paste0("n=",length(x)))) 
    } 

But I get the below error:

 Error: Discrete value supplied to continuous scale

For the colours, you want to add these using scale_colour_manual , so plot call looks like:

p <- 
    ggplot(mtcars, aes(factor(vs), mpg, colour = factor(am))) + 
    geom_boxplot() +
    stat_summary(fun.data = give.n, geom = "text", fun.y = median,
               position = position_dodge(width = 0.75)) +
    scale_colour_manual(values = c("black", "red"))

The answer to adding "n=" is a duplicate of this question: Use stat_summary to annotate plot with number of observations . You need to use data.frame(...) in your give.n function rather than c(...) :

give.n <- 
    function(x){
      return(data.frame(y = median(x)*1.05, label = paste0("n=",length(x)))) 
    } 

EDIT: Re comment on changing colours for stat_summary items only, this proved a bit tricky in that I don't think you can have multiple scale_colour_manual layers. However, in this case you can make use of the fill aesthetic for box plots and leave the colour aesthetic for your text geom. To make it cleaner, I've taken the colour and fill aesthetics out of the ggplot(...) call and put these in each geom:

p <- 
    ggplot(mtcars, aes(factor(vs), mpg)) + 
    geom_boxplot(aes(fill = factor(am))) +
    stat_summary(aes(colour = factor(am)), fun.data = give.n, 
                 geom = "text", fun.y = median, position = position_dodge(width = 0.75)) +
    scale_colour_manual(values = c("black", "red"))

Then if you want to specify colours for the box plot fill you can use scale_fill_manual(...)

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