Skip to content

Support sf objects for maps? #210

Open

Description

@etiennebacher

Hi @grantmcdermott, @vincentarelbundock and @zeileis, I'm just wondering whether support for sf objects is on the roadmap. ggplot2 can be quite slow when drawing maps with several elements (say all the US counties + fill by variable + points). To clarify, creating the map is not slow but printing it can easily take 15-20 seconds or more and I'm wondering if a base plot would be faster for this.

A complete support would probably be complicated, but here's a very simple example:

library(ggplot2)
library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

ggplot(nc) +
  geom_sf(aes(fill = AREA))

nc[, "AREA", drop = FALSE] |> 
  plot()

Being able to do plt(~ AREA, fill = "by", type = "map") would be great (contrarily to the syntax for density plots, we'd only need to have one variable in the formula here).

Activity

  1. changed the title [-]Support for `sf` objects for maps?[/-] [+]Support `sf` objects for maps?[/+] on Aug 30, 2024
  2. etiennebacher commented on Aug 30, 2024

    @etiennebacher
    ContributorAuthor

    Just to clarify, in the end tinyplot would call sf:::plot.sf

  3. grantmcdermott commented on Aug 30, 2024

    @grantmcdermott
    Owner

    Native sf support is something I'd love to support. See #159 (comment) for a proof of concept.

    One annoyance we'd have to figure out is color scaling for continuous grouping variables, since polypaths aren't currently compatible with our gradient legend logic. I have an idea for a workaround, but won't have time to look into this properly for the next few weeks.

    Thanks for putting it on my (our!) radar again.

  4. edzer commented on Feb 24, 2025

    @edzer

    Happy to help here, let me know!

  5. grantmcdermott commented on Feb 25, 2025

    @grantmcdermott
    Owner

    Thanks @edzer, I'm hoping to dive back into this in a few weeks.

  6. rsbivand commented on Feb 25, 2025

    @rsbivand

    @grantmcdermott Interesting! Note that mapsf is also base graphics. Further, classInt https://cran.r-project.org/package=classInt https://r-spatial.github.io/classInt/ is used by sf, tmap and mapsf and others for typically cartographic assignment of classes to continuous variables - mapsf has a couple of styles extending those in classInt. If need be, classInt could be modernised (originally written after a workshop almost 20 years ago). Might it help wrt. the annoyance you raise? Could I help?

  7. rsbivand commented on Feb 25, 2025

    @rsbivand
  8. grantmcdermott commented on Jul 12, 2025

    @grantmcdermott
    Owner

    I still hope to add sf support here (and #433 will be an important step towards that)... But I recently learned about mapsf, which looks like a fantastic option for base graphics maps: https://riatelab.github.io/mapsf/#/

  9. rCarto commented on Jul 15, 2025

    @rCarto

    I'm glad to hear that :-) I appreciate the "tiny" approach. Happy to help or discuss mapsf features if you'd like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions