8

我对 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
4

1 回答 1

15

您应该使用interaction()forfill=并同时提供SpeciesExperimentinside interaction()。然后scale_fill_manual()你可以设置values=你需要的颜色(颜色的数量对应于箱线图的数量)。颜色的顺序首先是 in 的颜色SpeciesAutumn然后是Speciesin的颜色Spring

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

在此处输入图像描述

于 2013-04-16T05:11:14.953 回答