Create Heatmaps.R

New file: Heatmaps template
master
Müller Stefanie 2022-02-13 15:16:17 +01:00
parent 7e76ea34a8
commit 53334c04b0
1 changed files with 93 additions and 0 deletions

93
Heatmaps.R Normal file
View File

@ -0,0 +1,93 @@
rm(list=ls())
#######get required libraries#######
library(ggplot2)
library(dplyr)
library(tidyverse)
library(RSwissMaps)
library(viridis)
#######set working direction and get the data
setwd("~/BAK_Projekt")
#base
mapCH <- mapCH2016 %>% dplyr::rename("bfs_nr"="can")
#create dataset on canton level
df_bak <- read.csv("~/BAK_Projekt/Liste_BAK2.csv", sep = ";")
df_bak_red <- df_bak %>%
dplyr::group_by(Kanton) %>%
dplyr::summarise(count=n())
df_bak_red <- df_bak_red[!(df_bak_red$Kanton ==""),]
df_bak_red$Kt <- c("AG", "AI", "AR", "BL", "BS", "BE", "FR", "GE", "GL", "GR", "JU", "LU", "NE",
"NW", "OW", "SH", "SZ", "SO", "SG", "TI", "TG", "UR", "VD", "VS", "ZG", "ZH")
df_bak_red$bfs_nr <- as.integer(c("19", "16", "15", "13", "12", "2", "10", "25", "8", "18", "26", "3", "24",
"7", "6", "14", "5", "11", "17", "21", "20", "4", "22", "23", "9", "1"))
#get coordinates (required reference system CH1903/LV03)
mapCH.short <- mapCH[!duplicated(mapCH$bfs_nr),]
df.map <- full_join(df_bak_red, mapCH.short, by="bfs_nr") %>%
select("bfs_nr", "Kt", "name", "count", "bfs_nr", "long", "lat")
# Plotting sample data
can.plot(df.map$bfs_nr, df.map$count, 2016,
boundaries = "c", boundaries_size = 0.2, boundaries_color = "white",
title = "Verteilung der Institutionen auf Kantonsebene")
#geom_text(aes(x=df.map$long, y=df.map$lat ,label = df.map$Kt))
####Example for district map -> can be deleted
# Generating sample data:
dt.dis <- dis.template(2016)
for(i in 1:nrow(dt.dis)){dt.dis$values[i] <- sample(c(300:700), 1)/1000}
# Plotting sample data:
dis.plot(dt.dis$bfs_nr, dt.dis$values, 2016,
boundaries = "c",
title = "Beispiel auf Bezirksebene (random data)")
# Plotting sample data for the canton of Aargau:
dis.plot(dt.dis$bfs_nr, dt.dis$values, 2016, cantons = c("GR"),
lakes = c("none"),
title = "Beispiel Kanton Graubünden (Bezirksebene)")
#Example Dataset
library(scatterpie)
table7 <- table7 %>% dplyr::rename("name"="Kanton")
table7 <- table7[!(table7$name ==""),]
test <- full_join(df.map, table7, by="name")
test$radius <- 6*abs(rnorm(nrow(test)))
p <- can.plot(df.map$bfs_nr, df.map$count, 2016,
boundaries = "c", boundaries_size = 0.2, boundaries_color = "white",
title = "Verteilung der Institutionen auf Kantonsebene") +
coord_quickmap()
p + geom_scatterpie(aes(x=long, y=lat, group=bfs_nr, r=radius),
data=test, cols=c(11:13), color=NA, alpha=.8)
geom_scatterpie_legend(test$radius, x=-160, y=-55)
test %>%
select(c(11:13)) %>%
pivot_longer(cols = names(.)) %>%
ggplot(aes(x = value, y = 1, fill = name)) +
geom_col(position = "stack") +
coord_polar() +
theme_void()