更改颜色中线ggplot geom_boxplot() [英] Change color median line ggplot geom_boxplot()

查看:949
本文介绍了更改颜色中线ggplot geom_boxplot()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 geom_boxplot()中更改中线的颜色。我看了,也找不到办法做到这一点。我已经在这里发布了R代码,但我真的需要参考如何更改颜色。

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


解决方案

您可以使用图的详细信息来导出中线的坐标,然后使用 geom_segment 添加颜色。 (mgclot,aes(因子(am),mpg)(b)b

$ p $ library(ggplot2)

p < - ggplot )+ geom_boxplot()

dat < - ggplot_build(p)$ data [[1]]

p + geom_segment(data = dat,aes(x = xmin,xend = xmax,
y = middle ,yend = middle),color =red,size = 2)

还必须增加 size 这一行,以便它覆盖原来的黑色中位数


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()) 

解决方案

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

这篇关于更改颜色中线ggplot geom_boxplot()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆