0

My 4x5 tibble (below) is stored in a .csv file:

My data

I would like to create a stacked bar chart, where the x-axis represents the three foods and the y-axis represents the percentage points (cumulatively, each bar should add up to 100%), using ggplot2.

I'm extremely new to using R (and Stackoverflow) and unsure where to begin with creating this.

Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • 1
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and a clear explanation of what you're trying to do and what hasn't worked. If you're totally unsure what to do, a tutorial is a better place to start than SO; the [package docs](http://ggplot2.tidyverse.org/) list several – camille Jan 21 '20 at 19:32

1 Answers1

1

Try this using tidyverse which contains ggplot2:

dat %>% 
gather(Key, value, -Food) %>%
ggplot(aes(x = Food, y = value, group = Key, fill = Key)) +
 geom_bar(stat = "identity")

I created dat using

dat <- data.frame(Food = c("Pizza", "Pasta", "Salad"), Positive = c(0.06, 0.04, 0.06), Neutral = c(0.21, 0.09, 0.13), Negative = c(0.54, 0.74, 0.69), No_Opinion = c(0.19, 0.13, 0.12))

So all of the bars add up to 1.

akash87
  • 3,876
  • 3
  • 14
  • 30