I'm making a number of different plots from the same data and each subset is a bit different, with different number of stacks (i.e: one plot has 12 stacks, and another might have 21 stacks) so each has a different legend. I would like to make each color the same name throughout each plot and make a single legend for 5 of these plots using grid.arrange. Here is 2 codes for 2 plots and the table the plots are being derived from. I've also attached images of the plots. I'm also hoping keeping 1 legend will allow the figure to become clearer. Thank you!.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)
When fixed with the union() command, I only recieve a portion of the legend and it does not show all of the stacks.
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)
Is there an additional command I should add to include all 24 available values in the legend? Here is an example of what I mean. Notice the small red bar is missing in the legend.enter image description here