Geographic Information Systems Asked by user128912 on January 20, 2021
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.
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.
Answered by Spacedman on January 20, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP