0

我从相同的数据制作了许多不同的图,每个子集都有点不同,堆栈数量不同(即:一个图有 12 个堆栈,另一个可能有 21 个堆栈),所以每个图都有不同的图例。我想在每个绘图中使每种颜色都具有相同的名称,并使用 grid.arrange 为其中 5 个绘图创建一个图例。这是 2 个地块的 2 个代码以及这些地块的派生表。我还附上了地块的图像。我也希望保留 1 个图例能让这个数字变得更清晰。谢谢!。在此处输入图像描述

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 个可用值吗?这是我的意思的一个例子。请注意图例中缺少红色小条。在此处输入图像描述

4

0 回答 0