Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

r - Change color median line ggplot geom_boxplot()

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 using but I just really need a reference to how to change the color.

ggplot(invitro2) +
  geom_boxplot(aes(x = reorder(CANCER_TYPE,tmedian), y = GeoMedian_IC50)) +
  xlab("") +  
  geom_point(aes(x = reorder(CANCER_TYPE,tmedian), y = GeoMedian_IC50)) +
  theme_bw() +
  scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x))) +
  annotation_logticks(sides="l")   +  
  theme(axis.text.x=element_text(angle=45,size=10,hjust=1),
        panel.grid.major = element_blank()) 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...