更改 ggplot2 boxplot 中胡须的颜色

问题描述 投票:0回答:1

我正在使用 ggplot2 制作箱线图。我可以创建盒子,但我想更改其中胡须的颜色。本质上,我希望胡须与盒子的颜色相匹配,但保持中线为黑色。我正在努力解决如何做到这一点,下面是我的代码和输出图。

ggplot(mirtras, aes(x=variable, y=value, fill=Cond)) +
geom_boxplot() +
theme_classic() +
theme_bw() +
theme(plot.title= element_text(hjust=0.5, face= "bold"),
        plot.margin = margin(1,3,1,3, "cm")) +
stat_boxplot(geom ='errorbar') +
scale_fill_manual(values=c("green","blue","red"), name ="condition") +
labs(colour="condition")
r ggplot2 boxplot
1个回答
0
投票

首先绘制彩色误差线,然后绘制没有胡须的箱线图:

ggplot(mirtras, aes(x=variable, y=value, fill=Cond)) +
  stat_boxplot(geom = 'errorbar', aes(color = Cond), width = 0.5,
               position = position_dodge(0.9), linewidth = 1) +
  geom_boxplot(aes(ymax = after_stat(upper), ymin = after_stat(lower)),
               position = position_dodge(0.9)) +
  theme_classic() +
  theme_bw() +
  theme(plot.title = element_text(hjust=0.5, face= "bold"),
        plot.margin = margin(1,3,1,3, "cm")) +
  scale_fill_manual(values=c("green", "blue", "red"), guide = 'none') +
  scale_color_manual(values=c("green4", "blue", "red2"), guide = 'none') 

© www.soinside.com 2019 - 2024. All rights reserved.