-2

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

sweatcrepe
  • 23
  • 3
  • I’m guessing that downvote is for using an image when text is requested for data. – IRTFM Aug 21 '22 at 01:29
  • https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example That's the link we're supposed to post in those cases? :) – Will Aug 21 '22 at 01:32

1 Answers1

1
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

Will
  • 910
  • 7
  • 17