On 18th September 2016, Berlin voted for the state elections. Using the library “leaflet”, the results of the state election is visualised according to the voting areas.
library(leaflet)
library(maptools)
library(dplyr)
library(sp)
library(rgdal)
library(caret)
berlin_shape <- readOGR("../data/geo/Berlin_shape/UWB.shp", layer = "UWB", encoding = "utf-8", verbose = FALSE)
berlin_map <- spTransform(berlin_shape, CRS("+init=epsg:4326"))
# create column for Winner
z <- apply(result1[18:43], 1, which.max)
result1$Winner <- as.factor(names(result1[18:43])[z])
# level of Winners are in the sequence: AfD CDU FDP GRÜNE LINKE SPD
# create palette colour according to levels in sequence
pal <- colorFactor(c('#00ADEF',
'#000000',
'#FFED00',
'#58AB27',
'#8C3473',
'#F0001C'),
result1$Winner)
winner_popup <- paste0("<strong>Winner: </strong>", result1$Winner,
"<br><strong>Voting area: </strong>", result1$Wahlbezirk, ", ",
result1$Bezirksname,
"<br><strong>Number of total votes: </strong>",
result1$Gültige.Stimmen
)
berlin_leaflet <-
berlin_map %>% leaflet() %>%
addTiles() %>%
addPolygons(
color = "#FFFFFF",
fillOpacity=0.6,
smoothFactor = 0.5,
weight=0.5,
fillColor = ~pal(result1$Winner),
popup = winner_popup,
group = "Normal transparency"
) %>%
addPolygons(
color = "#FFFFFF",
fillOpacity=0.8,
smoothFactor = 0.5,
weight=0.5,
fillColor = ~pal(result1$Winner),
popup = winner_popup,
group = "Low transparency"
) %>%
addPolygons(
color = "#FFFFFF",
fillOpacity=0.3,
smoothFactor = 0.5,
weight=0.5,
fillColor = ~pal(result1$Winner),
popup = winner_popup,
group = "High transparency"
) %>%
addLayersControl(
position="topright",
baseGroups = c("Normal transparency", "Low transparency", "High transparency"),
options = layersControlOptions(collapsed = FALSE)
) %>%
addLegend(pal=pal, values = ~result1$Winner, title= "Winning party", opacity=1) %>%
setView(lng = 13.40, lat = 52.52, zoom = 10)
berlin_leaflet