0
votes

I am trying to create a plot from a dataframe that looks like the following:

  management depth id          genus rep1 rep2 rep3 rep4 sum
1         OM     A  1     alternaria    3    0    0    0   3
2         OM     A  2    aspergillus    0    0    0    0   0
3         OM     A  3     chaetomium    0    0    0    0   0
103       PM     A  1     alternaria    0    0    0    0   0
104       PM     A  2    aspergillus    4    1    4   35  44
105       PM     A  3     chaetomium    0    0    0    7   7

I want to create a stacked barchart with the management on the x axis, filled with genus, and y should represent the sum. I used

stack<-ggplot(df, aes(x=management, fill=genus)) + geom_bar(position="fill", color="white")

My first problem is that I don't know how to implement the sum in this code. I can create two separate plots, but it would be nicer if I could fit both bars into one plot.

I also created a stack bar chart with the dataframe above, but the result was as the following: plot

Because I had a very large value for one genus, ggplot will always create a two-column-stacked-bar. Is there anyway to get around that?

1

1 Answers

0
votes

It would be easier to tailor a solution if you provided your expected result, but I believe this is what you are looking for?

library(ggplot2)
df <- read.table(textConnection("
 row.id management depth id          genus rep1 rep2 rep3 rep4 sum
1         OM     A  1     alternaria    3    0    0    0   3
2         OM     A  2    aspergillus    0    0    0    0   0
3         OM     A  3     chaetomium    0    0    0    0   0
103       PM     A  1     alternaria    0    0    0    0   0
104       PM     A  2    aspergillus    4    1    4   35  44
105       PM     A  3     chaetomium    0    0    0    7   7"), header=TRUE, stringsAsFactors = FALSE) 

ggplot(df, aes(x=management, y=sum, fill=genus)) + 
geom_bar( color="white", stat="identity")

enter image description here