My data includes 6 samples ( as rownames currently) and 24 columns each of which is named after different bacterial species, and the numbers are the relative abundances.
Here is the structure;
dput(sig_speciesstacked)
structure(c("Control1", "Control2", "Control3", "Disease1", "Disease2", "Disease3", "0.32503", "0.55197", "1.23225", "0", "0", "0", "0.11568", "1.27372", "0.04306", "0", "0", "0", "0.78402", "0.99583", "0.03723", "0", "0", "0", "0.07664", "0.0932", "0.28018", "0", "0", "0", "0.29037", "0.74246", "0.3061", "0", "0", "0", "0.22328", "0.40351", "0.00416", "0", "0", "0", "0", "0", "0", "0.23779", "0.70807", "0.00891", "0.04852", "0.34497", "0.19266", "0", "0", "0", "0.26408", "0.05026", "0.0022", "0", "0", "0", "0.31206", "0.59428", "0.15606", "0", "0", "0", "0.13716", "0.55023", "0.4716", "0", "0", "0", "0.27194", "0.57013", "0.23164", "0", "0", "0", "6.84233", "2.18166", "0.6827", "0", "0", "0", "0", "0", "0", "0.94569", "0.0108", "0.06016", "0.32686", "0.04407", "1.02125", "0", "0", "0", "0", "0", "0", "0.51243", "0.10427", "1.48269", "0", "0", "0", "1.49594", "0.90364", "0.0081", "1.27002", "1.80154", "0.33065", "0", "0", "0", "2.40484", "0.36535", "3.79276", "0", "0", "0", "4.23202", "2.63742", "0.37963", "0", "0", "0", "0.38793", "0.81874", "0.04095", "0", "0", "0", "0", "0", "0", "1.04847", "0.08983", "0.02608", "0", "0", "0", "0.14408", "0.1637", "0.07754"), .Dim = c(6L, 24L), .Dimnames = list(c("1", "2", "3", "4", "5", "6"), c("Sample", "Alistipes_finegoldii", "Alistipes_indistinctus", "Alistipes_onderdonkii", "Alistipes_senegalensis", "Bacteroidales_bacterium_ph8", "Bifidobacterium_adolescentis", "Bifidobacterium_dentium", "Collinsella_aerofaciens", "Coprobacter_fastidiosus", "Coprococcus_comes", "Dorea_longicatena", "Eubacterium_hallii", "Eubacterium_rectale", "Fusobacterium_varium", "Lachnospiraceae_bacterium_3_1_46FAA", "Lactobacillus_mucosae", "Megasphaera_micronuciformis", "Odoribacter_splanchnicus", "Roseburia_hominis", "Ruminococcus_bromii", "Ruminococcus_callidus", "Streptococcus_parasanguinis", "Veillonella_atypica")))
I am trying to make a stacked bar chart showing the different abundances for the different samples ( 3 control and 3 diseases).
First I added a column name to the one containing my sample names, so there were then 25 columns in total. 1st one contains samples, 2:25 contain the abundances of the 24 different species.
sig_speciesstacked <- cbind(Samples= rownames(sig_speciesstacked), sig_speciesstacked)
print(colnames(sig_speciesstacked))
rownames(sig_speciesstacked) <- c("1", "2", "3", "4", "5", "6").
I already have installed and loaded reshape2. The code I run then is
sig_speciesstackplot1 <- melt(sig_speciesstacked, id.vars = "Samples", variable.name = "species")
pdf("Stackedbarplot.species.pdf", width = 6, height = 7)
ggplot(sig_speciesstackplot1,aes(x=Samples, y=value, fill= species))+ geom_bar(stat =
"identity", position="fill")
The error I am met with is Error in FUN(X[[i]], ...) : object 'Samples' not found, then it will be abundance not found, then species not found.
Edit; I understand I have to rename aes(x=, y=) to the col names of sig_speciesstackplot1. However, this is not the correct format of the sig_speciesstackplot1 output following melt?
Var1 Var2 value
1 1 Samples Control1
2 2 Samples Control2
3 3 Samples Control3
4 4 Samples Disease1
5 5 Samples Disease2
6 6 Samples Disease3
7 1 Alistipes_finegoldii 0.32503
8 2 Alistipes_finegoldii 0.55197
9 3 Alistipes_finegoldii 1.23225
10 4 Alistipes_finegoldii 0
And so on, each of the 24 species is repeated 6 times with different abundance levels corresponding to the different samples.
Not sure why Var1 and Var2 were not renamed to "Samples" and "species" respectively from my line of code above, and why the output is like that.
And running the ggplot using aes(x = Var1 etc) gets a plot that is completely wrong.
Edit; For anybody having a similar issue, please do not use cbind. From an example on here, they made column 1 contain the sample names, hence why I used it. If you don't do this and just have the row names as the sample names, it will work fine. Thank you very much to those who helped below!