我正在使用ggplot进行条形图,在三个采样点(柱=因子)显示社区中有机体的相对丰度。但是,我希望facet网格根据它们的因素分离数据(顶部网格中的所有因子1,中间网格中的所有因子2以及下部网格中的所有因子3)。目前,所有样本(y值)都出现在每个网格中。
> head(facet,50)
ID Taxa Samples value factor
1 1 Ciliates DNAHOR5m 0.040637972 1
2 2 Dinoflagellates DNAHOR5m 0.265239240 1
3 3 MALVs DNAHOR5m 0.005025126 1
4 4 Phaeocystales DNAHOR5m 0.002184837 1
5 5 Prymnesiales DNAHOR5m 0.000436967 1
6 6 picozoa DNAHOR5m 0.002184837 1
7 7 Cercozoa DNAHOR5m 0.008083898 1
8 8 Chlorophyta DNAHOR5m 0.001529386 1
9 9 MASTs DNAHOR5m 0.000655451 1
10 10 Pelagophyceae DNAHOR5m 0.669652611 1
11 11 Other DNAHOR5m 0.004369674 1
12 12 Ciliates DNAHOR35m 0.062486345 1
13 13 Dinoflagellates DNAHOR35m 0.845095040 1
14 14 MALVs DNAHOR35m 0.018571116 1
15 15 Phaeocystales DNAHOR35m 0.024907144 1
16 16 Prymnesiales DNAHOR35m 0.001747870 1
17 17 picozoa DNAHOR35m 0.001310902 1
18 18 Cercozoa DNAHOR35m 0.001310902 1
19 19 Chlorophyta DNAHOR35m 0.000873935 1
20 20 MASTs DNAHOR35m 0.000873935 1
21 21 Pelagophyceae DNAHOR35m 0.031461656 1
22 22 Other DNAHOR35m 0.011361154 1
23 23 Ciliates DNAEDG5m 0.024251693 2
24 24 Dinoflagellates DNAEDG5m 0.607384750 2
25 25 MALVs DNAEDG5m 0.016604763 2
26 26 Phaeocystales DNAEDG5m 0.165392178 2
27 27 Prymnesiales DNAEDG5m 0.006772995 2
28 28 picozoa DNAEDG5m 0.098099192 2
29 29 Cercozoa DNAEDG5m 0.001966354 2
30 30 Chlorophyta DNAEDG5m 0.011579637 2
31 31 MASTs DNAEDG5m 0.010924186 2
32 32 Pelagophyceae DNAEDG5m 0.001310902 2
33 33 Other DNAEDG5m 0.055713349 2
34 34 Ciliates DNAEDG35m 0.041511907 2
35 35 Dinoflagellates DNAEDG35m 0.160367053 2
36 36 MALVs DNAEDG35m 0.002403321 2
37 37 Phaeocystales DNAEDG35m 0.730172602 2
38 38 Prymnesiales DNAEDG35m 0.002840288 2
39 39 picozoa DNAEDG35m 0.019663535 2
40 40 Cercozoa DNAEDG35m 0.005680577 2
41 41 Chlorophyta DNAEDG35m 0.001092419 2
42 42 MASTs DNAEDG35m 0.004588158 2
43 43 Pelagophyceae DNAEDG35m 0.000436967 2
44 44 Other DNAEDG35m 0.031243172 2
45 45 Ciliates DNAEDG75 0.064234215 2
46 46 Dinoflagellates DNAEDG75 0.181123006 2
47 47 MALVs DNAEDG75 0.019663535 2
48 48 Phaeocystales DNAEDG75 0.673366834 2
49 49 Prymnesiales DNAEDG75 0.002403321 2
50 50 picozoa DNAEDG75 0.016604763 2
这是我的命令
df$Samples <- factor(facet$Samples, levels = c("DNAEES370m", "DNAEES150m", "DNAEES35m",
"DNAEES15m", "DNAEES5m", "DNAEDG75", "DNAEDG35m", "DNAEDG5m", "DNAHOR35m", "DNAHOR5m"))
# Create bar graph
ggplot(facet, aes(x = Samples, y = value, group = Taxa, fill = Taxa)) +
geom_bar(stat = "identity", colour = "black") +
theme_gray(base_size = 12, base_family = "") +
labs(shape = "", x = "", y = "Relative abundance (%)") +
scale_fill_manual(values = group.colors, breaks = c("Ciliates", "Dinoflagellates",
"MALVs", "Phaeocystales", "Prymnesiales", "Picozoa", "Cercozoa", "Chlorophyta",
"MASTs", "Pelagophyceae", "Other")) +
facet_grid(factor ~ ., scales = "free") +
coord_flip()
谢谢!
答案 0 :(得分:1)
根据上述评论,以下是解决我的问题的解决方案:
p=ggplot(facet, aes(x=Samples,y=value, group=Taxa,fill=Taxa))+geom_bar(stat="identity",colour="black",width=0.5)+theme_gray(base_size=12, base_family="")+labs(shape="",x="", y="Relative abundance (%)")+scale_fill_manual(values=group.colors, breaks=c("Ciliates","Dinoflagellates","MALVs","Phaeocystales","Prymnesiales","Picozoa","Cercozoa","Chlorophyta","MASTs","Pelagophyceae","Other"))+facet_grid(. ~ factor, space="free",scales="free", drop = TRUE) + theme(axis.text.x = element_text(angle=90), axis.text.y = element_text(angle=90))
g <- ggplotGrob(p)
leg <- g$grobs[grepl("guide-box", g$layout$name)][[1]]
p2 <- grid.grabExpr(print(p+theme(legend.position="none"), vp=viewport(angle=270)))
gridExtra::grid.arrange(p2,leg, ncol=2, widths=c(2,1))