-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapview.Rmd
73 lines (63 loc) · 1.89 KB
/
mapview.Rmd
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
---
title: "mapview"
author: "Christian Födermayr"
date: "1/27/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
##Load Packages
```{r, include=FALSE}
library(dplyr)
library(magrittr)
library(ggplot2)
# never knew there was an gdla r package --> but awesome!
library(rgdal)
library(jsonlite)
library(leaflet)
```
## Set Working directory
```{r}
#setwd('stat-prog-2019')
```
## Read provided shapefiles
Thanks for natural erath data to provide good and high quality geodata all over the world (http://naturalearthdata.com/)
```{r}
eustates <- readOGR("shp/world/world_countries.shp",
layer = "world_countries", GDAL1_integer64_policy = TRUE)
```
## add datat to shapefile
You can take any data you want, if it has at least a isocode column with iso2 values in it (PL, AT,...)
```{r}
data = read.csv(file="data/clean_euro_data/unemployed_persons_data.csv")
# uppercase isocode for matching with geometries
data %<>%
mutate(isocode = toupper(isocode))
# use spatial merge
eustates = sp::merge(eustates, data, by="isocode",duplicateGeoms = TRUE)
```
## Generate Map
You can easily adapt this tooltip regarding two things:
- Define an html string
- select the correct columns
```{r}
# define labels
labels <- sprintf(
"<strong>%s</strong><br/>%s: %g",
eustates$label, eustates$WSTATUS, eustates$Value
) %>% lapply(htmltools::HTML)
leaflet(eustates) %>%
# set extent for europe
fitBounds(-15.117188, 32.101190, 39.199219,70.844673) %>%
addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,
opacity = 1.0, fillOpacity = 0.5,
fillColor = ~colorQuantile("YlOrRd", Value)(Value),
highlightOptions = highlightOptions(color = "white", weight = 2,
bringToFront = TRUE),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto"))
```