map_unif.Rd
place uniform point sample into the region `region` of a map. This function is used in `stat_polygon_jitter`.
map_unif(map, n)
map | data frame with long, lat and group variable |
---|---|
n | number of locations to sample |
library(dplyr) library(ggplot2) data(inset) df <- inset %>% filter(DIVISION == "9") %>% map_unif(1000) df <- inset %>% filter(NAME == "Iowa") %>% map_unif(10) df <- inset %>% map_unif(5000) inset %>% ggplot(aes(x = long, y = lat)) + geom_path(aes(group = group)) + geom_point(data = df, colour = "red", size = 0.5)data(crimes) population <- crimes %>% filter(Year == max(Year)) population$Abb <- as.character(population$Abb) population <- population %>% mutate( Abb = replace(Abb, Abb=="D.C.", "DC") ) popmap <- left_join(inset, population[,c("Abb", "Population")], by=c("STUSPS"="Abb")) poplist <- popmap %>% tidyr::nest(-NAME) poplist$sample <- poplist$data %>% purrr::map(.f = function(d) d %>% map_unif(50)) poplist$sample <- poplist$data %>% purrr::map(.f = function(d) d %>% map_unif(round(d$Population[1]/50000))) df <- poplist %>% select(-data) %>% tidyr::unnest() inset %>% ggplot(aes(x = long, y = lat)) + geom_path(aes(group = group), size = 0.25) + geom_point(data = df, colour = "red", size = .5) + ggthemes::theme_map()