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.

This exercise is part of the course

Analyzing US Census Data in R

View Course

Exercise 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.

Hands-on interactive exercise

Have a go at this exercise by completing this sample 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()