我做了几个图,我绘制了每种植物的10个细菌种类,我根据细菌所属的家族给条着色.虽然我可以做得很好,但我希望在不同的家庭和地块上保持相同的 colored颜色 ,因为稍后我想将它们组合成一个多面的图形,展示属于同一家庭的细菌属 colored颜色 相同是有意义的.
以下是我的数据和代码示例:
dput(top10_cintybus)
structure(list(Order = c("Enterobacterales", "Enterobacterales",
"Enterobacterales", "Sphingomonadales", "Enterobacterales", "Bacillales",
"Hyphomicrobiales", "Bacillales", "Xanthomonadales", "Hyphomicrobiales"
), Family = c("Enterobacteriaceae", "Erwiniaceae", "Yersiniaceae",
"Sphingomonadaceae", "Morganellaceae", "Bacillaceae", "Methylobacteriaceae",
"Bacillaceae", "Xanthomonadaceae", "Rhizobiaceae"), MKC132 = c(0L,
0L, 27L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), MKC146 = c(33L, 8L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), MKC227 = c(11L, 0L, 0L, 0L, 6L,
2L, 0L, 3L, 0L, 0L), MKC231 = c(37L, 0L, 0L, 20L, 0L, 0L, 3L,
0L, 2L, 2L), MKC242 = c(0L, 7L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L
), MKC276 = c(9L, 0L, 7L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), MKC351 = c(6L,
19L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Total = c(96, 34, 34, 20,
6, 4, 3, 3, 2, 2), Genus = structure(c(10L, 8L, 9L, 7L, 6L, 5L,
4L, 3L, 2L, 1L), levels = c("Allorhizobium", "Xanthomonas", "Bacillus",
"Methylobacterium", "Heyndrickxia", "Arsenophonus", "Sphingomonas",
"Pantoea", "Serratia", "???.1"), class = "factor")), row.names = c(NA,
10L), class = "data.frame")
dput(top10_alappa)
structure(list(Order = c("Enterobacterales", "Xanthomonadales",
"Enterobacterales", "Enterobacterales", "Enterobacterales", "Enterobacterales",
"Enterobacterales", "Hyphomicrobiales", "Enterobacterales", "Burkholderiales"
), Family = c("Erwiniaceae", "Xanthomonadaceae", "Enterobacteriaceae",
"Erwiniaceae", "Enterobacteriaceae", "Enterobacteriaceae", "Enterobacteriaceae",
"Rhizobiaceae", "Morganellaceae", "Oxalobacteraceae"), MKC154 = c(11L,
0L, 36L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), MKC167A = c(14105L, 0L,
5810L, 13055L, 1223L, 2316L, 1276L, 0L, 550L, 13L), MKC167B = c(18L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), MKC214 = c(83842L, 5L, 22936L,
175L, 6828L, 94L, 0L, 7L, 0L, 0L), MKC226 = c(0L, 0L, 11L, 0L,
0L, 0L, 0L, 0L, 0L, 0L), MKC233 = c(55L, 0L, 13L, 14L, 0L, 0L,
0L, 0L, 0L, 0L), MKC314 = c(0L, 0L, 0L, 3L, 0L, 0L, 0L, 0L, 0L,
0L), MKC364 = c(32L, 8L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), MKC366 = c(38L,
46599L, 0L, 10L, 62L, 0L, 0L, 549L, 0L, 512L), Total = c(98101,
46612, 28806, 13257, 8113, 2410, 1276, 556, 550, 525), Genus = structure(10:1, levels = c("Janthinobacterium",
"Morganella", "Allorhizobium", "Citrobacter", "Siccibacter",
"Enterobacter", "Erwinia", "???.1", "Stenotrophomonas", "Pantoea"
), class = "factor")), row.names = c(NA, 10L), class = "data.frame")
我的代码为其中一个框架绘制图形作为示例.我可以按家族为条着色,但如果可能的话,我希望有一种方法使跨数据集的家族保持相同.注意,"家庭"是一个因子列.
species="Cintybus"
ggplot(data=top10_cintybus,aes(x=Genus, y=Total, fill=Family)) +
geom_bar(stat = "identity", colour="black") +
coord_flip() +
theme_bw() +
scale_y_continuous(trans="log10", limits=c(1,1000000), labels=scales::comma,
expand=expansion(mult=c(0,.05))) +
#theme(text=element_text(size=18)) +
theme(axis.title.x = element_text(size=18)) +
theme(axis.text.x = element_text(colour="black", face="bold", size=15)) +
theme(axis.title.y = element_text(size=18, vjust=2.5)) +
theme(axis.text.y = element_text(face="bold.italic", colour="black", size=15)) +
geom_text(aes(label=format(Total, big.mark=",")), hjust=1.5,
color="white", fontface="plain", size=5) +
xlab("Genus") + ylab("Abundance") +
ggtitle(species) + theme(plot.title = element_text(size=18))
如果我为另一个数据帧运行上面的代码,这是一个图像.
例如,"Pantoea"属于欧文科,但该科在两个图的传说中有不同的 colored颜色 .我不想写一个长长的列表,我手动 for each 家族添加 colored颜色 ,因为在其他一些数据集中,我有100个家族,前10个将在数据集中混合.