Black Lives Matter. Please consider donating to Black Girls Code today.

Making a stacked bar plot

Hi to all!

I’m trying to make a stacked bar plot which are composed of percentage.

|_|Rhizosphere_Standard_30kg|Rhizosphere_Low_0kg|Soil_Low_0kg|Soil_Standard_30kg|
|Actinobacteria|0.0787|0.5085|0.2484|0.1644|
|Cyanobacteria|0.2654|0.6667|0.0247|0.0432|
|Proteobacteria|0.1397|0.8247|0.0192|0.0164|
|Unassigned|0.3750|0.6250|0.0000|0.0000|

So, this is the data I’d like to draw. I’d like to draw a stacked bar plot and each bar is composed of each columns data, and sum of each bar data is ,of course, 1.

I have really no idea why I’m wrong… please help…

Hi Jonathon,

Is this what you’re looking for?

df <- data.frame(act = c(0.0787,0.5085,0.2484,0.1644),
                   cya = c(0.2654,0.6667,0.0247,0.0432),
                   pro = c(0.1397,0.8247,0.0192,0.0164),
                   una = c(0.3750,0.6250,0.0000,0.0000),
                   group = c("Rhizosphere_Standard_30kg","Rhizosphere_Low_0kg",
                             "Soil_Low_0kg","Soil_Standard_30kg"))
df <- tidyr::gather(df, var, value, act:una)

p <- ggplot(df, aes(x = var, y = value,fill=group)) +
  geom_bar(stat='identity')

ggplotly(p)

Hi, @dooogan.

Thank you for your reply.

Yes, but if data are quite a lot, is it a only way to input each data with c(1,2,3,4,…) ?? like making a plot with ggplot2??

Thank you!

What format are your data in? I simply converted your data from a wide to long format data frame for plotting. I’m not sure I understand the question.