r-tmap / tmap

R package for thematic maps
https://r-tmap.github.io/tmap
GNU General Public License v3.0
862 stars 121 forks source link

Question Concerning Choropleth Demo #165

Closed jrisi256 closed 6 years ago

jrisi256 commented 6 years ago

Hello! Thank you for making this package! It is really a great package, and I am enjoying learning how to use it. Sorry if this isn't the right place to be asking this question, or if this is a total rookie mistake I am making.

In any case, looking at your choropleth demo code here: https://github.com/mtennekes/tmap/blob/master/demo/USChoropleth/US_choropleth.R I tried to follow along and run it on my computer (running Ubuntu 17.10 with 8GB Ram), and as soon as I get to lines 43/44 (making the choropleth interactive) my computer begins to slow down a lot. The map finally renders, but as soon as I try and interact with my RStudio crashes. Checking my memory usage in System Monitor confirms I am using up pretty much all of computer's RAM.

My question is whether or not this is a common issue? Is it specifically because I'm running Ubuntu? Do I need a better computer with more RAM? Is there a way to fix this issue without resorting to hardware upgrades?

Thanks!

mtennekes commented 6 years ago

Hi!

It is a large shapefile, so it could take a few seconds to load. However, after that it should function well. Your setup is identical to mine. I checked the System monitor and only 2.6GB of ram was used after plotting. Did you run the CRAN version or the github version? I also updated the installation script for Ubuntu 17: see https://github.com/mtennekes/tmap/blob/master/ubuntu_17_installation.sh Maybe that could help...

jrisi256 commented 6 years ago

I see what the error is now (although I haven't fixed it). So I wasn't following your code exactly. Instead I was using the "tigris" package to download my county shape files. It is when I use the shape files otained from the tigris package that my RStudio client crashes. The reason it crashes seems to because the LargeSpatialPolygonsDataFrame from the tigris packages is 10x larger than the LargeSpatialPolygonsDataFrame obtained using your code. Do you have any idea why it is so much larger? Or is this something I should post about in the tigris package?

library(readxl)
library(grid)
library(tmap)
library(tmaptools)
library(tigris)
#options(tigris_class = "sf")

get_food_envir_data <- function() {
  dir <- tempdir()
  download.file("https://www.ers.usda.gov/webdocs/DataFiles/48731/February2014.xls?v=41688", destfile = file.path(dir, "DataDownload.xls"), mode="wb")
  read_excel(file.path(dir, "DataDownload.xls"), sheet = "HEALTH")
}

FEA <- get_food_envir_data()

US_c <- counties()

US_c$FIPS <- paste0(US_c$STATEFP, US_c$COUNTYFP)

US_c <- append_data(US_c, FEA, key.shp = "FIPS", key.data = "FIPS", ignore.duplicates = TRUE)

ttm()
qtm(US_c, fill = "PCT_OBESE_ADULTS10")
jrisi256 commented 6 years ago

Further answering my own question, digging into the different LargeSpatialPolygonsDataFrames which are obtained by the two different function calls:

#using tigris
US_c = counties()
#Your way in the demo, not using tigris
# function to obtain US county shape
get_US_county_2010_shape <- function() {
  dir <- tempdir()
  download.file("http://www2.census.gov/geo/tiger/GENZ2010/gz_2010_us_050_00_20m.zip", destfile = file.path(dir, "gz_2010_us_050_00_20m.zip"))
  unzip(file.path(dir, "gz_2010_us_050_00_20m.zip"), exdir = dir)
  read_shape(file.path(dir, "gz_2010_us_050_00_20m.shp"))
}

# obtain US county shape
US <- get_US_county_2010_shape()

The tigris shape files have many, many more coordinates associated with them which is what causing the Large SpatialPolgyonsDataFrame to be nearly 10x the size of the shape Large SpatialPolygonsDataFrame created from your function call. It seems to me I should be going to the tigris page now, but if you had any insight to offer as to why your shape files are so much smaller (due to them having many fewer coordinates) but still seemingly producing the same map of the US counties, I would be grateful.

mtennekes commented 6 years ago

I'm not sure tigris also provides different levels of shape detail (and thus less coordinates). The reason I didn't use tigris in the example was that shapes from 2010 were not available via tigris.

jrisi256 commented 6 years ago

Yeah I think that's the solution. You have to adjust the shape details so they aren't so detailed (as they don't need to be). Thanks!