# Re-running the pivot if needed
viva_anx_ls_layered <- viva %>%
select(Sex, ANXavg, LSavg) %>%
pivot_longer(
cols = c(ANXavg, LSavg),
names_to = "Var_Type",
values_to = "Score"
) %>%
mutate(Var_Type = recode(Var_Type, "ANXavg" = "Anxiety", "LSavg" = "Loneliness"),
Sex_Num = ifelse(Sex == "Men", 1, 1.4))
ggplot(viva_anx_ls_layered, aes(x = Sex_Num, y = Score, fill = interaction(Sex, Var_Type))) +
# 1. The Clouds
stat_halfeye(
adjust = .5, width = .6, .width = 0,
justification = 0, point_colour = NA, alpha = 0.6,
position = position_dodge(width = .15)
) +
# 2. The Boxplots
geom_boxplot(
width = .08, outlier.shape = NA, alpha = 0.6,
position = position_dodge(width = .15)
) +
# 3. The Rain
geom_jitter(
aes(color = interaction(Sex, Var_Type)),
alpha = 0.2, size = 0.7,
position = position_jitterdodge(jitter.width = 0.05, dodge.width = 0.15)
) +
# Colors
scale_fill_manual(values = c(
"Men.Anxiety" = "steelblue4", "Men.Loneliness" = "lightsteelblue3",
"Women.Anxiety" = "maroon4", "Women.Loneliness" = "thistle"
)) +
scale_color_manual(values = c(
"Men.Anxiety" = "steelblue4", "Men.Loneliness" = "lightsteelblue3",
"Women.Anxiety" = "maroon4", "Women.Loneliness" = "thistle"
)) +
annotate("text", x = 1.65, y = 4.3, label = "— Anxiety (W)", color = "maroon4", fontface = "bold", size = 4.4, hjust = 0) +
annotate("text", x = 1.58, y = 4.3, label = "— Loneliness (W)", color = "thistle4", fontface = "bold", size = 4.4, hjust = 0) +
# Men's Legend
annotate("text", x = 1.25, y = 4.3, label = "— Anxiety (M)", color = "steelblue4", fontface = "bold", size = 4.4, hjust = 0) +
annotate("text", x = 1.18, y = 4.3, label = "— Loneliness (M)", color = "lightsteelblue4", fontface = "bold", size = 4.4, hjust = 0) +
# THE SQUEEZE: xlim starts at 0.9 to cut off space below Men (who are at 1.0)
coord_flip(ylim = c(1, 5), xlim = c(0.9, 1.75), clip = "off") +
scale_x_continuous(
breaks = c(1, 1.4),
labels = c("Men", "Women"),
expand = c(0, 0)
) +
labs(x = NULL, y = NULL) +
theme_minimal() +
theme(
legend.position = "none",
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank(),
axis.text.y = element_text(face = "bold", size = 12),
axis.text.x = element_text(size = 12),
plot.margin = margin(t = 2, r = 10, b = 2, l = 5)
)