Home > OS >  Create a stacked bar chart with already grouped data using ggplot2
Create a stacked bar chart with already grouped data using ggplot2

Time:08-21

I have a dataset that has grouped values like this: enter image description here

How can I turn it into a stacked bar chart using ggplot? Thanks

CodePudding user response:

library(data.table)
library(ggplot2)

dt <- data.table(Type =  c('a',  'b', 'c', 'd', 'E'),
                 Location1 = c(0, 3, 44, 5, 8),
                 Location2 = c(2, 26, 33, 2, 0),
                 Location3 = c(23, 6, 6, 8, 0),
                 Location4 = c(32, 22, 89, 0, 0))

dt <- melt(dt, id.vars = c("Type"))

ggplot(dt, aes(x = variable, y = value, fill = Type))  
  geom_bar(stat = "identity")

stackbar

  • Related