堆积的bar_plots的颜色在不同的地块上相同

时间:2017-11-21 22:34:02

标签: r ggplot2 legend stackedbarseries

我从相同的数据制作了许多不同的图,每个子集有点不同,堆栈数量不同(即:一个图表有12个堆栈,另一个图表可能有21个堆栈)所以每个都有不同的传说。 我想在每个绘图中使每个颜色都具有相同的名称,并使用grid.arrange为这些绘图中的5个绘制单个图例。 这是2个图的2个代码,以及从中得出的图表。我还附上了这些情节的图像。我也希望保留一个传奇,让这个数字变得更加清晰。谢谢!enter image description here

library(phyloseq)
library(ggplot2)
library(RColorBrewer)
library(ggthemes)
library(extrafont)
library(plyr)
library(scales)
library(gridExtra)

j <- summarize_taxa(p1.16.phyl, "Phylum", GroupBy = "DayTreat")
j
                           Phylum DayTreat       meanRA         sdRA        minRA        maxRA
          1: D_1__Gracilibacteria        1 6.909613e-04 0.0007985475 0.0000000000 0.0014226919
          2:    D_1__Spirochaetae        1 0.000000e+00 0.0000000000 0.0000000000 0.0000000000
          3:   D_1__Cyanobacteria        1 1.196423e-02 0.0031877772 0.0081233225 0.0151930660
          4:   D_1__Acidobacteria        1 9.084486e-05 0.0001063644 0.0000000000 0.0002032417
          5:  D_1__Actinobacteria        1 2.580024e-02 0.0185281338 0.0081269892 0.0424516639
         ---                                                                                  
         98:  D_1__Proteobacteria        0 2.722527e-01 0.0777378046 0.1960290756 0.3398140889
         99:   D_1__Parcubacteria        0 4.040840e-03 0.0042106725 0.0005486216 0.0092128009
        100:     D_1__Tenericutes        0 0.000000e+00 0.0000000000 0.0000000000 0.0000000000
        101:      D_1__Firmicutes        0 0.000000e+00 0.0000000000 0.0000000000 0.0000000000
        102: D_1__Armatimonadetes        0 9.858497e-04 0.0011513898 0.0000000000 0.0021832379

mypal <- colorRampPalette(brewer.pal(12,"Paired"))
p1 <- ggplot() + theme_bw() +  
      ggtitle("Relative Abundance of Phyla Representing >= 0.1%") + 
      labs(x="Day of Experiment", y= "Relative Abundance")+
      geom_bar(aes(y=100*(meanRA), x = DayTreat, fill = Phylum), data = j, stat = "identity")+
      scale_y_continuous(labels = dollar_format(suffix = "%", prefix =""))+
      scale_fill_manual(values= mypal(17))
p1  
##################
j <- summarize_taxa(p2.17.phyl, "Phylum", GroupBy = "Date")
j
                      Phylum      Date       meanRA         sdRA        minRA        maxRA
     1:    D_1__Spirochaetae 19-Jul-17 2.056108e-03 1.913357e-03 2.640947e-04 4.303678e-03
     2:   D_1__Bacteroidetes 19-Jul-17 1.355181e-01 2.420567e-02 1.026573e-01 1.664959e-01
     3:  D_1__Actinobacteria 19-Jul-17 2.524229e-01 1.599763e-01 1.020998e-01 4.160870e-01
     4:  D_1__Planctomycetes 19-Jul-17 1.071392e-03 1.133542e-03 5.591965e-05 2.531823e-03
     5: D_1__Armatimonadetes 19-Jul-17 2.625672e-04 2.278485e-04 2.104488e-05 5.176257e-04

mypal <- colorRampPalette(brewer.pal(12,"Paired"))
p5 <- ggplot() + theme_bw() +  
          ggtitle("Relative Abundance of Phyla Representing >= 0.1%") + 
          labs(x="Day of Experiment", y= "Relative Abundance")+
          geom_bar(aes(y=100*(meanRA), x = Date, fill = Phylum), data = j, stat = "identity")+
          scale_y_continuous(labels = dollar_format(suffix = "%", prefix =""))+
          scale_fill_manual(values= mypal(12))
p5  

grid.extra(ncol=2, nrow=1, p1,p5)

使用union()命令修复时,我只接收图例的一部分,但不会显示所有堆栈。

mypal <- colorRampPalette(brewer.pal(9,"Set1"))
dd <- union(j1$Phylum,j2$Phylum)
dd2 <- union(dd,j3$Phylum)
dd3 <- union(dd2, j4$Phylum)
dd4 <- union(dd3, j5$phylum)
dd.col2 <- mypal(length(dd4))
names(dd.col2)<-dd4

p4 <- ggplot() + theme_bw() +  
  ggtitle("2016 Pond 2 Control; Abundant Phyla") + 
  labs(x="Day of Experiment", y= NULL)+
  geom_bar(aes(y=100*(meanRA), x = Date, fill = Phylum), data = j4, stat = "identity")+
  scale_y_continuous(labels = dollar_format(suffix = "%", prefix =""))+
  scale_fill_manual(values= dd.col) +
  theme(legend.position="none")
p4 

p5 <- ggplot() + theme_bw() +  
  ggtitle("2017 Pond 2 Control; Abundant Phyla") + 
  labs(x="Day of Experiment", y= NULL)+
  geom_bar(aes(y=100*(meanRA), x = Date, fill = Phylum), data = j5, stat = "identity")+
  scale_y_continuous(labels = dollar_format(suffix = "%", prefix =""))+
  scale_fill_manual(values= dd.col)
p5

grid_arrange_shared_legend(p4,p5,ncol=2, nrow=1)

我是否应添加一个额外的命令以包含图例中的所有24个可用值?这是我的意思的一个例子。请注意图例中缺少小红条。enter image description here

0 个答案:

没有答案