更改ggplot中的颜色

时间:2018-11-14 15:08:05

标签: ggplot2 colors

我有此代码:

  ggplot(baseline, aes(x=Group, y=Thickness, color=Group, !is.na(Thickness))) + 
  geom_boxplot(outlier.colour="black", outlier.shape=16, outlier.size=0.5, notch=FALSE) +
  geom_dotplot(binaxis='y', stackdir='center', dotsize=0.5) +
  scale_x_discrete(limits=c("HC", "Patients")) +
  scale_y_continuous(breaks = seq(0.15, 0.30, by = 0.05 ))

默认情况下,我的HC为红色,患者为蓝色。但是我想得到方框图:黑色的HC和红色的患者。

This is the output image that I get

可重现的示例:

PatientID.  Group.  Thickness
1OD         HC        0.5
1OS         HC        0.5
23OD        Patient   0.001
23OS        Patient   0.01
44OD        Patient   0.03
44OS        Patient   0.04
3OD         HC        0.7
3OS         HC        0.9

我找不到代码。你可以帮帮我吗?

谢谢 莉莉

1 个答案:

答案 0 :(得分:2)

您可以使用scale_color_manual指定所需的颜色。 附注:如果您想使用更高级的混合箱形图,请查看this

library(tidyverse)

df <- read_table("PatientID.  Group.  Thickness
1OD         HC        0.5
1OS         HC        0.5
23OD        Patient   0.001
23OS        Patient   0.01
44OD        Patient   0.03
44OS        Patient   0.04
3OD         HC        0.7
3OS         HC        0.9")

ggplot(df, aes(x = Group., y = Thickness, color = Group.)) +
  geom_boxplot(outlier.colour = "black", 
               outlier.size = 0.5, notch = FALSE) +
  geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 0.5, 
               show.legend = FALSE) +
  scale_color_manual(values = c("HC" = "black", "Patient" = "red"))
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.

reprex package(v0.2.1.9000)于2018-11-15创建