-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.R
More file actions
44 lines (34 loc) · 1.32 KB
/
Copy pathscript.R
File metadata and controls
44 lines (34 loc) · 1.32 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
rm(list=ls())
#packages
library(sf)
library(readxl)
library(dplyr)
library(ggplot2)
library(ggsn)
#load the data
mun <- read_sf("~/BRMUE250GC_SIR.shp")
pop <- read_excel("~/brasil/pop_mun.xlsx")
#remove unvalid charaters from pop column
pop$`POPULAÇÃO ESTIMADA` <- gsub("\\([0-9]{1,2}\\)", "", pop$`POPULAÇÃO ESTIMADA`)
pop$`POPULAÇÃO ESTIMADA` <- gsub("\\.", "", pop$`POPULAÇÃO ESTIMADA`)
pop$`POPULAÇÃO ESTIMADA` <- as.numeric(pop$`POPULAÇÃO ESTIMADA`)
#join the population data with the geometries
pop$code <- paste(pop$`COD. UF`, pop$`COD. MUNIC`, sep = "")
mun <- mun[mun$CD_GEOCMU %in% pop$code,]
mun <- left_join(mun, pop, by=c("CD_GEOCMU"="code"))
#function to create random coordinates in geometries according to population data
generate_samples <- function(data){
suppressMessages(st_sample(data, size = data$`POPULAÇÃO ESTIMADA` / 1000))
}
points <- generate_samples(mun)
#################################################
#visualisation
g <- ggplot()+
theme_void()+
theme(panel.grid = element_line(size = 0),
plot.background = element_rect(fill = "white",
colour = "white"),
axis.title = element_blank())+
geom_sf(data=points, size=.001)+
scalebar(data = bb2, dist = 500, dd2km = TRUE, model = "WGS84",
location = "topleft", height = .01, st.size = 3)