Sehj Kashyap
Sehj Kashyap

Reputation: 57

Why is plot of GADM SpatialPolygonsDataFrame not loading in R?

One-liner: R is taking too long to plot a SpatialPolygonsDataFrame and the best answer I've found online is that the issue is machine-specific.

Problem: I am trying to plot a SpatialPolygonsDataFrame of India (level = 1) that I downloaded from GADM but no plotting function (spplot, plot, tm_shape) is producing a plot in any given time; instead R appears to do work in the background but 5+ minutes later there still is no plot. Pressing the stop button that appears above the console does nothing and eventually in order to re-try some other method I have to force-quit the app. My question is similar to this question but the consensus on that question was that the issue appears machine specific with no solution thereafter.

Here's some info about my machine/R studio version: R version 3.3.2 (2016-10-31) / R Studio Version 1.0.136 / OS: MacOS Sierra version 10.12.6.

The file was downloaded from: GADM in RDS format

(1.) I've tried different plotting mechanisms

setwd("~/Data/Reference/")
india <- readRDS("IND_adm1.rds")

library(sp)
library(tmap)

# Plot method #1 
plot(india)

# Plot method #2
spplot(india, "NAME_1")

# Plot method #3
tm_shape(india) + tm_borders()

(2.) I've tried recreating the SpatialPolygonsDataFrame from a shapefile instead of the RDS

india <- readOGR(dsn = "~/Data/Reference/" , layer = "IND_adm1")

(3.) I've updated all packages on the machine and uninstalled and re-installed the sp package and the tmap package.

(4.) I've tried other countries' file and had the same issue.

(5.) I've examined the structure of the SpatialPolygonsDataFrame and found no issues with it -- things seem to be in place and in the right way.

(6.) Using maps package to plot their data works fine. For some reason SpatialPolygonsDataFrame classes are not plotting. I've now also tried to plot the shape files from DIVA-GIS.

library(maps)
tx <- map("county", "texas", plot = FALSE, fill = TRUE)
plot(tx) # this plots fine and immediately

Requirement I basically need a state boundary map of India which I can put into a Shiny interactive RMarkdown report and fill state colors based on a factor variable. Is there some work-around for the data that I can use?

Ask: I'm not sure what to do or how to further diagnose. I'd appreciate any help that I can get and happy to provide any more info if that would be helpful. I'm also linking the exact RDS file that I'm using for you to load and try on your machine.

Update None of the suggested solutions worked on my system but I think this challenge I was having was systems specific. I ultimately tried running the same code on a remote Windows instance on Amazon web services and there the code worked fine. So I am not sure whether it was a Mac issue or a hardware issue, but I hope this update is helpful.

Upvotes: 1

Views: 1146

Answers (1)

AJGL
AJGL

Reputation: 303

I've had this problem on my mac for a while and when searching for solutions, found this on R Bloggers. The code simplifies a shapefile enabling it to plot much more easily:

library("shapefiles")

map = readOGR(dsn = "shp", layer = "shp")

for(i in 1:length(map@polygons)){
  for(j in 1:length(map@polygons[[i]]@Polygons)){
    temp <- as.data.frame(map@polygons[[i]]@Polygons[[j]]@coords)
    names(temp) <- c("x", "y")
    temp2 <- dp(temp, 0.01)
    map@polygons[[i]]@Polygons[[j]]@coords <- as.matrix(cbind(temp2$x, temp2$y))
  }
}

plot(map, col = "black")

(http://www.r-bloggers.com/simplifying-polygon-shapefiles-in-r/).

However, although this can help the plotting process, it becomes troublesome to try and perform GIS operations on the new shapefile due to its simplified form.

It is worth looking for country shapefiles from sites other than GADM. For example, all the shapefiles I have used from this site plot effortlessly and instantly on my mac: http://mapeastafrica.com/countries/east-africa-shapefiles/.

Upvotes: 0

Related Questions