制作带有颜色编码的堆积条形图

时间:2015-04-10 16:18:24

标签: r ggplot2

我想制作一个堆积条形图,处理包含来自不同种类食草鱼的生物量的数据集,这些食肉鱼属于不同的饲养guildes(食草动物/浏览器),并且已经在不同的海湾,地点和珊瑚礁方面观察到。我想显示这个方面,并尝试在下图中这样做。我希望将不同颜色主题的grazer和浏览器物种进行缩放以使区别清晰(又称蓝色和红色),但保留各个物种之间的差异(SE尽可能但不一定)。问题还在于不同的类型不会在图上聚集在一起。我不知道如何解决这个问题。

我使用的代码是......

plot2 <- ggplot(bayreef, aes(x=interaction(bay,reefaspect),y=meanbiomass, fill=interaction(species,type), colour=type))+geom_bar(stat="identity")
plot2 <- plot2 + scale_fill_hue("grazer"="reds", "browser"="blues")
plot2 <- plot2 + scale_x_discrete(labels=c("Crest / Little Pionier", "Crest / Pionier", "Flat / Little Pionier", "Flat / Pionier"))
plot2 <- plot2 + theme_classic(base_size = 12, base_family = "")
plot2 <- plot2 + xlab("Reef aspects across bays") + theme(axis.title.x=element_text(size=16,face="bold"))
plot2 <- plot2 + theme(axis.text.x=element_text(angle=30, hjust=1, vjust=1))
plot2

使用的数据是

   bay                     reefaspect    species              type     meanbiomass     sd           se
1  Little Pionier Bay      Crest         Acanthurus sp.       grazer   37.3466074      33.1108820   9.55828830
2  Little Pionier Bay      Crest         Chlorus microrhinus  grazer   73.6559169      16.1972196   4.67573455
3  Little Pionier Bay      Crest         Kyphosidae           browser  15.7671586      22.2981295   6.43691554
4  Little Pionier Bay      Crest         Naso unicornis       browser  0.0000000       0.0000000    0.00000000
5  Little Pionier Bay      Crest         Platax sp.           browser  79.7765218      112.8210392  32.56862866
6  Little Pionier Bay      Crest         Scarus rivulatus     grazer   349.6316135     47.6521281   13.75598451
7  Little Pionier Bay      Crest         Scarus sp.           grazer   105.7073736     8.2609870    2.38474154
8  Little Pionier Bay      Crest         Siganus caligulatus  browser  15.9865133      22.6083440   6.52646675
9  Little Pionier Bay      Crest         Siganus doliatus     grazer   144.7839062     7.0905667    2.04687030
10 Little Pionier Bay      Crest         Siganus lineatus     browser  52.2767747      29.9854272   8.65604722
11 Little Pionier Bay      Crest         Siganus puellis      grazer   0.0000000       0.0000000    0.00000000
12 Little Pionier Bay      Crest         Siganus vulpinus     grazer   0.0000000       0.0000000    0.00000000
13 Little Pionier Bay      Flat          Acanthurus sp.       grazer   128.4966437     131.4472282  37.94554628
14 Little Pionier Bay      Flat          Chlorus microrhinus  grazer   31.1013765      43.9839885   12.69708380
15 Little Pionier Bay      Flat          Kyphosidae           browser  1.2150360       1.7183204    0.49603637
16 Little Pionier Bay      Flat          Naso unicornis       browser  0.0000000       0.0000000    0.00000000
17 Little Pionier Bay      Flat          Platax sp.           browser  0.0000000       0.0000000    0.00000000
18 Little Pionier Bay      Flat          Scarus rivulatus     grazer   254.7203565     189.4881645  54.70052140
19 Little Pionier Bay      Flat          Scarus sp.           grazer   153.5634393     35.2397847   10.17284958
20 Little Pionier Bay      Flat          Siganus caligulatus  browser  0.0000000       0.0000000    0.00000000
21 Little Pionier Bay      Flat          Siganus doliatus     grazer   82.2371130      42.7959296   12.35412073
22 Little Pionier Bay      Flat          Siganus lineatus     browser  0.0000000       0.0000000    0.00000000
23 Little Pionier Bay      Flat          Siganus puellis      grazer   0.1309192       0.1851478    0.05344756
24 Little Pionier Bay      Flat          Siganus vulpinus     grazer   0.0000000       0.0000000    0.00000000
25 Pionier Bay             Crest         Acanthurus sp.       grazer   133.7529913     132.2174288  38.16788406
26 Pionier Bay             Crest         Chlorus microrhinus  grazer   62.2027530      0.0000000    0.00000000
27 Pionier Bay             Crest         Kyphosidae           browser  5.4614001       7.7235861    2.22960726
28 Pionier Bay             Crest         Naso unicornis       browser  14.4845452      20.4842403   5.91329082
29 Pionier Bay             Crest         Platax sp.           browser  26.5921740      37.6070131   10.85620956
30 Pionier Bay             Crest         Scarus rivulatus     grazer   356.7316585     251.9779148  72.73975846
31 Pionier Bay             Crest         Scarus sp.           grazer   221.6165110     183.3991895  52.94278572
32 Pionier Bay             Crest         Siganus caligulatus  browser  0.0000000       0.0000000    0.00000000
33 Pionier Bay             Crest         Siganus doliatus     grazer   187.3705947     224.7579079  64.88201930
34 Pionier Bay             Crest         Siganus lineatus     browser  155.3693789     219.7254829  63.42928335
35 Pionier Bay             Crest         Siganus puellis      grazer   48.8260281      54.8655623   15.83832358
36 Pionier Bay             Crest         Siganus vulpinus     grazer   149.1514527     172.8811497  49.90648916
37 Pionier Bay             Flat          Acanthurus sp.       grazer   120.8111113     9.3324359    2.69404219
38 Pionier Bay             Flat          Chlorus microrhinus  grazer   31.1013765      43.9839885   12.69708380
39 Pionier Bay             Flat          Kyphosidae           browser  0.0000000       0.0000000    0.00000000
40 Pionier Bay             Flat          Naso unicornis       browser  0.0000000       0.0000000    0.00000000
41 Pionier Bay             Flat          Platax sp.           browser  0.0000000       0.0000000    0.00000000
42 Pionier Bay             Flat          Scarus rivulatus     grazer   253.1849091     16.2296929   4.68510878
43 Pionier Bay             Flat          Scarus sp.           grazer   206.9541989     7.4724640    2.15711456
44 Pionier Bay             Flat          Siganus caligulatus  browser  0.0000000       0.0000000    0.00000000
45 Pionier Bay             Flat          Siganus doliatus     grazer   141.6131771     35.9189172   10.36889827
46 Pionier Bay             Flat          Siganus lineatus     browser  0.0000000       0.0000000    0.00000000
47 Pionier Bay             Flat          Siganus puellis      grazer   0.0000000       0.0000000    0.00000000
48 Pionier Bay             Flat          Siganus vulpinus     grazer   0.0000000       0.0000000    0.00000000

1 个答案:

答案 0 :(得分:1)

试试这个

plot2 <- ggplot(bayreef, aes(x=interaction(bay,reefaspect),y=meanbiomass, fill =type))+geom_bar(stat="identity")
plot2 <- plot2 + scale_x_discrete(labels=c("Crest / Little Pionier", "Crest / Pionier", "Flat / Little Pionier", "Flat / Pionier")) + scale_fill_hue(c("grazer"="reds", "browser"="blues"))
plot2 <- plot2 + theme_classic(base_size = 12, base_family = "")
plot2 <- plot2 + xlab("Reef aspects across bays") + theme(axis.title.x=element_text(size=16,face="bold"))
plot2 <- plot2 + theme(axis.text.x=element_text(angle=30, hjust=1, vjust=1))+ scale_fill_discrete(name = "Feeding guild")
plot2
这有效吗? enter image description here