Oikeusministeriön vaalien tulospalvelusta on ladattavissa ensimmäisen kierroksen tulosdatat. Alla kartta jossa kunnan väri kertoo suosituimman ehdokkaan ja siirtämällä osoittimen kunnan päälle näet kuvan ehdokkaiden kannatuksesta sekä kyseisessä kunnassa että sen naapurikunnissa.
[/kode]
if (!file.exists("./2024-pressa-kierros-I.RData")){
library(tidyverse)
library(leaflet)
library(htmlwidgets)
library(glue)
library(sf)
# 2024
<- "tpv-2024_1_ehd_maa.csv.zip"
flie <- "https://tulospalvelu.vaalit.fi/TPV-2024_1/tpv-2024_1_ehd_maa.csv.zip"
urli download.file(urli, flie)
unzip(zipfile = flie, exdir = "./")
# fs::dir_ls(tmpdir)
<- read_csv2("./tpv-2024_1_aea_maa.csv", col_names = FALSE)
dat <- dat %>%
dat23 filter(grepl("\\*", X5),
!grepl("\\*", X3)) %>%
# äänimäärä ja ehdokasnumero kokonaisluvuiksi
mutate(X3 = as.integer(X3),
X35 = as.integer(X35),
X38 = as.integer(X38)
%>%
) # vaalipiirit veks
filter(!is.na(X3)) %>%
# Merkistöenkoodaukset
mutate(
X16 = iconv(x = X16, from = "Windows-1252", to = "UTF-8"),
X17 = iconv(x = X17, from = "Windows-1252", to = "UTF-8"),
X19 = iconv(x = X19, from = "Windows-1252", to = "UTF-8"),
X18 = iconv(x = X18, from = "Windows-1252", to = "UTF-8"),
%>%
) select(X3,X6,X18,X19,X35) %>% #print(n = 100)
mutate(ehdokas = paste0(X18," ", X19)) %>%
group_by(X3) %>%
mutate(osuus = round(X35 / sum(X35) * 100,1)) %>%
arrange(desc(osuus)) %>%
mutate(rank = 1:n()) %>%
ungroup() %>%
select(X3,ehdokas,X35,osuus,rank) %>%
left_join(geofi::municipality_key_2023 %>% select(municipality_code,municipality_name_fi), by = c("X3" = "municipality_code"))
<- geofi::get_municipalities()
muni
<- dat23 %>%
levelit_ehdokas group_by(ehdokas) %>%
summarise(X35 = sum(X35)) %>%
arrange(desc(X35)) %>%
pull(ehdokas)
# tehdään png-kuvat labeleihin
# fs::dir_create("../../images/png_labs_vaalit2023")
<- unique(dat23$municipality_name_fi)
kuntanimet for (i in seq(kuntanimet)){
print(paste0(i,"/",length(kuntanimet)))
<- muni %>% filter(municipality_name_fi == kuntanimet[i])
this_muni
<- sf::st_intersection(muni, this_muni) %>%
naapurikunnat ::st_drop_geometry() %>%
sfcount(municipality_name_fi) %>%
pull(municipality_name_fi)
<- muni %>% filter(municipality_name_fi %in% naapurikunnat)
naapurikunnat_shape
<- dat23[dat23$municipality_name_fi %in% naapurikunnat,]
dat_tmp
$ehdokas <- factor(dat_tmp$ehdokas,
dat_tmplevels = levelit_ehdokas)
<- c(kuntanimet[i], naapurikunnat[!naapurikunnat %in% kuntanimet[i]]) |> rev()
levelit_kunta $municipality_name_fi <- factor(dat_tmp$municipality_name_fi, levels = levelit_kunta)
dat_tmp$municipality_name_fi <- factor(naapurikunnat_shape$municipality_name_fi,
naapurikunnat_shapelevels = levelit_kunta)
<- ggplot(data = dat_tmp,
p aes(x = osuus, y = municipality_name_fi, fill = municipality_name_fi)) +
geom_col() +
geom_text(aes(x = 5, label = paste0(osuus, "%")), family = "Space Mono", size = 2.3) +
facet_wrap(~ehdokas) +
# hrbrthemes::scale_fill_ipsum() + #scale_fill_brewer(palette = "Set1") +
::theme_ipsum_rc(base_size = 9, grid = "XY",
hrbrthemesplot_title_size = 18,
strip_text_size = 9,
base_family = "Space Mono") +
theme(legend.position = "none",
plot.margin = unit(c(1,1,1,1), "mm"),
legend.key.size = unit(4, "mm"),
plot.background = element_rect(fill = "transparent", color = NA),
panel.background = element_rect(fill = "transparent", color = NA)) +
labs(y = NULL, x = "kannatus (%)") +
theme(panel.spacing = unit(0.1, "mm"))
<- ggplot(naapurikunnat_shape, aes(label = municipality_name_fi, fill = municipality_name_fi)) + geom_sf() +
p1 geom_sf_text(family = "Space Mono", size = 2.5) +
::theme_ipsum_rc(base_size = 9, grid_col = "white",
hrbrthemesplot_title_size = 18,
base_family = "Space Mono") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
legend.position = "none")
<- patchwork::wrap_plots(p,p1, widths = c(0.55,.45)) + patchwork::plot_annotation(
pp title = paste0(kuntanimet[i], " ja naapurikunnat"),
subtitle = "Ehdokkaat järjestetty alueen kokonaisäänimäärän mukaan",
theme = hrbrthemes::theme_ipsum_rc(base_size = 9, grid_col = "white",
plot_title_size = 18,
base_family = "Space Mono"))
# p
ggsave(filename = glue("./png_labs/{kuntanimet[i]}.png"),plot = pp, width = 8, height = 6, dpi = 100)
}<- geofi::get_municipalities(year = 2022) %>% mutate(vuosi = 2023)
kunnat
<- left_join(muni, dat23)
dat_kartta
save.image(file = "./2024-pressa-kierros-I.RData")
}
library(tidyverse)
library(leaflet)
library(htmlwidgets)
library(glue)
library(sf)
load(file = "2024-pressa-kierros-I.RData")
<- dat_kartta %>%
dat_alue # lasketaan kuntakohtaiset summat
group_by(municipality_code) %>%
filter(osuus == max(osuus, na.rm = TRUE)) %>%
ungroup() %>%
select(municipality_code,municipality_name_fi,ehdokas,osuus,X35,rank) %>%
::st_transform(4326)
sf
# puoluekohtaiset kannatukset
library(tidyverse)
library(leaflet)
library(htmlwidgets)
library(glue)
library(sf)
<- leaflet::colorFactor(palette = "Set1", domain = factor(dat_alue$ehdokas))
pal
<- leafletCRS(crsClass = "L.Proj.CRS", code = "EPSG:3067",
epsg3067 proj4def = "+proj=utm +zone=35 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs",
resolutions = 2^(13:-1), # 8192 down to 0.5
origin = c(0, 0)
)
<- sprintf(
labels '<div class="card" style="width: auto;">
<div class="card-body">
<img src ="png_labs/%s.png">
</div>
</div>',
$municipality_name_fi
dat_alue%>% lapply(htmltools::HTML)
)
leaflet(dat_alue,
options = leafletOptions(worldCopyJump = F,
crs = epsg3067)
%>%
) # addProviderTiles(provider = providers$CartoDB.Positron) %>%
# leaflet::setView(lng = 25.25,lat = 61.742, zoom = 4) %>%
addPolygons(fillColor = ~pal(factor(ehdokas)),
color = "white",
weight = 1,
opacity = 1,
dashArray = "3",
fillOpacity = 0.4,
highlight = highlightOptions(
weight = 2,
color = "#666",
dashArray = "",
fillOpacity = 0.4,
bringToFront = TRUE),
label = labels,
labelOptions = labelOptions(opacity = .7,
style = list("font-weight" = "normal",
"font-family" = "Courier New",
"padding" = "0px"),
textsize = "12px",
direction = "auto")
%>%
) addLegend(pal = pal, values = ~ehdokas, opacity = 0.7, title = "Kunnan suosituin ehdokas",
position = "bottomright") %>%
::addFullscreenControl() leaflet.extras
Uudelleenkäyttö
Viittaus
BibTeX-viittaus:
@online{kainu2024,
author = {Kainu, Markus},
title = {Presidentinvaalit 2024 I kierros: ehdokkaiden kannatus
kunnittain ja naapurikunnittain},
date = {2024-01-29},
url = {https://markuskainu.fi/posts/2024-01-29-presidentinvaalit-kunta/},
langid = {fi}
}
Viitatkaa tähän teokseen seuraavasti:
Kainu, Markus. 2024. “Presidentinvaalit 2024 I kierros:
ehdokkaiden kannatus kunnittain ja naapurikunnittain.” January
29, 2024. https://markuskainu.fi/posts/2024-01-29-presidentinvaalit-kunta/.