Created
March 8, 2018 12:06
-
-
Save giocomai/ab3e14574e59c088e239024940d6196f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("geofacet") | |
library("ggplot2") | |
library("tidyverse") | |
library("scales") | |
library("readxl") | |
# dati ripuliti: https://data.world/sapomnia/elezioni-2018/workspace/file?filename=Risultati%20elezioni%20camera%202018%20livello%20comunale.xlsx | |
# basato su: https://github.com/ondata/elezionipolitiche2018 | |
comune <- readxl::read_excel("Risultati elezioni camera 2018 livello comunale.xlsx") | |
regione <- comune %>% | |
mutate(Regione = str_replace_all(string = Regione, pattern = fixed("Trentino Alto Adige"), replacement = "TrentinoAlto Adige/Südtirol")) %>% | |
mutate(Lista = str_replace_all(string = Lista, pattern = "Partito democratico", replacement = "Partito Democratico")) %>% | |
group_by(Regione, Lista) %>% | |
replace_na(replace = list(`Voti assoluti` = 0)) %>% | |
summarise(totaleVoti = sum(`Voti assoluti`)) %>% | |
group_by(Regione) %>% | |
mutate(Share = totaleVoti/sum(totaleVoti)) %>% | |
mutate(Check = sum(Share)) %>% | |
ungroup() %>% | |
mutate(Regione = str_replace_all(string = Regione, pattern = fixed("TrentinoAlto Adige/Südtirol"), replacement = "Trentino-Alto Adige")) %>% | |
mutate(Regione = str_replace_all(string = Regione, pattern = "EmiliaRomagna", replacement = "Emilia-Romagna")) %>% | |
mutate(Regione = str_replace_all(string = Regione, pattern = "Valle d'Aosta/Vallée d'Aoste", replacement = "Valle d'Aosta")) %>% | |
mutate(Regione = str_replace_all(string = Regione, pattern = "FriuliVenezia Giulia", replacement = "Friuli-Venezia Giulia")) %>% | |
mutate(Lista = factor(x = Lista, levels = rev(unique(Lista)))) | |
ggplot(regione, aes(x = Lista, y = Share, fill = Lista)) + | |
geom_col() + | |
scale_x_discrete(name = "", labels = NULL) + | |
scale_y_continuous(name = "") + | |
# scale_fill_manual(values = c("#d3dddc", "#446455" , "#fdd262", "#c7b19c")) + | |
scale_fill_manual(values = rev(c("#335b8e", "#b5b867" , "#fdd262", "#bc5e21"))) + | |
coord_flip() + | |
facet_geo(~ Regione, grid = "italy_grid1") + | |
guides(fill = guide_legend(reverse = TRUE)) + | |
theme_bw(base_family = "Carlito") + | |
theme(legend.title=element_blank(), | |
legend.position="bottom", | |
strip.background =element_rect(fill="white"), | |
axis.ticks.x = element_blank(), | |
axis.ticks.y = element_blank(), | |
axis.text.x = element_blank(), | |
panel.background = element_blank()) + | |
labs(title = "Elezioni politiche 2018 - Camera dei deputati") | |
ggsave(filename = "italy_vote_2018.png", width = 6, height = 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment