5

In my macOS system RStudio, plotting spatial data (statialpolygons, points and lines) is extremely slow, making it impossible to work with.

When I try to plot the shapefile in this link (@https://drive.google.com/drive/folders/0B0XRH7FN95-RSzFRckhXNjVWYlk), it takes about 30 minutes in RStudio. If I plot in R (outside RStudio), it takes about 20 minutes to print in an external plotting windows titled Quartz2. On the contrary, it is a couple of less than a minute in a window's machine in my lab with equivalent specs.

The best workaround I know is to use a pdf as plotting device (or png). Doing that, it takes just a couple of seconds.

shp <- readOGR("~/stof_shp", "AP_PAT_edited1")
pdf()
plot(shp)
dev.off()

But it is not a satisfactory solution. I love work with spatial data on R, but the biggest inconvenient is the difficulty for visualizing the data, and this makes it much harder.

I have talked with different people that are in my same situation, but there doesn't seem to be too much information about it when you google or search in gis.stackexchange. I have doubts on whether it affects all macOS systems, or only laptops, only some version of the OS or all of them. My system and applications are updated.

sessionInfo() R version 3.4.0 (2017-04-21) Platform: x86_64-apple-darwin16.6.0 (64-bit) Running under: macOS Sierra 10.12.5

At some point, I thought it was the matter of my version of gdal or proj, but I am confident now that I have the latests set and working. I have rgdal_1.2-8 and sp_1.2-5.

> library(rgdal)
Loading required package: sp
rgdal: version: 1.2-8, (SVN revision 663)
 Geospatial Data Abstraction Library extensions to R successfully loaded
 Loaded GDAL runtime: GDAL 2.1.3, released 2017/20/01
 Path to GDAL shared files: /opt/local/share/gdal
 Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
 Path to PROJ.4 shared files: (autodetected)
 Linking to sp version: 1.2-5

I have also been messing with XQuartz and X11, getting nowhere (I wouldn't know how to get the info of which versions I have installed, but if I enter X11() on RStudio, it will try to start X11, but it will crash in an infinite loop that I have been unable to fix).

I started the question in stackoverflow, but I was told to post the question here, where it belongs better. Few months ago I made similar question here, but I did not reproduce the example data and I think that I might have been missleading in how I presented the problem (@Slow plotting SpatialPolygonDataFrames on some systems). For those reasons I am starting a new question.

3

As you say, I also think that using RStudio for plotting is slower than only using R. I suggest you try using sf package because it's faster for loading spatial vector data than rgdal see here: Simple Features for R and First Impressions From sf – The Simple Features R Package. Also, I had a better impression for performance when plotting than sp. And sf will replace sp.

I tried download your shapefile and plot in RStudio using sf. It took some seconds to make the visualizations.

# Load libraries
library('sf') # manage spatial vector data
library('mapview') # interactive map visualization in R

# Load data from ESRI shapefile
AP_PAT_edited1 <- st_read("AP_PAT_edited1.shp")

# View data 
head(AP_PAT_edited1)

Simple feature collection with 6 features and 1 field
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -70.21592 ymin: 7.712805 xmax: -66.27898 ymax: 12.04268
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
  id                       geometry
1  1 MULTIPOLYGON(((-69.96232793...
2  1 MULTIPOLYGON(((-67.01927266...
3  1 MULTIPOLYGON(((-69.84210651...
4 10 MULTIPOLYGON(((-67.59778541...
5 10 MULTIPOLYGON(((-70.08384200...
6 11 MULTIPOLYGON(((-66.28849953...    

# Simple plot shapefile
plot(AP_PAT_edited1, 
     xlab = "Longitude", 
     ylab = "Latitude",
     graticule = st_crs(4326), 
     axes = TRUE, main = "Plot")

simplePlot

# Interactive plot with mapview
mapView(AP_PAT_edited1, zcol = 'id')

interactivePlot

  • thanks for your detailed comment. I knew sf, but had never given it a try. It was surprisingly nice to test it, but when it comes to plotting, I am stuck with the same problem. It also takes ages using RStudio's device. However, it made me more confidence into thinking that it is the device's problem. – Javier Fajardo Jul 7 '17 at 15:50
  • @JavierFajardo what a pity, I had the hope that maybe changing libraries would solve the problem. Using mapview is also slow? That seems to be some graphic card related problem. – Guzmán Jul 7 '17 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.