10

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)
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Dambo
  • 3,318
  • 5
  • 30
  • 79
  • 3
    From what I understand, it was a [deliberate design decision](https://github.com/tidyverse/ggplot2/issues/1371) for `alpha` to affect `fill` but not `colour` aesthetics in geoms with both. It **is** possible to rewrite your own geom with a different behaviour in `GeomSf`'s `draw_panel` function, but I wouldn't recommend it. When you have adjacent polygons, the overlapping lines will have a higher cumulative alpha than the lines at the outer boundaries. – Z.Lin Feb 18 '19 at 05:58

2 Answers2

13

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

Nakx
  • 1,460
  • 1
  • 23
  • 32
0

Also alpha, can be clashing with other packages, i would recommend to specify which package you are using. In my case I had to

color = scales::alpha('#c55252', 0.3)

Jota
  • 11
  • 3