Plik kandydaci_wbp_utf8.csv
pobrany 9. kwietnia 2024.
Ponieważ jest powien chaos w nazewnictwie to najpierw uwspółniłem
warianty. Zamieniłem też przecinek na kropkę w kolumnie
Procent.głosów
.
ppp <- c('PiS', 'PO', 'PSL', 'P2050')
f <- read.csv(file='kandydaci_wbp_utf8.csv',
colClasses = c( "TERYT"="character"),
sep=';',header=T) %>%
mutate(across('Poparcie', str_replace, 'popieran[ay] przez partiÄ™ politycznÄ…: ', '')) %>%
mutate(across('Poparcie', str_replace, '^ +| +$', '')) %>%
####
mutate(across('Poparcie', str_replace, "KW Prawo i Sprawiedliwość", "PiS" )) %>%
mutate(across('Poparcie', str_replace, "Prawo i Sprawiedliwość", "PiS")) %>%
mutate(across('Poparcie', str_replace, "Platforma Obywatelska RP", "PO")) %>%
mutate(across('Poparcie', str_replace, "Platforma Obywatelska", "PO")) %>%
mutate(across('Poparcie', str_replace, "Platforma RP", "PO")) %>%
mutate(across('Poparcie', str_replace, "PlatformaObywatelskaRzeczypospolitejPolskiej", "PO")) %>%
mutate(across('Poparcie', str_replace, "Polska 2050 Szymona Hołowni", "P2050")) %>%
mutate(across('Poparcie', str_replace, "Polskie Stronnictwo Ludowe", "PSL" )) %>%
mutate(across('Poparcie', str_replace, "PSL", "PSL")) %>%
###
mutate(across('Przynależność.do.partii', str_replace, "członek partii politycznej:", "" )) %>%
mutate(across('Przynależność.do.partii', str_replace, '^ +| +$', '')) %>%
mutate(across('Przynależność.do.partii', str_replace, "Prawo i Sprawiedliwość", "PiS")) %>%
mutate(across('Przynależność.do.partii', str_replace, "Platforma Obywatelska RP", "PO")) %>%
mutate(across('Przynależność.do.partii', str_replace, "Platforma Obywatelska", "PO")) %>%
mutate(across('Przynależność.do.partii', str_replace, "Platforma RP", "PO")) %>%
mutate(across('Przynależność.do.partii', str_replace, "PlatformaObywatelskaRzeczypospolitejPolskiej", "PO")) %>%
mutate(across('Przynależność.do.partii', str_replace, "Polska 2050 Szymona Hołowni", "P2050")) %>%
mutate(across('Przynależność.do.partii', str_replace, "PL2050 Szymona Hołowni", "P2050")) %>%
mutate(across('Przynależność.do.partii', str_replace, "Polska 2050", "P2050")) %>%
mutate(across('Przynależność.do.partii', str_replace, "Polskie Stronnictwo Ludowe", "PSL" )) %>%
mutate(across('Przynależność.do.partii', str_replace, "PSL", "PSL")) %>%
### Przecinek na kropkÄ™
mutate(across('Procent.głosów', str_replace, ",", ".")) %>%
### Zamień na liczbę
mutate(procent = as.numeric(Procent.głosów)) %>%
###
### Tylko na urzÄ…d Prezydent/Burmistrz
filter(grepl('Prezydent|Burmistrz', UrzÄ…d)) %>%
### Tylko afiliowani
filter (Przynależność.do.partii %in% ppp | Poparcie %in% ppp)
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `across(...)`.
## Caused by warning:
## ! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
## Supply arguments directly to `.fns` through an anonymous function instead.
##
## # Previously
## across(a:b, mean, na.rm = TRUE)
##
## # Now
## across(a:b, \(x) mean(x, na.rm = TRUE))
##nrow(f)
## Przynależność.do.partii lub Poparcie zamień na jedną kolumnę
## kolumna p0 = 1 jeżeli procent > 50
f1 <- f %>%
## If poparcie then poparcie else Przynależność :-)
mutate ( p = if_else(Poparcie %in% ppp, Poparcie,
if_else(Przynależność.do.partii %in% ppp, Przynależność.do.partii, NA)
)) %>%
mutate (p0 = if_else (procent >50, 1, 0)) %>%
mutate (z0 = if_else (p0 >0, p, NA))
### Według stanu na 1 stycznia 2024 na terenie Polski jest 1013 miast
fx <- f %>% group_by(Obszar) %>% summarise(t = first(Obszar))
##nrow(fx)
## liczba kandydatów
fyy <- f1 %>% group_by(p) %>% summarise(p0sum=n())
## liczba kandydatów co dostali > 50%
fzz <- f1 %>% group_by(p) %>% summarise(p0sum=sum(p0))
## gmina gdzie był max
fmx <- f1 %>% group_by(p) %>% arrange(procent) %>% summarise(p0sum=last(procent), p0name=last(Obszar))
## gmina gdzie było min
fmx.min <- f1 %>% group_by(p) %>% arrange(procent) %>% summarise(p0sum=first(procent), p0name=first(Obszar))
p4 <- f1 %>% ggplot() +
###
ggtitle("Wyniki kandydatów na urząd prezydentów i burmistrzów ", subtitle='startujących z rekomendacji 4 partii') +
ylab("%") +
xlab("") +
## kreska 90%
geom_hline(yintercept = 90, color="red", alpha=.25, size=0.4) +
annotate("text", x = 'P2050', y = 88, label = '90%', hjust = +2.5, size=3.5, alpha=.3, color='red') +
##
annotate("text", x = 'P2050', y = -3, label = 'kandydaci', hjust = +1.3, size=3.5, alpha=.3, color='deeppink') +
annotate("text", x = 'P2050', y = 94, label = 'wybrani', hjust = +1.2, size=4.5, alpha=.3, color='steelblue4') +
## wykres główny
geom_jitter(
aes(y=procent, x=as.factor(p), color=as.factor(p0)),
position=position_jitter(0.20, ), alpha=.4 ) +
scale_color_manual(values = c("deeppink", "steelblue4")) +
## ilu przeszło
geom_text(data=fzz, aes(x= as.factor(p), y=95, label=sprintf("%i", p0sum)), color='steelblue4', size=8 ) +
## ilu startowało
geom_text(data=fyy, aes(x= as.factor(p), y=-3, label=sprintf("%i", p0sum)), color='deeppink' ) +
### gmina max:
geom_text(data=fmx, aes(x= as.factor(p), y=-8, label=sprintf("%s/%.1f", p0name, p0sum)),
color='steelblue4', size=3 ) +
## gmina min:
geom_text(data=fmx.min, aes(x= as.factor(p), y=-11, label=sprintf("%s/%.1f", p0name, p0sum)),
color='deeppink', size=3 ) +
theme(legend.position = "none") +
labs(caption='Dwa dolne wiersze wynik najlepszy/najgorszy (w %). Dane: PKW')
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
p4
ggsave(p4, file='PB1.png')
## Saving 7 x 5 in image
Zwycięzcy na mapie
##
library("sf")
## Linking to GEOS 3.9.0, GDAL 3.2.2, PROJ 7.2.1; sf_use_s2() is TRUE
g.PL <-read_sf("/home/tomek/Data/GiS","Gminy") %>%
mutate (JPT_KOD_JE = substr(JPT_KOD_JE, 1, 6))
f3 <- left_join(g.PL, f1, by=c("JPT_KOD_JE"='TERYT'))
## Kandydaci na mapie
## błąd koncepcyjny
#r1 <- ggplot(data=f3 ) +
# geom_sf(fill='red') +
# facet_wrap(~ as.factor(p)) +
# #scale_fill_manual(
# # values = c(
# # "PiS" = "red",
# # "PO" = "violet",
# # "PSL" = "forestgreen",
# # "P2050" = 'lightblue'),
# # na.value = 'white'
# # ) +
# ggtitle('Kandydaci na burmistrzów/prezydentów 7/4/2024',
# subtitle="Oficjalna afiliacja zarejestrowana w PKW") +
# theme(legend.title=element_blank())
###
##r1
r2 <- ggplot(data=f3, aes(fill=z0)) +
geom_sf() +
scale_fill_manual(
values = c(
"PiS" = "red",
"PO" = "violet",
"PSL" = "forestgreen",
"P2050" = 'lightblue'),
na.value = 'white'
) +
ggtitle('Burmistrzowie/prezydenci wybrani 7/4/2024') +
# ## remove legend title
theme(legend.title=element_blank())
# ## remove legend
# ## theme(legend.position="none")
r2