# --------------------------------------------------
# Factor levels
# --------------------------------------------------
q45_levels <- c(
"Experience",
"Performance",
"Attendance",
"Service quality",
"Skills",
"Position",
"Additional tasks",
"Work location",
"Education"
)
# --------------------------------------------------
# Prepare data
# --------------------------------------------------
plot_q45 <- raw_combined %>%
select(loc_label, q4, q5) %>%
pivot_longer(
cols = c(q4, q5),
names_to = "question",
values_to = "factor"
) %>%
filter(!is.na(factor)) %>%
mutate(
question = recode(
question,
q4 = "Most important",
q5 = "Least important"
),
factor = factor(factor, levels = q45_levels)
) %>%
count(loc_label, question, factor) %>%
group_by(loc_label, question) %>%
mutate(
pct = n / sum(n),
pct = if_else(question == "Least important", -pct, pct)
) %>%
ungroup()
# --------------------------------------------------
# Order factors by importance (based on Q4 across both locations)
# --------------------------------------------------
factor_order <- plot_q45 %>%
filter(question == "Most important") %>%
group_by(factor) %>%
summarise(pct = sum(abs(pct)), .groups = "drop") %>%
arrange(pct) %>%
pull(factor)
plot_q45$factor <- factor(plot_q45$factor, levels = factor_order)
# --------------------------------------------------
# Construct mirrored horizontal bar chart
# --------------------------------------------------
ggplot(plot_q45, aes(x = pct, y = factor, fill = question)) +
geom_col(width = 0.7) +
facet_wrap(~ loc_label) +
scale_x_continuous(
limits = c(-0.3, 0.5),
breaks = c(-0.3, -0.1, 0, 0.1, 0.3, 0.5),
minor_breaks = seq(-0.3, 0.5, by = 0.1), # gridlines every 10%
labels = function(x) paste0(abs(x * 100), "%"),
expand = c(0, 0)
) +
scale_fill_manual(
values = c(
"Most important" = "#2166ac",
"Least important" = "#b2182b"
)
) +
labs(
title = "Most and least important criteria for incentive allocation",
subtitle = "Mirrored horizontal bar chart comparing Q4 and Q5",
x = "Percentage of respondents",
y = NULL,
fill = NULL
) +
theme_minimal(base_size = 11) +
theme(
# spacing between facets
panel.spacing.x = unit(4, "lines"),
# gridlines
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_line(
colour = "grey80",
linewidth = 0.5
),
panel.grid.minor.x = element_line(
colour = "grey90",
linewidth = 0.3
),
# axes
axis.text.y = element_text(size = 10),
axis.text.x = element_text(size = 9),
axis.ticks.x = element_blank(),
axis.line.x = element_blank(),
# facet titles
strip.text = element_text(face = "bold"),
# legend
legend.position = "bottom",
# margin
plot.margin = margin(10, 20, 10, 10)
)