You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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.
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.3con<- 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
The general strategy used by many GIS is to not plot features smaller than
a single pixel and simplify on-the-fly to screen resolution when necessary
to avoid repeatedly painting the same pixel. I don't know if there is an
easy way to do this with ggplot et al. but it would certainly make things
fast for large coverages. Perhaps the QGIS codebase has some examples?
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
@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.
Activity
batpigandme commentedon May 24, 2018
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
thk686 commentedon May 24, 2018
batpigandme commentedon May 24, 2018
Hmm – I just took the elapsed time from before and after actually calling ggplot and it was < 4 seconds…
clauswilke commentedon May 24, 2018
It takes about 90 seconds to render this on my system.
fillis 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.Created on 2018-05-24 by the reprex package (v0.2.0).
thomasp85 commentedon May 24, 2018
While your issue may be valid, may I suggest you don’t word your next issue in ways that seem to insult the developers
hadley commentedon May 24, 2018
I don't know of a good way to figure out the total number of points, but I think there are a lot
hadley commentedon May 24, 2018
@edzer is there a simple way to total number of vertices in a multipolygon geometry?
tungttnguyen commentedon May 25, 2018
I tried @clauswilke 's code and it took ~ 2s on my Win 10 laptop (core i5, 16GB RAM). So problem might be OS dependent?
clauswilke commentedon May 25, 2018
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.
Created on 2018-05-25 by the reprex package (v0.2.0).
clauswilke commentedon May 25, 2018
The upside is there's nothing fundamentally wrong with ggplot's approach to rendering maps.
thk686 commentedon May 26, 2018
edzer commentedon May 27, 2018
These are great insights!
@hadley to compute # vertices, the simplest I can think of is
thk686 commentedon May 27, 2018
hadley commentedon May 28, 2018
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
clauswilke commentedon May 28, 2018
@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.
hadley commentedon May 28, 2018
Probably best to email the maintainer directly (I'm pretty sure it's Simon Urbanek)
ateucher commentedon May 28, 2018
Just FYI, this was also discussed in the rstudio community forum
clauswilke commentedon May 28, 2018
I have contacted the R Core team, which is listed as official maintainer of the quartz device.
jeroen commentedon May 29, 2018
XQuartz is a mess, put this in your onload to default to cairo instead:
thk686 commentedon May 29, 2018
clauswilke commentedon May 29, 2018
Unfortunately this doesn't fix the interactive use in R Studio, though, as far as I can tell.
jeroen commentedon May 30, 2018
@clauswilke I think it does? Rstudio uses the default png device.
clauswilke commentedon May 30, 2018
This is the behavior I see in RStudio:
lock commentedon Nov 26, 2018
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/