Skip to content

geom_sf is appallingly slow in some cases #2655

Closed

Description

@thk686

Literally getting questions like "why should I use this software" from students when it takes 5 minutes to draw a map. Here's a specific example from a lesson. While it is always a bit slow, some plots take forever. Could it be because of the 'fill' argument?

con <- url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_PAK_3_sf.rds")
pakistan.gadm <- readRDS(con) %>%
  st_transform("+proj=laea +lat_0=31 +lon_0=69 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs")
ggplot() +
  geom_sf(data = st_simplify(pakistan.gadm, 1e4), aes(fill = NAME_3)) +
  guides(fill = FALSE)

Activity

  1. batpigandme commented on May 24, 2018

    @batpigandme
    Member

    Could you please turn this into a self-contained reprex (short for minimal reproducible example)?

    If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page.

    Thanks

  2. thk686 commented on May 24, 2018

    @thk686
    Author
    devtools::install_github("hadley/ggplot2")
    #> Using GitHub PAT from envvar GITHUB_PAT
    #> Warning in strptime(x, fmt, tz = "GMT"): unknown timezone 'zone/tz/2018c.
    #> 1.0/zoneinfo/America/Chicago'
    #> Downloading GitHub repo hadley/ggplot2@master
    #> from URL https://api.github.com/repos/hadley/ggplot2/zipball/master
    #> Installing ggplot2
    #> '/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file  \
    #>   --no-environ --no-save --no-restore --quiet CMD INSTALL  \
    #>   '/private/var/folders/j5/977488_x28x3hcjmb6p5nmgw0000gn/T/RtmphU41Uj/devtools4c3513daba4c/tidyverse-ggplot2-69dfc4b'  \
    #>   --library='/Library/Frameworks/R.framework/Versions/3.4/Resources/library'  \
    #>   --install-tests
    #> 
    library(ggplot2)
    library(sf)
    #> Warning: package 'sf' was built under R version 3.4.4
    #> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
    con <- url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_PAK_3_sf.rds")
    pakistan.gadm <- readRDS(con) %>% st_transform("+proj=laea +lat_0=31 +lon_0=69 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs")
    ggplot() + geom_sf(data = st_simplify(pakistan.gadm, 10000), aes(fill = NAME_3)) + 
      guides(fill = FALSE)
  3. batpigandme commented on May 24, 2018

    @batpigandme
    Member

    Hmm – I just took the elapsed time from before and after actually calling ggplot and it was < 4 seconds…

    library(ggplot2)
    library(sf)
    #> Linking to GEOS 3.5.0, GDAL 2.1.0, proj.4 4.8.0
    con <- url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_PAK_3_sf.rds")
    pakistan.gadm <- readRDS(con) %>% st_transform("+proj=laea +lat_0=31 +lon_0=69 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs")
    x <- Sys.time()
    ggplot() + geom_sf(data = st_simplify(pakistan.gadm, 10000), aes(fill = NAME_3)) + 
      guides(fill = FALSE)

    (Sys.time() - x)
    #> Time difference of 3.330275 secs
  4. clauswilke commented on May 24, 2018

    @clauswilke
    Member

    It takes about 90 seconds to render this on my system. fill is not at fault. And when I try to display the result in the RStudio plot window things seem even slower. In fact, this crashed my RStudio session when I first tried to run it.

    library(sf)
    #> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
    con <- url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_PAK_3_sf.rds")
    pakistan.gadm <- readRDS(con) %>%
      st_transform("+proj=laea +lat_0=31 +lon_0=69 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs")
    pakistan.simple <- st_simplify(pakistan.gadm, 1e4)
    
    library(ggplot2)
    library(microbenchmark)
    
    p1 <- ggplot() +
      geom_sf(data = pakistan.simple, aes(fill = NAME_3)) +
      guides(fill = FALSE)
    
    p2 <- ggplot() +
      geom_sf(data = pakistan.simple)
    
    microbenchmark(print(p1), print(p2), times = 1)

    #> Unit: seconds
    #>       expr      min       lq     mean   median       uq      max neval
    #>  print(p1) 91.13243 91.13243 91.13243 91.13243 91.13243 91.13243     1
    #>  print(p2) 90.76306 90.76306 90.76306 90.76306 90.76306 90.76306     1
    

    Created on 2018-05-24 by the reprex package (v0.2.0).

  5. thomasp85 commented on May 24, 2018

    @thomasp85
    Member

    While your issue may be valid, may I suggest you don’t word your next issue in ways that seem to insult the developers

  6. hadley commented on May 24, 2018

    @hadley
    Member
    sum(sapply(pakistan.simple$geometry[[122]], function(x) nrow(x[[1]])))
    [1] 67556
    

    I don't know of a good way to figure out the total number of points, but I think there are a lot

  7. hadley commented on May 24, 2018

    @hadley
    Member

    @edzer is there a simple way to total number of vertices in a multipolygon geometry?

  8. tungttnguyen commented on May 25, 2018

    @tungttnguyen

    I tried @clauswilke 's code and it took ~ 2s on my Win 10 laptop (core i5, 16GB RAM). So problem might be OS dependent?

    R> microbenchmark(print(p1), print(p2), times = 1)
    Unit: seconds
          expr    min     lq   mean median     uq    max neval
     print(p1) 2.3797 2.3797 2.3797 2.3797 2.3797 2.3797     1
     print(p2) 1.8401 1.8401 1.8401 1.8401 1.8401 1.8401     1
    
  9. clauswilke commented on May 25, 2018

    @clauswilke
    Member

    I guess we can blame the OS X Quartz device. Unfortunately it's used by R Studio and hence what people experience in interactive work. Rendering to pdf and opening in Preview takes ~2s total on the same machine.

    library(sf)
    #> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
    con <- url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_PAK_3_sf.rds")
    pakistan.gadm <- readRDS(con) %>%
      st_transform("+proj=laea +lat_0=31 +lon_0=69 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs")
    pakistan.simple <- st_simplify(pakistan.gadm, 1e4)
    
    library(ggplot2)
    library(microbenchmark)
    
    p1 <- ggplot() +
      geom_sf(data = pakistan.simple, aes(fill = NAME_3)) +
      guides(fill = FALSE)
    
    microbenchmark(ggsave("test1.pdf", p1), ggsave("test1.png", p1), times = 1)
    #> Saving 7 x 5 in image
    #> Saving 7 x 5 in image
    #> Unit: seconds
    #>                     expr        min         lq       mean     median
    #>  ggsave("test1.pdf", p1)   1.357542   1.357542   1.357542   1.357542
    #>  ggsave("test1.png", p1) 119.305828 119.305828 119.305828 119.305828
    #>          uq        max neval
    #>    1.357542   1.357542     1
    #>  119.305828 119.305828     1

    Created on 2018-05-25 by the reprex package (v0.2.0).

  10. clauswilke commented on May 25, 2018

    @clauswilke
    Member

    The upside is there's nothing fundamentally wrong with ggplot's approach to rendering maps.

  11. thk686 commented on May 26, 2018

    @thk686
    Author
  12. edzer commented on May 27, 2018

    @edzer
    Contributor

    These are great insights!

    @hadley to compute # vertices, the simplest I can think of is

    sum(rapply(st_geometry(pakistan.gadm), nrow))
  13. thk686 commented on May 27, 2018

    @thk686
    Author
  14. hadley commented on May 28, 2018

    @hadley
    Member

    This example has 127,680 points, so it's not surprising that it's slow to render. I think the solutions are outside the scope of ggplot2 — either someone needs to improve the performance of the quartz device, or sf needs to perform that optimisation suggested by @thk686

  15. clauswilke commented on May 28, 2018

    @clauswilke
    Member

    @hadley I agree it’s out of scope for ggplot2. Do you know how to best report a bug for the quartz device? I think the performance is seriously off, if I can render to pdf and then pdf to screen in ~2s total but rendering to png via quartz takes ~100s.

  16. hadley commented on May 28, 2018

    @hadley
    Member

    Probably best to email the maintainer directly (I'm pretty sure it's Simon Urbanek)

  17. ateucher commented on May 28, 2018

    @ateucher

    Just FYI, this was also discussed in the rstudio community forum

  18. clauswilke commented on May 28, 2018

    @clauswilke
    Member

    I have contacted the R Core team, which is listed as official maintainer of the quartz device.

  19. jeroen commented on May 29, 2018

    @jeroen
    Contributor

    XQuartz is a mess, put this in your onload to default to cairo instead:

    if(!identical(getOption("bitmapType"), "cairo") && isTRUE(capabilities()[["cairo"]])){
      options(bitmapType = "cairo")
    }
  20. thk686 commented on May 29, 2018

    @thk686
    Author
  21. clauswilke commented on May 29, 2018

    @clauswilke
    Member

    Unfortunately this doesn't fix the interactive use in R Studio, though, as far as I can tell.

  22. jeroen commented on May 30, 2018

    @jeroen
    Contributor

    @clauswilke I think it does? Rstudio uses the default png device.

  23. clauswilke commented on May 30, 2018

    @clauswilke
    Member

    This is the behavior I see in RStudio:

    > dev.off()
    null device 
              1 
    > dev.list()
    NULL
    > options(bitmapType = "cairo")
    > library(ggplot2)
    > ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point()
    > dev.list()
            RStudioGD quartz_off_screen 
                    2                 3 
    
  24. lock commented on Nov 26, 2018

    @lock

    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/

  25. locked and limited conversation to collaborators on Nov 26, 2018
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions