geom_sf fill missing when xlim,ylim set in coord_sf
When plotting polygons with a fill, setting xlim and ylim using coord_sf() will cause features outside of the plot area to lose their fills. Setting clip = "off" shows that the edge of the entire image (not just the clip/plot area) seems to cut the polygons without closing them.
tracts <- read_sf("https://opendata.arcgis.com/datasets/230f9d23f6874be983901876cd600c4e_0.geojson") %>%
select(geometry)
ggplot() +
geom_sf(data = tracts,
color = "red",
fill = "red",
alpha = 0.5) +
coord_sf(ylim = c( 43.04, 43.07),
xlim = c(-76.14,-76.10),
clip = "off")

This is the fault of whichever graphics device you're using (maybe default Windows device?). It works correctly on OS X with Quartz.
library(dplyr)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, PROJ 4.9.3
library(ggplot2)
tracts <- read_sf("https://opendata.arcgis.com/datasets/230f9d23f6874be983901876cd600c4e_0.geojson") %>%
select(geometry)
# without clipping
ggplot() +
geom_sf(data = tracts,
color = "red",
fill = "red",
alpha = 0.5) +
coord_sf(ylim = c( 43.04, 43.07),
xlim = c(-76.14,-76.10),
clip = "off")

# with clipping
ggplot() +
geom_sf(data = tracts,
color = "red",
fill = "red",
alpha = 0.5) +
coord_sf(ylim = c( 43.04, 43.07),
xlim = c(-76.14,-76.10))

Created on 2019-04-30 by the reprex package (v0.2.1)
Any workarounds for us lowly Windows users?
Use a cairo device (png(..., type = 'cairo'))
@thomasp85 Thanks.
This works in Windows:
library(dplyr)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(ggplot2)
tracts <- read_sf("https://opendata.arcgis.com/datasets/230f9d23f6874be983901876cd600c4e_0.geojson") %>%
select(geometry)
png(filename = "file.png",
width = 672, height = 384,
type = "cairo")
ggplot() +
geom_sf(data = tracts,
color = "red",
fill = "red",
alpha = 0.5) +
coord_sf(ylim = c( 43.04, 43.07),
xlim = c(-76.14,-76.10),
clip = "off")
dev.off()
#> png
#> 2

Created on 2019-05-01 by the reprex package (v0.2.1)
@jonoyuan @thomasp85 Do you really see this as a solution? What actually restored the fill was the clip="off" option. But then you haven't really limited your window at all. The shapes beyond ylim = c( 43.04, 43.07), xlim = c(-76.14,-76.10) are still present, when the goal was to limit the graphic to that small window.
The solution was to not use the default windows device. clip = “off” was just used to show the effect of it
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/