gpt4 book ai didi

ggplot2 - 更改ggplot中的颜色

转载 作者:行者123 更新时间:2023-12-02 00:57:21 25 4
gpt4 key购买 nike

我有这个代码:

  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

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

谢谢
丽丽

最佳答案

您可以使用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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com