I am trying to plot the coefficients of some regressions. I have a dataset with the coefficients and some variables on the characteristics of the model (the data is included at the end of the post).
I want to plot the coefficients using facet_grid, so that I can plot the actual effect next to a placebo (columns) and so that the effects are split into "types" of outcomes (rows). The problem is that I also want to print the value of the coefficients of each model. My desired output is something like what is below (except that without the color aesthetic), for each facet:
I can't make it so that each coefficient is printed next to the point that represents them. Here's what I have tried:
# Load necessary libraries
library(tidyverse)
anotate <- main2$coef
# Generate annotations in the plot
dat_text <- data.frame(
label = anotate,
cyl = c(4, 6, 8))
# Make the plot
main_plot <- main2 %>%
ggplot(aes(x = Model, y = coef)) +
geom_hline(aes(yintercept = 0), linetype = 2) +
geom_linerange(aes(ymin = ci_lower, ymax = ci_upper),
position = position_dodge(width = 0.75) ) +
geom_point(size = 1, position = position_dodge(width = 0.75) ) +
facet_grid(Model_type ~ var, scale = 'free') +
theme_minimal(base_size = 9) +
coord_flip(ylim = c(-0.6, 0.6)) +
theme(panel.background = element_rect(fill = NA, color = "black")) +
guides(color = FALSE, shape = guide_legend(reverse = TRUE)) +
geom_text(
data = dat_text,
mapping = aes(x = -Inf, y = -Inf, label = label),
hjust = -0.1,
vjust = -1)
main_plot
My data:
# Generate dataset
main2 <- structure(list(Model = structure(c(9L, 8L, 7L, 6L, 5L, 4L, 1L,
3L, 2L, 9L, 8L, 7L, 6L, 5L, 4L, 1L, 3L, 2L),
.Label = c("Satisfaction w/ democracy",
"Satisfaction w/ economy", "Satisfaction w/ government", "Trust in parliament",
"Trust in parties", "Trust in politicians", "Trust in UN", "Stf elite performance (PCA)",
"Trust in elites (PCA)"),
class = "factor"),
coef = c(-0.044688936, -0.046109919, -0.017885279, -0.04640542, -0.030446326,
-0.058590218, -0.028425576, -0.024230592, -0.073079899, -0.38493791,
-0.30536923, -0.025754517, -0.35433629, -0.38493976, -0.27348521,
-0.23950855, -0.20876083, -0.22473606),
var = c("Placebo using median date",
"Placebo using median date", "Placebo using median date", "Placebo using median date",
"Placebo using median date", "Placebo using median date", "Placebo using median date",
"Placebo using median date", "Placebo using median date", "Effect of the protest",
"Effect of the protest", "Effect of the protest", "Effect of the protest",
"Effect of the protest", "Effect of the protest", "Effect of the protest",
"Effect of the protest", "Effect of the protest"),
Model_type = structure(c(2L, 1L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 3L,
2L, 2L, 2L, 1L, 1L, 1L),
.Label = c("Satisfaction with elite performance",
"Trust in elites", "Placebo"),
class = "factor"),
ci_lower = c(-0.13866682, -0.14119312, -0.11960363, -0.13918212, -0.12342494,
-0.15370362, -0.12298683, -0.1166563, -0.16764188, -0.54483932,
-0.47237837, -0.24132778, -0.50503796, -0.53971964, -0.45066974,
-0.41685063, -0.37277633, -0.38359925),
ci_upper = c(0.049288955, 0.048973277, 0.083833076,
0.046371285, 0.062532283, 0.036523174, 0.066135675, 0.068195112,
0.02148208, -0.2250365, -0.13836008, 0.18981874, -0.20363465,
-0.23015988, -0.096300691, -0.062166482, -0.044745326, -0.065872885)),
class = "data.frame", row.names = c(NA, -18L))
However, doing this prints all coefficients in each facet on top of one another. How can I make the plot such that the coefficient for each model is printed next to the point that represents it?

