map_unif: Find a uniform sample of points for one region of a map

Description Usage Arguments Examples

View source: R/sample.R

Description

place uniform point sample into the region 'region' of a map. This function is used in 'stat_polygon_jitter'.

Usage

1
map_unif(map, n)

Arguments

map

data frame with long, lat and group variable

n

number of locations to sample

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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()

heike/ggmapr documentation built on May 17, 2019, 3:23 p.m.