-->

Removing y categorical variables facet_grid

2019-08-03 08:03发布

问题:

I am doing a bar plot using ggplot presenting relative abundances of organisms in a community at three sampling sites (column=factor). However, I would like that the facet grid separate the data according to their factor (all factor 1 in top grid, all factor 2 in middle grid, and all factor 3 in lower grid). Currently, all the samples (y value) appear in each grid.

> 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

and here is my command

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()

finally here is my current graph

Thank you!

回答1:

Following the comments above, here is the solution that resolved my issue:

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))


标签: r facet-grid