简体   繁体   中英

Making a stacked barplot of bacteria in R

I melted my dataframe, which contains two groups:'high' and 'low', which all have the relative abundance of each phyla in the group. I would like to create a stacked bar plot, so that there are two columns (high and low) and its filled with the percentage of the phyla out of 100.

    Phylum          Group   value
1   Euryarchaeota   High    0.0000000000
2   Acidobacteria   High    0.0000000000
3   Actinobacteria  High    0.0014929190
4   Bacteroidetes   High    0.6620724580
5   Chloroflexi     High    0.0000000000
6   Cyanobacteria   High    0.0036403330
7   Elusimicrobia   High    0.0000000000
8   Fibrobacteres   High    0.0000000000
9   Firmicutes      High    0.3299947690
10  Fusobacteria    High    0.0008169250
11  GemmatimonadetesHigh    0.0000000000
12  Lentisphaerae   High    0.0000000000
13  Nitrospirae     High    0.0000000000
14  Planctomycetes  High    0.0000000000
15  Proteobacteria  High    0.0010844560
16  SR1             High    0.0000000000
17  Spirochaetes    High    0.0002579760
18  Synergistetes   High    0.0000000000
19  TM7             High    0.0000000000
20  Tenericutes     High    0.0000023900
21  Verrucomicrobia High    0.0006377750
22  [Thermi]        High    0.0000000000
23  Euryarchaeota   Low     0.0000018200
24  Acidobacteria   Low     0.0000015100
25  Actinobacteria  Low     0.0094485600
26  Bacteroidetes   Low     0.4611814500
27  Chloroflexi     Low     0.0000009090
28  Cyanobacteria   Low     0.0005776960
29  Elusimicrobia   Low     0.0000272000
30  Fibrobacteres   Low     0.0000001330
31  Firmicutes      Low     0.5217123900
32  Fusobacteria    Low     0.0005029220
33  GemmatimonadetesLow     0.0000003990
34  Lentisphaerae   Low     0.0000044400
35  Nitrospirae     Low     0.0000000444
36  Planctomycetes  Low     0.0000000222
37  Proteobacteria  Low     0.0027838720
38  SR1             Low     0.0000002440
39  Spirochaetes    Low     0.0000716000
40  Synergistetes   Low     0.0000093400
41  TM7             Low     0.0000555000
42  Tenericutes     Low     0.0013692220
43  Verrucomicrobia Low     0.0022507390
44  [Thermi]        Low     0.0000000222

I have tried plotted like this: ggplot(df_long, aes(x = "Group", y = value, fill = Phylum)) geom_bar(stat = "identity") but the plot comes out empty.

I melted my dataframe (as shown) before with this: df_long <- melt(data, id.vars = "Phylum", variable.name = "Group")

Any suggestions why my plot isn't appearing as I would like?

You need to use position="stack" at the geom_bar function, like this:

ggplot(df_long, aes(Group, value, fill=Phylum)) + geom_bar(stat="identity",          
position="stack") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM