CommencerCommencer gratuitement

Generating random dots with sf

Dot-density maps are created by randomly placing dots within areas where each dot is proportional to a certain number of observations. In this exercise, you'll learn how to create dots in this way with the sf package using the st_sample() function. You will generate dots that are proportional to about 100 people in the decennial Census, and then you will group the dots to speed up plotting with ggplot2.

Cet exercice fait partie du cours

Analyzing US Census Data in R

Afficher le cours

Instructions

  • Use the st_sample() function to create dots where each dot represents approximately 100 people.
  • Use the mutate() function to generate a new group column named group.
  • Group by the new group column and summarize your data to speed up plotting.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Generate dots, create a group column, and group by group column
dc_dots <- map(c("White", "Black", "Hispanic", "Asian"), function(group) {
  dc_race %>%
    filter(variable == group) %>%
    ___(., size = .$value / 100) %>%
    st_sf() %>%
    ___(group = group) 
}) %>%
  reduce(rbind) %>%
  group_by(___) %>%
  summarize()
Modifier et exécuter le code