When I use the below code and data to make a faceted coefficient plot, the y axis repeats across each category row. Is there a simple way to make it so that only the relevant y axis labels/ticks show? (I know I can do this in facet_wrap, but I'm using facet_grid to try to cluster the points/bars of different groups closer together).
ggplot(d_demog, aes(y = model, x = coef, shape = model,
xmin = ci_lower, xmax = ci_upper)) +
geom_errorbarh(aes(xmin = ci_lower, xmax = ci_upper),
position = position_dodge(.5),
linewidth = .8,
height = 0,
alpha = 0.5) +
geom_point(size = 3.5, fill = "white", stroke = 2) +
geom_vline(xintercept = 0,
linetype = "dashed",
color = "black",
linewidth = .3) +
labs(x = "Coefficient",
y = NULL) +
scale_y_discrete(limits = d_demog$model) +
scale_x_continuous(breaks = seq(-0.5, 0.2, 0.1)) +
coord_cartesian(xlim = c(-0.55, 0.21)) +
scale_shape_manual(values = c("CohortA" = 23,
"CohortB" = 23,
"Female" = 21,
"Male" = 21,
"White" = 25,
"Hispanic" = 25,
"Black" = 25)) +
facet_grid(rows = vars(group),
scales = "free_y",
space = "free") +
theme_minimal(base_size = 15) +
theme(legend.position = "none",
panel.grid = element_blank(),
strip.text = element_blank(),
strip.background = element_blank())
structure(list(var = structure(c("absent", "absent",
"absent", "absent", "absent",
"absent", "absent"), label = "Variable", format.stata = "%21s"),
coef = structure(c(-0.221818581223488, -0.250119477510452,
-0.218866005539894, -0.266342610120773, -0.192735567688942,
-0.248423606157303, -0.181893035769463), label = "Coefficient", format.stata = "%9.0g"),
stderr = structure(c(0.0679426342248917, 0.0690209195017815,
0.0712617486715317, 0.0640746206045151, 0.149574533104897,
0.0786021128296852, 0.0709330886602402), label = "Standard error", format.stata = "%9.0g"),
ci_lower = structure(c(-0.355985969305038, -0.386303544044495,
-0.359476625919342, -0.392865926027298, -0.491636425256729,
-0.403717339038849, -0.322694092988968), label = "95% confidence interval (lower bound)", format.stata = "%9.0g"),
ci_upper = structure(c(-0.0876511856913567, -0.113935396075249,
-0.0782553851604462, -0.139819279313087, 0.106165274977684,
-0.093129888176918, -0.0410919710993767), label = "95% confidence interval (upper bound)", format.stata = "%9.0g"),
N = structure(c(185, 205, 204, 186, 84, 173, 117), label = "Number of observations", format.stata = "%10.0g"),
model = structure(c("CohortA", "CohortB", "Female", "Male",
"White", "Hispanic", "Black"), label = "Model name", format.stata = "%9s"),
group = c("Cohort", "Cohort", "Gender", "Gender", "Race",
"Race", "Race")), row.names = c(NA, -7L), class = c("tbl_df",
"tbl", "data.frame"))
Your problem is that you're hard-coding the y-axis scale (scale_y_discrete
). A much more minimal example (try to make your examples more like this — it makes it much easier to look for/spot problems ...)
g0 <- ggplot(d_demog, aes(y = model, x = coef)) + geom_point()
g1 <- g0 + facet_grid(rows = vars(group), scales = "free_y", space = "free")
print(g1) ## OK
g1 + scale_y_discrete(limits = d_demog$model) ## not OK
If you want to enforce a particular ordering of the labels/categories you probably need to do this by setting the levels of the model
factor appropriately ...