Dan
Dan

Reputation: 12084

geom_sf ignores fill variable when plotting a shapefile

I want to plot a shapefile using geom_sf and to fill the polygons that are plotted with a certain colour. Below, I download the data and plot it, attempting to colour the outlines in red and fill in black.

# Download data
download.file("http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/physical/ne_10m_coastline.zip", 
              destfile = 'coastlines.zip')

# Unzip
unzip(zipfile = "coastlines.zip", 
      exdir = 'ne-coastlines-10m')

# Load packages
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, PROJ 4.9.3
library(ggplot2)

# Load shape file
shp <- read_sf("ne-coastlines-10m/ne_10m_coastline.shp")

# Plot map
ggplot(shp) + 
  geom_sf(fill = "black", colour = "red") + 
  theme(panel.background = element_blank())

Created on 2019-09-12 by the reprex package (v0.3.0)

The outline of the geometry is plotted correctly (i.e., in red), but there is no fill. Documentation (e.g.) shows fill being used by geom_sf, so it's unclear to me why the polygons are not filled in my example. Any ideas?

Upvotes: 2

Views: 1288

Answers (1)

marine-ecologist
marine-ecologist

Reputation: 182

Below, I download the data and plot it, attempting to colour the outlines in red and fill in black.

The outline of the geometry is plotted correctly (i.e., in red), but there is no fill.

A solution is to combine the outputs from both countries and coastlines datasets from natural earth:

# Download data
download.file("http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/physical/ne_10m_coastline.zip", 
              destfile = 'coastlines.zip')

download.file("https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/cultural/ne_10m_admin_0_countries.zip", 
              destfile = 'countries.zip')

# Unzip
unzip(zipfile = "coastlines.zip", 
      exdir = 'ne-coastlines-10m')

unzip(zipfile = "countries.zip", 
      exdir = 'ne_10m_admin_0_countries')

# Load packages
library(sf)
library(ggplot2)

# Load shape file
shp_coastline <- read_sf("ne-coastlines-10m/ne_10m_coastline.shp")
shp_countries <- read_sf("ne_10m_admin_0_countries/ne_10m_admin_0_countries.shp")

# Plot map
ggplot(shp) + 
  geom_sf(data=shp_countries, fill = "black", color="black") + 
  geom_sf(data=shp_coastline, colour = "red") +
  theme(panel.background = element_blank())

Which gives this:

enter image description here

If you're using the natural earth datasets frequently, library(rnaturalearth) is a good option to extract the sf files directly:


# Load packages
library(sf)
library(ggplot2)
library(rnaturalearth)

# Load shape file
shp_coastline <- ne_coastline(scale = "medium", returnclass = "sf") 
shp_countries <- ne_countries(scale = "medium", returnclass = "sf") 

# Plot map
ggplot(shp) + 
  geom_sf(data=shp_countries, fill = "black", color="black") + 
  geom_sf(data=shp_coastline, colour = "red") +
  theme(panel.background = element_blank())

Upvotes: 1

Related Questions