This is a test version of Biostars. For the public version, visit https://www.biostars.org.
geom_bar with several variables - preferably stacked bar

Hi

I am quite new to R and ggplot - I need to make a stacked barplot with the variables below

           -     +      1:2,5   1:12,5   1:1     1:2      1:5    1:10
alive    15800  28000   89000   52000   34000   28000   12000   28000
dead     11500  23000   60000   33000   25000   18000   23000   18000
sawed    53000  53000   73000   57000   130000  78000   63000   58000

So I want stacked barplots for the groups alive / dead and then join it with a normal barplot for the sawed category... is that even possible in ggplot? I managed to do it with normal R studio ... can't figure it out with ggplot.

This is how I want my plots to look like https://imgur.com/a/V87GyFJ

Thank you for any of your advice / help :)

r ggplot2

1 answer

Here is a start:

library(data.table)
library(ggplot2)

df1 <- fread("
status - + 1:2,5 1:12,5 1:1 1:2 1:5 1:10
alive 15800 28000 89000 52000 34000 28000 12000 28000
dead 11500 23000 60000 33000 25000 18000 23000 18000
sawed 53000 53000 73000 57000 130000 78000 63000 58000
")

plotDat <- melt(df1, id.vars = "status")
plotDat$grp <- ifelse(plotDat$status %in% c("dead", "alive"), 1, 2)

ggplot(plotDat, aes(variable, value, group = grp, fill = status)) +
  geom_bar(stat = "identity", position = "dodge")

Log in to answer this question.