R语言 更改ggplot2箱线图中须的颜色

piok6c0g  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(116)

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

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")
n1bvdmb6

n1bvdmb61#

首先绘制彩色误差条,然后绘制不带须线的箱线图:

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)) +
  scale_fill_manual(values = c("green", "blue", "red"), guide = 'none') +
  scale_color_manual(values = c("green4", "blue", "red2"), guide = 'none') +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5, face = "bold"),
        plot.margin = margin(1, 3, 1, 3, "cm"))

使用数据

问题中没有提供数据,但以下数据集具有相同的名称和近似值:

set.seed(1)

mirtras <- data.frame(variable = 'X27',
                      Cond = rep(c('A', 'B', 'C'), times = c(20, 20, 40)),
                      value = c(rnorm(45, 15.5, 0.5), rnorm(35, 19, 0.5)))

相关问题