pr <- avg_predictions(glmod, by = c("age", "country"),
newdata = datagrid(age = seq(18, 80, 2), country = unique, grid_type = "counterfactual"))
# Age histogram scaled to the plot (bar height of 11 = top share of the sample)
pr_hist <- function(ages, mean_age, label, color, top = .15) {
ggplot(pr, aes(x = age)) +
geom_histogram(data = filter(ages, age <= 80),
aes(y = after_stat(11 * density / top)), binwidth = 1, fill = NA, color = color) +
geom_ribbon(aes(ymin = conf.low, ymax = conf.high, fill = country), alpha = .2) +
geom_line(aes(y = estimate, color = country)) +
geom_vline(xintercept = mean_age, linetype = "dashed", color = color) +
annotate("text", x = mean_age + 1, y = 8.5, label = label, hjust = 0,
fontface = "italic", color = color) +
scale_x_continuous(breaks = seq(20, 80, 10)) +
scale_y_continuous(breaks = 0:10) +
coord_cartesian(ylim = c(-.5, 10)) +
labs(x = "Age", y = "Acceptance", color = NULL, fill = NULL)
}
pr_hist(gn, 40.89, "Mean of age\nin pooled sample", "gray50") +
labs(title = "[7.10.d] Acceptance of same-sex relationship by age - Germany and Nigeria (WVS6)",
subtitle = "Distribution of age pooling across countries shown")