Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
################################################################################
#
# Marc Belzunces (Twitter: @marcbeldata)
# marcbeldata.github.io 2017-07-20
#
# Playing with ggjoy.
# Evolution of vote in Catalan regional elections (1980-2015)
#
################################################################################
library(tidyverse)
library(forcats)
library(ggjoy)
library(viridis)
library(grid)
library(gridExtra)
library(RColorBrewer)
#Data obtained and processed from Idescat (not public)
df <- read_csv("Catalan Parliament 1980-2015.csv")
glimpse(df)
head(df)
#Vote for the main parties
#CiU
p1 <- df %>% select(Municipality, INE, Year, CiU_percent) %>%
filter(!is.na(CiU_percent)) %>%
mutate(
Municipality = as.factor(Municipality),
Year = as.factor(Year)
) %>%
ggplot(aes(x = CiU_percent, y = Year %>% fct_rev())) +
geom_joy(aes(fill = Year))+
scale_fill_viridis(discrete = T, option = "D", direction = -1,
begin = .1, end = .9) +
labs(x = "Percent Vote",
y = "Election's Year",
title = "CiU's vote in Catalan elections",
subtitle = "Analysis unit: municipalities",
caption = "Marc Belzunces (@marcbeldata)") +
theme_minimal(base_size = 15) +
theme(legend.position = "none")
#ERC
p2 <- df %>% select(Municipality, INE, Year, ERC_percent) %>%
filter(!is.na(ERC_percent)) %>%
mutate(
Municipality = as.factor(Municipality),
Year = as.factor(Year)
) %>%
ggplot(aes(x = ERC_percent, y = Year %>% fct_rev())) +
geom_joy(aes(fill = Year))+
scale_fill_viridis(discrete = T, option = "A", direction = -1,
begin = .1, end = .9) +
labs(x = "Percent Vote",
y = "Election's Year",
title = "ERC's vote in Catalan elections",
subtitle = "Analysis unit: municipalities",
caption = "Marc Belzunces (@marcbeldata)") +
theme_minimal(base_size = 15) +
theme(legend.position = "none")
#PSC
p3 <- df %>% select(Municipality, INE, Year, PSC_percent) %>%
filter(!is.na(PSC_percent)) %>%
mutate(
Municipality = as.factor(Municipality),
Year = as.factor(Year)
) %>%
ggplot(aes(x = PSC_percent, y = Year %>% fct_rev())) +
geom_joy(aes(fill = Year))+
scale_fill_viridis(discrete = T, option = "B", direction = -1,
begin = .1, end = .9) +
labs(x = "Percent Vote",
y = "Election's Year",
title = "PSC's vote in Catalan elections",
subtitle = "Analysis unit: municipalities",
caption = "Marc Belzunces (@marcbeldata)") +
theme_minimal(base_size = 15) +
theme(legend.position = "none")
#PP
p4 <- df %>% select(Municipality, INE, Year, PP_percent) %>%
filter(!is.na(PP_percent)) %>%
mutate(
Municipality = as.factor(Municipality),
Year = as.factor(Year)
) %>%
ggplot(aes(x = PP_percent, y = Year %>% fct_rev())) +
geom_joy(aes(fill = Year))+
scale_fill_viridis(discrete = T, option = "C", direction = -1,
begin = .1, end = .9) +
labs(x = "Percent Vote",
y = "Election's Year",
title = "PP's vote in Catalan elections",
subtitle = "Analysis unit: municipalities",
caption = "Marc Belzunces (@marcbeldata)") +
theme_minimal(base_size = 15) +
theme(legend.position = "none")
#Comuns
p5 <- df %>% select(Municipality, INE, Year, CSQP_percent) %>%
filter(!is.na(CSQP_percent)) %>%
mutate(
Municipality = as.factor(Municipality),
Year = as.factor(Year)
) %>%
ggplot(aes(x = CSQP_percent, y = Year %>% fct_rev())) +
geom_joy(aes(fill = Year))+
scale_fill_viridis(discrete = T, option = "D", direction = -1,
begin = .1, end = .9) +
labs(x = "Percent Vote",
y = "Election's Year",
title = "ICV/CSQP's vote in Catalan elections",
subtitle = "Analysis unit: municipalities",
caption = "Marc Belzunces (@marcbeldata)") +
theme_minimal(base_size = 15) +
theme(legend.position = "none")
#Cs
p6 <- df %>% select(Municipality, INE, Year, Cs_percent) %>%
filter(!is.na(Cs_percent)) %>%
mutate(
Municipality = as.factor(Municipality),
Year = as.factor(Year)
) %>%
ggplot(aes(x = Cs_percent, y = Year %>% fct_rev())) +
geom_joy(aes(fill = Year))+
scale_fill_viridis(discrete = T, option = "A", direction = -1,
begin = .1, end = .9) +
labs(x = "Percent Vote",
y = "Election's Year",
title = "Cs's vote in Catalan elections",
subtitle = "Analysis unit: municipalities",
caption = "Marc Belzunces (@marcbeldata)") +
theme_minimal(base_size = 15) +
theme(legend.position = "none")
grid.arrange(p1, p2, p3, p4, p5, p6, ncol = 2)
#Vote in terms of independence of Catalonia/Unions to Spain
#Generating a new 11 color palette (thanks to Ilya Kashnitsky tip)
green <- brewer.pal(11,"BrBG")[7:11]
int <- colorRampPalette(green)
eleven <- int(11)
brown <- brewer.pal(11,"BrBG")[5:1]
interpol <- colorRampPalette(brown)
eleven2 <- interpol(11)
#Indy
p7 <- df %>% select(Municipality, INE, Year, Indy_percent) %>%
filter(!is.na(Indy_percent)) %>%
mutate(
Municipality = as.factor(Municipality),
Year = as.factor(Year)
) %>%
ggplot(aes(x = Indy_percent, y = Year %>% fct_rev())) +
geom_joy(aes(fill = Year))+
scale_fill_manual(values = eleven) +
labs(x = "Percent Vote",
y = "Election's Year",
title = "Indy parties' vote in Catalan elections",
subtitle = "Analysis unit: municipalities",
caption = "Marc Belzunces (@marcbeldata)") +
theme_minimal(base_size = 15) +
theme(legend.position = "none")
#Unio
p8 <- df %>% select(Municipality, INE, Year, Unio_percent) %>%
filter(!is.na(Unio_percent)) %>%
mutate(
Municipality = as.factor(Municipality),
Year = as.factor(Year)
) %>%
ggplot(aes(x = Unio_percent, y = Year %>% fct_rev())) +
geom_joy(aes(fill = Year))+
scale_fill_manual(values = eleven2) +
labs(x = "Percent Vote",
y = "Election's Year",
title = "Unionist parties' vote in Catalan elections",
subtitle = "Analysis unit: municipalities",
caption = "Marc Belzunces (@marcbeldata)") +
theme_minimal(base_size = 15) +
theme(legend.position = "none")
grid.arrange(p7,p8, ncol = 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment