Reputation: 493
Suppose I have the below data. I want to create a bar plot, which shows the proportion that said '1' in winter, and 1 in 'spring', and then '2' in Winter and '2' in spring. data example.
So:
x% said 1 in Winter x% said 1 in Spring
x% said 2 in Winter x% said 2 in Spring
The chart I have so far is here chart example.
The issue with this is that I don't want all the bars to add up to 100. I want the red (spring bars) to add to 100%, and the green (winter) bars to add to 100.
So instead of the proportion being based on winter and spring combined, winter and spring proportions should be calculated seperately, but appear on the same chart.
Here is my code so far:
ggplot(data = a, aes(x = Answer, y = (..count..)/sum(..count..))) +
geom_bar(aes(fill = season), position = "dodge") +
theme_minimal() +
scale_y_continuous(labels=scales::percent,limits= c(0, 1))+
labs (x="%")
The issue is that:
The two green bars should add to 100% The two red bars should add up to 100%
Thanks in advance!
Upvotes: 0
Views: 189
Reputation: 10352
My approach would be to calculate the frequencies direct in the data.frame and then plot:
### example data
a <- data.frame(
Answer = c(1,2,2,1,1,2,1,2,1,2,2,2,1),
season = c("Winter", "Spring", "Spring", "Winter", "Spring", "Winter",
"Spring", "Spring", "Spring", "Spring", "Spring", "Spring", "Winter")
)
library(tidyverse)
a <- a %>% group_by(season, Answer) %>%
summarise(n = n()) %>%
mutate(freq = n/sum(n))
ggplot(data = a, aes(x = Answer, y = freq)) +
geom_bar(aes(fill = season),stat = "identity", position = "dodge") +
theme_minimal() +
scale_y_continuous(labels=scales::percent,limits= c(0, 1))+
labs (y="%")
Upvotes: 1