Search code examples
rggplot2shapefileaestheticsgeom-sf

How to change polygon colors to white when plotting a shapefile with ggplot2


I want to plot the polygons of this shapefile without the colors (just white with black borders); I also do not want the legend. I have tried scale_fill_manual and a few other things but to no avail. I would also like to include the labels (i.e. 901, 902 ....943) within each polygon. Any assistance would be greatly appreciated.

statsf2134 %>%
 ggplot()+
  geom_sf(aes(fill=Statistica, border = "black"))

plot


Solution

  • As already suggested by @jdobres in the comments set your desired fill color as a parameter outside of aes() instead of mapping a variable on the fill aesthetic inside aes(). Also note that, there is no border aesthetic or parameter. If you want to switch the border color use the color= parameter. Finally, you could add labels at the center of each polygon using geom_sf_text.

    Using some fake example data based on the default example from ?geom_sf:

    library(ggplot2)
    
    # Example data
    statsf2134 <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
    statsf2134$Statistica <- substr(statsf2134$FIPS, 3, 5)
    
    ggplot(statsf2134) +
      geom_sf(fill = "white") +
      geom_sf_text(
        aes(label = Statistica),
        size = 6 / .pt
      )
    #> Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
    #> give correct results for longitude/latitude data