Skip to content

Instantly share code, notes, and snippets.

@PaulC91
Created May 16, 2018 12:18
Show Gist options
  • Save PaulC91/6200a6c43895420beba53a2d76573295 to your computer and use it in GitHub Desktop.
Save PaulC91/6200a6c43895420beba53a2d76573295 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(sf)
#https://www.cultureofinsight.com/blog/2018/05/02/2018-04-08-multivariate-dot-density-maps-in-r-with-sf-ggplot2/
svk.dot <- st_read("shp/hranice_obce_3.shp", stringsAsFactors = FALSE, quiet = TRUE) %>%
st_transform(4326)
colnames(svk.dot)[1] <- "id"
nrsr<- read.csv("nrsrall.csv",fileEncoding = "UTF-8",check.names=TRUE, stringsAsFactors = FALSE)
colnames(nrsr)[2] <- "id"
svk.dot <- left_join(nrsr, svk.dot) %>%
st_as_sf()
random_round <- function(x) {
v=as.integer(x)
r=x-v
test=runif(length(r), 0.0, 1.0)
add=rep(as.integer(0),length(r))
add[r>test] <- as.integer(1)
value=v+add
ifelse(is.na(value) | value<0,0,value)
return(value)
}
num_dots <- as.data.frame(svk.dot) %>%
select(tip_16:sas_16) %>%
mutate_all(funs(. / 100)) %>%
mutate_all(random_round)
sf_dots <- map_df(names(num_dots),
~ st_sample(svk.dot, size = num_dots[,.x], type = "random") %>%
st_cast("POINT") %>%
st_coordinates() %>%
as_tibble() %>%
setNames(c("lon","lat")) %>%
mutate(category = .x)
) %>%
slice(sample(1:n()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment