ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

geom_sf fill missing when xlim,ylim set in coord_sf

Open jonoyuan opened this issue 7 years ago • 7 comments

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")

image

jonoyuan avatar May 01 '19 01:05 jonoyuan

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)

clauswilke avatar May 01 '19 03:05 clauswilke

Any workarounds for us lowly Windows users?

jonoyuan avatar May 01 '19 12:05 jonoyuan

Use a cairo device (png(..., type = 'cairo'))

thomasp85 avatar May 01 '19 12:05 thomasp85

@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

file

Created on 2019-05-01 by the reprex package (v0.2.1)

jonoyuan avatar May 01 '19 13:05 jonoyuan

@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.

jacobgellman avatar Sep 04 '19 13:09 jacobgellman

The solution was to not use the default windows device. clip = “off” was just used to show the effect of it

thomasp85 avatar Sep 04 '19 16:09 thomasp85

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/

lock[bot] avatar Mar 10 '20 18:03 lock[bot]