My data is:
positive <- c("21", "22", "33", "21", "27") ##Percentage
negative<- c("71", "77", "67", "79", "73") ##Precentage
sample <- c("Hr", "Fi", "We", "Pa", "Ki")
mydata <- data.frame(positive , negative, sample)
I want to create a stacked bar graph that shows positive and negative percentages for each category in the sample variable. I tried this:
ggplot(mydata) +
geom_bar(aes(x = sample, fill = positive))
but did not wrok. Sorry if the question looks basic. I started R a few weeks ago.
This probably serves your purpose:
library(tidyverse)
mydata %>% pivot_longer(cols = !sample, names_to = "status", values_to = "percentage") %>%
ggplot(aes(fill = status, x = sample, y = percentage)) +
geom_bar(position = "stack", stat = "identity")