I am creating a diverging bar plot based on code in this tutorial (https://rfortherestofus.com/2021/10/diverging-bar-chart/) and it all looks great, except that I want to compare by year (2021 vs. 2023). Here is an example of what I was able to produce with the two years combined (which is useless to me).
I have managed to create two separate diverging plots and use plot_grid() in cowplot to combine them in one figure. I would be okay with somehow nudging the plots so that they align at the middle (the line that separates agree/disagree), but what I would really like is for the bars for 2021 and 2023 to be interspersed so that they're all in one plot.
I can't share the raw data for ethical reasons, but here is my code based on the link I shared. This is for the two years combined.
support <- edi %>%
dplyr::select(role, supportive, year)
#a dataframe that only includes role, year, and supportive column. Adding the "definitions" of supportive column
support <- support %>%
mutate(define = case_when(
supportive == "1" ~ "Strongly Disagree",
supportive == "2" ~ "Disagree",
supportive == "3" ~ "Neutral",
supportive == "4" ~ "Agree",
supportive == "5" ~ "Strongly Agree"
))
#creating a summary of the data with counts per choice and per role, and percents showing as decimals and as %
support_sum <- support %>%
group_by(role, define) %>%
count(name = "n_support") %>%
group_by(role) %>%
mutate(percent_support = n_support / sum(n_support)) %>%
ungroup() %>%
mutate(percent_support_labels = percent(percent_support, accuracy = 1))
support_sum
#set up for divergence - basically telling R that everything below Strongly Agree and Agree is negative and adds a "-" before the number
support_sum_div <- support_sum %>%
mutate(percent_support = if_else(define %in% c("Strongly Agree", "Agree"), percent_support, -percent_support)) %>%
mutate(percent_support_labels = percent(percent_support, accuracy = 1))
support_sum_div
#change so that labels don't come up as negative
support_sum_div_labs <- support_sum_div %>%
mutate(percent_support_labels = abs(percent_support)) %>%
mutate(percent_support_labels = percent(percent_support_labels, accuracy = 1))
support_sum_div_labs
#reorder bars, put neutral at the very end
support_div_ordered <- support_sum_div_labs %>%
mutate(define = fct_relevel(define,
"Netural", "Strongly Disagree", "Disagree", "Agree", "Strongly Agree"),
define = fct_rev(define))
support_div_ordered
support_div_ordered %>%
ggplot(aes(x = role,
y = percent_support,
fill = define)) +
geom_col() +
geom_text(aes(label = percent_support_labels),
position = position_stack(vjust = 0.5),
color = "white",
fontface = "bold") +
coord_flip() +
scale_x_discrete() +
scale_fill_manual(breaks = c("Neutral", "Strongly Disagree", "Disagree", "Agree", "Strongly Agree"),
values = c(
"Neutral" = "gold",
"Strongly Disagree" = "slateblue",
"Disagree" = "plum",
"Agree" = "#41b6c4",
"Strongly Agree" = "dodgerblue4"
)) +
labs(title = "People are supportive of each other.",
x = NULL,
fill = NULL) +
theme_minimal() +
theme(axis.text.x = element_blank(),
axis.title.x = element_blank(),
panel.grid = element_blank(),
legend.position = "top")
To create the plot that I attached, I simply took this code and subset the data into 2021 and 2023, then combined with cowplot.
plot_grid(support2023.plot,
support2021.plot)+
coord_flip()