1

Posting here (rather than Github) as it is not an issue with the tmap package.

Situation:

I am doing simple choropleth maps of varinterest for U.S. counties (N~3000) using tmap (data stored in data object). Each map also has a key column that we will call category. I am rendering a variable of about ~300 counties for a varinterest for a category, but I would also like to fill the counties with missing data as gray.

My current method is as follows:

tm_shape(data) + 
     tm_fill() +
tm_shape(data %>% filter(category='cat1')) + 
     tm_polygons(varinterest) +

However, this is a bit slow (taking 5-7 minutes to render on a very strong computer). Mapping only the variable of interest (i.e. the second tm_shape(... takes only a few seconds.

I feel like I am missing something obvious that might speed up rendering - any advice?

I will work on a reproducible example and post my times.

1

Here's a reproducible example. Get county shapes from USAboundaries:

> library(USAboundaries)
> data = us_counties()
> dim(data)
[1] 3220   13

add a category column with about 300 in each category, and a varinterest

> data$category=paste0("cat",sample(1:10,nrow(data),TRUE))
> table(data$category)

 cat1 cat10  cat2  cat3  cat4  cat5  cat6  cat7  cat8  cat9 
  342   324   329   310   344   310   332   289   321   319 
> data$varinterest = runif(nrow(data))

Your tm_shape line looks a bit broken - you've got category='cat1' which needs a ==, no quotes round varinterest which are necessary, and a stray + sign on the end. Fixing those gives:

> tm_shape(data) + tm_fill() + tm_shape(data %>% filter(category=='cat1')) + tm_fill("varinterest")

which runs in seconds on my mediocre computer.

enter image description here

  • Thanks for the quick reproducible example, @Spacedman! Apologeis for the mistakes, I typed it up quick without my reference code. Beyond the small issues, do you think tm_fill() would speed up the rendering process? I am currently doing a tm_polygons() for my varinterest, and a tm_fill for the gray background. – Reputable Misnomer Oct 2 at 13:49
  • Does my code do what you want, and does it still take 5-7 minutes for you with your data on your computer? There's no timing difference between tm_fill and tm_polygons. Is my reproducible example still slow for you? – Spacedman Oct 2 at 15:54
  • Hmm...both are taking the same amount of time on my computer and getting your results (<10 seconds for both both tm_fill("varinterest") and tm_polygons("varinterest")). I'm guessing that this must be a dataframe problem - the dataframes from which I am subsetting the data are quite large, so perhaps I need to create some additional objects that are simplified... For example, one of the data objects is ~26000 rows with 100 variables. I use that same object to render the gray background, the fill variable, and the state outline. – Reputable Misnomer Oct 2 at 15:59
  • You said you were using a ~3000 row counties data and a ~300 row subset. Where does the 26,000x100 dataset come into it? Want to delete this question and start again with a reproducible example? – Spacedman Oct 2 at 16:51
  • Sorry - it is still US counties but it is longitudinal with many variables. I can delete to avoid confusion and it seems reducing the size of dataframes has improved speed (albeit only slightly). – Reputable Misnomer Oct 2 at 19:47

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.