Search code examples
rggplot2scaleusmapgeofacet

geom_facet with scales = 'free_y' generates y-axis texts in empty plots


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: enter image description here

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))

Solution

  • 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 the aes() function instead:

    plotdf <- expand.grid(state = state.abb, year = 1990:2020, group1 = c('White', 'Black', 'Asian', 'Hispanic'))
    plotdf$value <- rnorm(nrow(plotdf))
    plotdf$intercept <- 0
    
    ggplot(plotdf, aes(x = year, y = value)) + 
      geom_line(aes(color = group1), linewidth = 0.6) +
      geom_hline(aes(yintercept = intercept),
                 lwd = 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')
    

    facet plot