Change color median line ggplot geom_boxplot()

前端 未结 1 1399
囚心锁ツ
囚心锁ツ 2020-12-09 20:15

I would like to change the color of the median line in geom_boxplot(). I have looked and can\'t find a way to do it. I have posted the R code here that I am usi

相关标签:
1条回答
  • 2020-12-09 20:42

    You can use the details of the plot, to derive the coordinates of where the median line is, and then add colour to it using geom_segment.

    library(ggplot2)
    
    p <- ggplot(mtcars, aes(factor(am), mpg)) + geom_boxplot()
    
    dat <- ggplot_build(p)$data[[1]]
    
    p + geom_segment(data=dat, aes(x=xmin, xend=xmax, 
                                   y=middle, yend=middle), colour="red", size=2)
    

    Also had to increase the size of the line so that it covers the original black median line

    0 讨论(0)
提交回复
热议问题