gpt4 book ai didi

r - 为多个组生成具有不同颜色的ggplot2 boxplot

转载 作者:行者123 更新时间:2023-12-03 10:19:04 29 4
gpt4 key购买 nike

我对 R 和 ggplot 还很陌生。

我正在尝试生成一个按两个变量排序的箱线图。就我而言,物种和实验。
到目前为止我得到了什么

ggplot(DF, aes(Species, Protein, fill=Experiment, dodge=Experiment)) +
stat_boxplot(geom ='errorbar')+
geom_boxplot()

是我的物种的箱线图,每个物种有 2 个条形图,每个实验一个。

我现在的问题是,是否有可能以这种方式改变颜色,每个物种都有不同的颜色,比如说,实验中这些颜色的不同阴影?

比方说,第一个物种会有一个深蓝色和浅蓝色条,第二个是深绿色和浅绿色条,等等。

数据采用 csv 文件的形式,如下所示:
Experiment,Species,Protein
Spring,D_strigosa,5.107767364
Spring,D_strigosa,8.288980741
Spring,D_strigosa,7.537376567
Spring,D_strigosa,4.811744241
Spring,D_strigosa,9.559043454
Spring,M_cavernosa,13.66759289
Spring,M_cavernosa,23.54880195
Spring,M_cavernosa,11.00790037
Spring,M_cavernosa,13.70597973
Spring,M_cavernosa,12.26970906
Spring,M_faveolata,9.559043454
Spring,M_faveolata,4.135860474
Spring,M_faveolata,4.557306615
Spring,M_faveolata,7.621872315
Spring,M_faveolata,6.016859956
Spring,M_annularis,6.294920733
Spring,M_annularis,5.163371831
Spring,M_annularis,2.177243143
Spring,M_annularis,3.55966551
Autumn,D_strigosa,3.709978392
Autumn,D_strigosa,7.192777836
Autumn,D_strigosa,5.31544441
Autumn,D_strigosa,6.578602058
Autumn,D_strigosa,5.610085559
Autumn,M_cavernosa,11.64182554
Autumn,M_cavernosa,10.44968102
Autumn,M_cavernosa,9.377389318
Autumn,M_cavernosa,10.99346107
Autumn,M_cavernosa,11.5676896
Autumn,M_faveolata,4.638054165
Autumn,M_faveolata,7.191664953
Autumn,M_faveolata,6.981827102
Autumn,M_faveolata,6.386452477
Autumn,M_annularis,4.709770404
Autumn,M_annularis,3.554071459
Autumn,M_annularis,4.596686351
Autumn,M_annularis,3.530585628

最佳答案

您应该使用 interaction()fill=并同时提供 SpeciesExperimentinteraction() .然后用 scale_fill_manual()您可以设置 values=对于您需要的颜色(颜色数量对应于箱线图的数量)。颜色顺序首先是 Species 的颜色在 Autumn然后颜色为 SpeciesSpring .

ggplot(DF, aes(Species, Protein, fill=interaction(Species,Experiment), dodge=Experiment)) +
stat_boxplot(geom ='errorbar')+
geom_boxplot()+
scale_fill_manual(values=c("blue","green","red","cyan",
+ "darkblue","darkgreen","darkred","darkcyan"))

enter image description here

关于r - 为多个组生成具有不同颜色的ggplot2 boxplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16026215/

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