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.
Este ejercicio forma parte del curso
Analyzing US Census Data in R
Instrucciones del ejercicio
- 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 namedgroup. - Group by the new
groupcolumn and summarize your data to speed up plotting.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
# 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()