I have the following code which produces the map below. As you can see, the map correctly overlays two sets of polygons:

  • A large polygon outlining the city of Liverpool
  • A group of polygons outlining the 'MSOA' census components of the city

I'm happy with how it's outputting up to now, but I'd like to change the border opacity of the MSOA polygons so they are less bold and in-your-face. How can I do this?

The MSOA polygons are produced using the last line of the following code 'LiverpoolMSOA_LL'

ggmap(BaseMap) + 
  stat_density2d(data=CrimeDatabase,aes(x=long, y=lat, fill= ..level.., alpha=..level..), size=1, bins=200, geom="polygon") +
  scale_fill_gradient(low="green",high="red") +
  scale_alpha(range=c(0.05,0.7), guide=FALSE) +
  geom_density2d(data=CrimeDatabase,aes(x=long, y=lat), size=0.3, bins=20, alpha=0.4) +  
  # Main CIty Polygon #
  geom_polygon(data = Liverpool_LL, aes(x=long, y=lat, group=group), colour="black", fill=NA) +
  # MSOA Polygons - to have border opacity altered #
  geom_polygon(data = LiverpoolMSOA_LL, aes(x=long, y=lat, group=group), colour="black", fill=NA)

I have tried to use alpha=0.2 in both the aes() and after 'fill=NA' but this has not worked.

I have also tried rendering these polygons before the heatmap, but I'm not happy with the clarity of the borders so would prefer them to be on top.

ggmap showing polygons overlayed

  • 1
    Use geom_path instead of geom_polygon – rcs Mar 27 '17 at 15:29
  • That sorted it, cheers (y) – Cobain Mar 28 '17 at 12:47
  • If you're stuck with geom_polygon because you want a fill, you could go with a dark grey color instead of black. Not the same as transparency, but the effect would be similar. – Gregory Sep 10 '19 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.