I wasn't clear whether the sample names were the row names in your dataframe, so I simply recreated the data frame putting the sample name in a variable, same as the bacteria names:
Sample Actinobacteria Bacteroidetes Firmicutes Fusobacteria Proteobacteria
1 sample1 0.008424628 0.4162710 0.5547550 0.000000e+00 0.0007245180
2 sample2 0.016857133 0.1329880 0.8028944 3.560112e-05 0.0042721350
3 sample3 0.002029929 0.5381382 0.4236795 3.311006e-02 0.0007978327
Verrucomicrobia Other
1 5.391762e-05 0.019770920
2 4.238314e-02 0.000569618
3 3.534702e-05 0.002209189
To reproduce this dataset you can run the following command:
df <- structure(list(Sample = structure(1:3, .Label = c("sample1",
"sample2", "sample3"), class = "factor"), Actinobacteria = c(0.0084246282,
0.0168571327, 0.0020299288), Bacteroidetes = c(0.41627099, 0.132988,
0.53813817), Firmicutes = c(0.55475503, 0.80289437, 0.42367947
), Fusobacteria = c(0, 3.560112e-05, 0.03311006), Proteobacteria = c(0.000724518,
0.004272135, 0.0007978327), Verrucomicrobia = c(5.391762e-05,
0.04238314, 3.534702e-05), Other = c(0.01977092, 0.000569618,
0.002209189)), .Names = c("Sample", "Actinobacteria", "Bacteroidetes",
"Firmicutes", "Fusobacteria", "Proteobacteria", "Verrucomicrobia",
"Other"), class = "data.frame", row.names = c("1", "2", "3"))
As @zx8754 suggested, this data frame requires reshaping, i.e., moving from a wide format to a long format. For more info, check this link for a few examples.
If the dataframe above is named df
, the following command will reshape it in long format:
library(reshape2)
df_long <- melt(df, id.vars = "Sample", variable.name = "Phyla")
From here we can plot using ggplot:
library(ggplot2)
ggplot(df_long, aes(x = Sample, y = value, fill = Phyla)) +
geom_bar(stat = "identity")
which gives:
