rggplot2bordershadinggeom-sf

How to completely get rid of borders in geom_sf


How can I completely get rid of borders using geom_sf? (The grids were created with st_make_grid.) I have tried color=NA and size=0, but neither do what I want.

ggplot() +
    geom_sf(data = plot_data, aes(fill = as.vector(dist)), color=NA, size=0) + 
    scale_fill_viridis(discrete=FALSE, name="", direction=-1) +
    geom_sf(data = states, fill=alpha("black", 0)) +
    theme_bw()

Example:

enter image description here

This relates to: How can I remove border lines when using geom_sf?.


Solution

  • This appears to be somewhat device dependent. I can reproduce your problem on my machine like this:

    library(sf)
    library(tidyverse)
    
    states <- st_as_sf(maps::map("state", fill = TRUE, plot = FALSE)) %>%
      filter(ID %in% c("kansas", "oklahoma", "missouri", "arkansas")) %>%
      mutate(geom = st_crop(geom, c(xmin = -96.5, xmax = -94, 
                                    ymin = 35, ymax = 37.25)))
    
    plot_data <- st_make_grid(states, n = 80) %>%
      st_as_sf() %>%
      mutate(geom = x, 
             dist = st_distance(geom, 
                      st_as_sfc(list(st_point(c(-96, 36))), crs = "WGS84")))
    
    ggplot() +
      geom_sf(data = plot_data, aes(fill = as.vector(dist)), size = 0, color = NA) + 
      scale_fill_viridis_c(name = "", direction = -1) +
      geom_sf(data = states, fill = alpha("black", 0), linewidth = 2) +
      theme_bw() 
    

    enter image description here

    The easiest way to fix this is, rather than trying to make the lines disappear, simply color them the same as the grid boxes.

    ggplot() +
      geom_sf(data = plot_data, aes(fill = as.vector(dist),
                                    color = after_scale(fill)), linewidth = 1) + 
      scale_fill_viridis_c(name = "", direction=-1) +
      geom_sf(data = states, fill = alpha("black", 0), linewidth = 2) +
      theme_bw() 
    

    enter image description here