为多个组生成不同颜色的ggplot2 boxplot [英] Generate ggplot2 boxplot with different colours for multiple groups

查看:817
本文介绍了为多个组生成不同颜色的ggplot2 boxplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R和ggplot相当陌生。

我试图生成一个由两个变量排序的boxplot。在我的案例物种和实验。
我到目前为止使用的是

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

是我的物种的箱型图,每个物种有2个条,每个实验都有一个条。我的问题是现在,是否可以改变这个颜色我可以说每种物种都有不同的颜色,并且可以说,这些实验的颜色的阴影不同吗?可以说,第一种物种比黑暗和光明蓝色的酒吧,第二个黑暗和浅绿色的酒吧,等等。

数据的形式是一个csv文件,看起来像这样:

 实验,物种,蛋白质
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
秋天,M_faveolata,4.638054165
秋天,M_faveolata,7.191664953
秋天,M_faveolata,6.981827102
秋天,M_faveolata,6.386452477
Autumn,M_annularis,4.709770404
秋天,M_annularis,3.554071459
Autumn,M_annularis,4.596686351
Autumn,M_annularis,3.530585628


解决方案

您应该为 fill = 使用 interaction() 种类实验在 interaction()内。然后使用 scale_fill_manual(),您可以为您需要的颜色设置 values = -plots)。颜色顺序首先是 Species Autumn 中的颜色,然后是 Species 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))


I'm fairly new to R and ggplot.

I'm trying to generate a boxplot sorted by two variables. In my case Species and Experiment. What I got so far by using

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

are boxplots of my species and each species has 2 bars, one for each experiment.

My question is now, is it possible to change the colours in this way, that I have different colours per species and lets say, different shading of those colours for the experiments?

Lets say, the first species would than have a dark and light blue bar, the second a dark and light green bar, etc.

The data is in the form of a csv file and looks like this:

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

解决方案

You should use interaction() for the fill= and provide both Species and Experiment inside interaction(). Then with scale_fill_manual() you can set values= for the colors you need (number of colors correspond to number of box-plots). Order of colors is, first, colors for Species in Autumn then colors for Species in 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"))

这篇关于为多个组生成不同颜色的ggplot2 boxplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆