Search code examples
rggplot2r-sf

How to change alpha in geom_sf?


Is there a way to change alpha with geom_sf? This example is from the examples in ?geom_sf. I tried adding alpha=.2 but it seems to ignore that aesthetic, although alpha is an accepted aesthetic for geom_line. It does not ignore alpha for the fill - which in this example is NA though.

library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
library(ggplot2)

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
nc_3857 <- sf::st_transform(nc, "+init=epsg:3857")

ggplot() + geom_sf(data = nc) + 
  geom_sf(data = nc_3857, colour = "red", fill = NA, alpha = 0.2)

Solution

  • It's called an outline. I never worked with maps and the result is not quite pretty but I hope this is still helpful and this could help you more: ggplot2: different alpha values for border and filling of geom_point

    library(sf)
    #> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
    library(ggplot2)
    
    nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
    nc_3857 <- sf::st_transform(nc, "+init=epsg:3857")
    
    ggplot() + geom_sf(data = nc) + 
      geom_sf(data = nc_3857, color=alpha("red",0.2))
    

    enter image description here