so I am pretty close to getting the plot I want as shown below, but what I would like to do is to add in an interaction for sex and vaccine. Vaccine is a factor variable, i.e. “True” or “False”. I would still like the age groups to be split out as such but I would like the vaccine split outs to be stacked for each gender’s bar.
death_summary = data %>%
filter(covidDeath == "True") %>%
group_by(age_group, sex, vaccine) %>%
summarise(death_num = n(), .groups = "drop")
ggplot(death_summary, aes(x = age_group, y = death_num, fill = sex)) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = c("F" = "lightpink", "M" = "lightblue")) +
labs(title = "Distribution of COVID-19 Deaths by Age Group, Gender and Vaccine Status",
x = "Age Group",
y = "Number of Deaths",
fill = "Gender") +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5), legend.position = "bottom")
This is what I tried but it splits out into two graphs for male and female
ggplot(death_summary, aes(x = age_group, y = death_num, fill = vaccine)) +
geom_bar(stat = "identity", position = position_stack(vjust = 0.5)) +
facet_wrap(~sex) +
scale_fill_manual(values = c("True" = "lightgreen", "False" = "lightcoral")) +
labs(title = "Distribution of COVID-19 Deaths by Age Group, Gender, and Vaccination Status",
x = "Age Group",
y = "Number of Deaths",
fill = "Vaccination Status") +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5), legend.position = "bottom")
enter image description here
New contributor
Amanda Ooi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.