data.table + sf integration with ggplot2 regression

Open
#6,707 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r

Research direction

Start by running the reproducible R example in the issue and compare the plotted extent with and without coord_sf(). Investigate the data.table result containing the grouped st_union geometries and its interaction with ggplot2's geom_sf(); done means both regions render with the full extent without requiring an explicit coord_sf() bounding box, with a regression test for the example.

Written by the indexing model from the issue text.

Description

regression
          A related issue AFAICT is the `data.table` + `sf` integration with `ggplot2`. I've just discovered this as I was trying to replicate an old [blog post](https://grantmcdermott.com/fast-geospatial-datatable-geos/) of mine and was surprised to find that the following example no longer works:
library(sf)
#> Linking to GEOS 3.13.0, GDAL 3.10.0, PROJ 9.5.1; sf_use_s2() is TRUE
library(data.table)
library(ggplot2)
theme_set(theme_minimal())

nc = st_read(system.file("shape/nc.shp", package = "sf"))
#> Reading layer `nc' from data source `/usr/lib/R/library/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27
nc_dt = as.data.table(nc)

nc_dt[
  ,
  .(geometry = st_union(geometry)), 
  by = .(region = ifelse(CNTY_ID<=1980, 'high', 'low'))
]  |> 
  ggplot(aes(geometry=geometry, fill=region)) + 
  geom_sf()

As you can see, the plot extent has been cut off at the bounding box of the first row in the data.table (corresponding to the "high" region).

I can fix the problem by explicitly passing the original bounding box extent to a coord_sf() layer, but this is obviously more of a pain.

nc_dt[
  ,
  .(geometry = st_union(geometry)), 
  by = .(region = ifelse(CNTY_ID<=1980, 'high', 'low'))
]  |> 
  ggplot(aes(geometry=geometry, fill=region)) + 
  geom_sf() +
  coord_sf(xlim = st_bbox(nc)[c(1,3)], ylim = st_bbox(nc)[c(2,4)])

Created on 2025-01-03 with reprex v2.1.1

Originally posted by @grantmcdermott in https://github.com/Rdatatable/data.table/issues/5352#issuecomment-2569901267

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.