walkerke / tidycensus

Load US Census boundary and attribute data as 'tidyverse' and 'sf'-ready data frames in R
https://walker-data.com/tidycensus
Other
640 stars 99 forks source link

as_dot_density() hangs with Los Angeles County #536

Closed darrellcarvalho closed 1 year ago

darrellcarvalho commented 1 year ago

Code below hangs on execution of the as_dot_density() function call until R is terminated externally. Hennepin example from book still functions. My intuition is that it has something to do with empty geometries/NA values.

library(tidycensus)
library(sf)
library(tidyverse)
los_angeles_race <- get_decennial(
  geography = "tract",
  state = "CA",
  county = "Los Angeles",
  variables = c(
    Hispanic = "P2_002N",
    White = "P2_005N",
    Black = "P2_006N",
    Native = "P2_007N",
    Asian = "P2_008N"
  ),
  summary_var = "P2_001N",
  year = 2020,
  geometry = TRUE
) %>% 
  mutate(percent = 100 * (value / summary_value))

los_angeles_dots <- los_angeles_race %>% 
  as_dot_density(
    value = "value",
    values_per_dot = 100,
    group = "variable"
)
darrellcarvalho commented 1 year ago

Unsure if relatedly, but alternative dot densities (i.e. setting to 1000) result in an error:

Error in `map()`:
ℹ In index: 4.
Caused by error:
! [dots] 'size' is too small
Backtrace:
  1. los_angeles_race %>% ...
 15. terra::dots(., field = value, size = values_per_dot)
 16. terra (local) .local(x, ...)
 17. terra:::error("dots", "'size' is too small")
 18. base::stop("[", f, "] ", emsg, ..., call. = FALSE)
walkerke commented 1 year ago

@darrellcarvalho I was able to run your first example successfully, it just took a couple minutes. Perhaps give it a bit longer?

Regarding your second example, I can reproduce the error. The code that triggers the error in terra is here:

https://github.com/rspatial/terra/blob/9b68ebdbb1493984d59db38a44fbe87521a34a3c/R/plot_vector.R#L21C1-L28C4

The error gets thrown if no dots are to be drawn for a given layer after rounding the data values divided by the size. As as_dot_density() partitions the layer by group to generate the dots, the Native American population in Los Angeles is too small for a values_per_dot of 1000 as all values then round to 0.

This is worth some error handling in as_dot_density() to explain.

darrellcarvalho commented 1 year ago

@walkerke I think it was something to do with the instance I was operating on; a reboot and running fresh resulted in no hang. Glad that tinkering with it let me bring the terra aspect to your attention, though!