生成具有不同颜色的多组箱线图GGPLOT2(Generate ggplot2 boxplot wit

2019-08-31 07:03发布

我是相当新的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

Answer 1:

您应该使用interaction()fill= ,并提供两个SpeciesExperimentinteraction() 然后用scale_fill_manual()可以设置values=您需要的颜色(颜色数对应箱线图的数量)。 颜色的顺序是,首先,颜色SpeciesAutumn则颜色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"))



文章来源: Generate ggplot2 boxplot with different colours for multiple groups