r - 更改颜色中线 ggplot geom_boxplot()

标签 r ggplot2

我想改变 geom_boxplot() 中线的颜色.我已经看过了,但找不到办法。我已经在这里发布了我正在使用的 R 代码,但我真的需要一个关于如何更改颜色的引用。

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

最佳答案

您可以使用绘图的细节,推导出中线所在位置的坐标,然后使用 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)
还得加size的线,以便它覆盖原来的黑色中位数
线
enter image description here

关于r - 更改颜色中线 ggplot geom_boxplot(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30002257/

相关文章:

r - 使用plotly更改ggplot中的轴标题位置

从 ggplot2 中删除顶部和右侧边框

r - ggplot2:geom_point() 闪避形状但不是颜色

c - Valgrind 报告 C 代码读取错误

r - 向下复制列表元素以填充 NA 列表元素

r - ggplot : Using strip. text.x (element_text) 用于仅制作刻面轴标签 "bold"的一个元素

r - 在within()中使用向量和R中的替换函数(还涉及memisc)。

在 Tableau 中复制 R cor() 函数

r - 如何使用 ggplot2 在情节的不同侧(底部和右侧)放置图例?

r - geom_text 如何根据需要在栏上定位文本?