Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

r - RStudio graphics device really slow for spatial objects on mac

I recently got a new macbook pro and am having some R graphics related problems on it. R is working insanely slowly when plotting sf objects. I found a thread that's a couple of years old on this issue (here: https://github.com/rstudio/rstudio/issues/3866), but no solution was ever proposed. For reference, RStudioGD is plotting the object >300x more slowly than pdf and it's making me crazy. Sharing the reproducible example from the link above here (though the system time numbers are mine):

<<================= copy from link above: ===========================>>

I wanted to plot the shapefile for Myanmar found here:

https://gadm.org/download_country_v3.html

library(rgdal)
library(sp)
tdir = tempdir()

get_poly = function() {
  tmp = tempfile(tmpdir = tdir)
  download.file(
    'https://biogeo.ucdavis.edu/data/gadm3.6/shp/gadm36_MMR_shp.zip',
    tmp
  )
  
  unzip(tmp, exdir = tdir)
  
  readOGR(tdir, 'gadm36_MMR_0', stringsAsFactors = FALSE)
}

Plotting this with RStudioGD is much, much slower than to e.g. pdf:

mmr = get_poly()
system.time(plot(mmr))
#    user  system elapsed 
# 128.162   0.510 129.271 
unlink(tdir, recursive = TRUE)

Restart R to clear cache/overhead and run again:

mmr = get_poly()
tpdf = tempfile(tmpdir = tdir, fileext = 'pdf')
system.time({
  pdf(tpdf)
  plot(mmr)
  dev.off()
})
#    user  system elapsed 
# 0.423   0.027   0.460 
unlink(tdir, recursive = TRUE)

So using the external device is about 300x faster... any idea?

png also takes < 1 second

<<=================== end copy from link =======================>>

I am on macOS Big Sur 11.1 RStudio version 1.3.1093

(I am having some other vague graphics-related problems that I posted about here: quartz device behaving strangely after mac update - R mac, but I am not sure if the two are related or not).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I can't reproduce the problem without a mac, but you could try reading the file as an sf object rather than a SpatialPolygonsDataFrame.

Using sf::read_sf() will return an sf object. The readOGR() function returns the older (and more difficult to use) sp object types.

library(sf)
library(ggplot2)

# Change the path to your downloaded / unzipped location
mmr <- read_sf('Downloads/delete_mmr/gadm36_MMR_0.shp')

head(mmr)
Simple feature collection with 1 feature and 2 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: 92.1725 ymin: 8.824445 xmax: 101.1768 ymax: 28.54326
geographic CRS: WGS 84
# A tibble: 1 x 3
  GID_0 NAME_0                                                                                 geometry
  <chr> <chr>                                                                        <MULTIPOLYGON [°]>
1 MMR   Myanmar (((97.79915 8.83028, 97.79944 8.830002, 97.79972 8.830002, 97.8 8.829722, 97.80222 8...

#base plot:
plot(mmr)

enter image description here


# ggplot2 (recommended)
ggplot(mmr) + geom_sf()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...