Aan de slagBegin gratis

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.

Deze oefening maakt deel uit van de cursus

Analyzing US Census Data in R

Bekijk cursus

Oefeninstructies

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

Interactieve oefening met praktijkervaring

Probeer deze oefening door deze voorbeeldcode aan te vullen.

# 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()
Code bewerken en uitvoeren