I have a following ggplot2
R code that creates state-specific plot using geofacet
package:
ggplot(plotdf, aes(x = year, y = value)) +
geom_line(aes(color = group1), linewidth = 0.6) +
geom_hline(yintercept = 0, lty = 2) +
facet_geo(~ state, grid = 'us_state_without_DC_grid1', scales = 'free_y', move_axes = TRUE) +
scale_color_manual(name = "", values = c('black', 'darkgreen', 'darkviolet','blue')) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5),
panel.grid = element_blank(),
axis.text.x = element_text(angle = 90),
legend.position = 'bottom')
which produces the following plot that includes y-axis text even in empty plot areas:
How can I remove the axis labels in the empty plots while asserting scales = 'free_y'
?
Below is a fake data set to reproduce a plot similar to what's shown above:
plotdf <- expand.grid(state = state.abb, year = 1990:2020, group1 = c('White', 'Black', 'Asian', 'Hispanic'))
plotdf$value <- rnorm(nrow(plotdf))
The problem here is with
geom_hline()
- it's being added to every facet plot (even empty ones) since there's no facet variable associated with the intercept value. One option is to add it to your data and specify the intercept in theaes()
function instead: