rggplot2r-sfgeom-vlinegeom-hline

When geom_vline is used, meridional lines cannot extend to the border of the figure under a non-WGS 84 projection


I want the grid lines to be displayed on top of the picture, so I tried to use geom_hline/geom_vline to do this, but it turned out that the vertical lines do not extend to the northern border of the picture. Is there anything I can do to improve?

Here is the code and output picture.

library("sf")
library("rnaturalearth")
library("tidyverse")

world <- ne_countries(scale = "medium", returnclass = "sf")
usa = filter(world,admin =="United States of America")

# Plot
ggplot() +
  geom_sf(data = usa, fill = "lightblue",color = "black",alpha=.9) +
  coord_sf(
    xlim = c(-119, -74),
    ylim = c(22, 51),
    default_crs = sf::st_crs(4326),
    crs = st_crs("ESRI:102003"),
    expand = TRUE,
    lims_method = "box",
    label_axes = list(
      bottom = "E", top = "E",
      left = "N", right = "N"
    )) +
  xlab(NULL) + ylab(NULL) +
  ### adding grid lines on the top
  geom_hline(yintercept = seq(0, 90, by=5), 
             color = "blue", linetype = "solid", size = 0.5) +
  geom_vline(xintercept=seq(-180, 0, by=10),
             color = "blue", linetype = "solid", size = 0.5         )+ 
  theme_bw()+
  theme( 
    panel.grid=element_blank(),
    plot.background = element_rect(fill = NA, color = NA),
    panel.background = element_rect(fill = "grey"),
    axis.text = element_text(size = 12 ),
)

enter image description here


Solution

  • You have set the y limit of the co-ordinate system to not draw anything above 51 degrees of latitude. This includes geom_vline, which will not draw anything beyond the co-ordinate limits.

    What can be drawn beyond the co-ordinate limits is the panel grid. Of course, this will be plotted under your map. However, if the grid is in the same position, color and width as your hlines and vlines, then the lines will appear to be drawn over the map and continued to the edge of the map.

    ggplot() +
      geom_sf(data = usa, fill = "lightblue",color = "black",alpha=.9) +
      coord_sf(
        xlim = c(-119, -74),
        ylim = c(22, 51),
        default_crs = sf::st_crs(4326),
        crs = st_crs("ESRI:102003"),
        expand = TRUE,
        lims_method = "box",
        label_axes = list(
          bottom = "E", top = "E",
          left = "N", right = "N"
        )) +
      xlab(NULL) + ylab(NULL) +
    
      geom_hline(yintercept = seq(0, 90, by=5), 
                 color = "blue", linetype = "solid", size = 0.5) +
      geom_vline(xintercept=seq(-180, 0, by=10),
                 color = "blue", linetype = "solid", size = 0.5)+ 
      theme_bw()+
      theme( 
        panel.grid = element_line(colour = 'blue', size = 0.5),
        plot.background = element_rect(fill = NA, color = NA),
        panel.background = element_rect(fill = "grey"),
        axis.text = element_text(size = 12 ),
      )
    

    enter image description here