Skip to content

Instantly share code, notes, and snippets.

@resulumit
Last active December 9, 2022 15:08
Show Gist options
  • Save resulumit/f53b01a6de7e5e7596fe351173037b64 to your computer and use it in GitHub Desktop.
Save resulumit/f53b01a6de7e5e7596fe351173037b64 to your computer and use it in GitHub Desktop.
The positional distances between right-wing parties and several other party families
# load the data
library(tidyverse)
# write functions to calculate confidence intervals
lower_ci <- function(mean, se, n, conf_level = 0.95){
lower_ci <- mean - qt(1 - ((1 - conf_level) / 2), n - 1) * se
}
upper_ci <- function(mean, se, n, conf_level = 0.95){
upper_ci <- mean + qt(1 - ((1 - conf_level) / 2), n - 1) * se
}
# get the 1999-2019 chapel hill expert survey (CHES) trend file
ches <- read.csv("https://www.chesdata.eu/s/1999-2019_CHES_dataset_meansv3.csv")
# name the party families
par_fam <- ches %>%
select(year, party_id, family) %>%
mutate(family = recode_factor(family, `1` = "Radical Right", `2` = "Conservative", `3` = "Liberal",
`4` = "Christian-Democratic", `5` = "Socialist", `6` = "Radical Left",
`7` = "Green", `8` = "Regionalist", `10` = "Confessional",
`11` = "Agrarian/Center", `9` = "No Family"))
# select the position variables to be used
par_pos <- ches %>%
select(year, party_id, vote, lrgen, galtan, immigrate_policy)
# start preparing the data
ches %>%
# keep only the large families
filter(family < 8) %>%
# create party combinations
select(country, year, party.x = party_id, party.y = party_id) %>%
group_by(country, year) %>%
complete(party.x, party.y) %>%
ungroup() %>%
# add positions to each party
left_join(., par_fam, by = c("year" = "year", "party.x" = "party_id")) %>%
left_join(., par_fam, by = c("year" = "year", "party.y" = "party_id")) %>%
# keep radical right in the x group, others in the y group
filter(family.x == "Radical Right" & family.y != "Radical Right") %>%
left_join(., par_pos, by = c("year" = "year", "party.x" = "party_id")) %>%
left_join(., par_pos, by = c("year" = "year", "party.y" = "party_id")) %>%
# calculate the absolute positional differences
mutate(left_right = lrgen.x - lrgen.y,
gal_tan = galtan.x - galtan.y,
immigration = immigrate_policy.x - immigrate_policy.y) %>%
# switch from wide to long data
pivot_longer(c("left_right", "gal_tan", "immigration"),
names_to = "area", values_to = "distance") %>%
# calculate statistics by family and position
group_by(year, family.y, area) %>%
summarise(mean_d = mean(distance, na.rm = TRUE),
sd_d = sd(distance, na.rm = TRUE),
count = n()) %>%
mutate(se = sd_d / sqrt(count),
lower_ci = lower_ci(mean_d, se, count),
upper_ci = upper_ci(mean_d, se, count)) %>%
# plot the data
ggplot(aes(x = year, y = mean_d, color = area)) +
geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci),
width = .1, size = 0.8) +
geom_line(size = 1.2) +
geom_point(size = 1.3) +
facet_wrap(. ~ family.y, nrow = 3) +
theme_bw() +
theme(legend.position = "bottom",
legend.title = element_blank(),
legend.text = element_text(size = 16),
strip.text = element_text(size = 16),
axis.text = element_text(size = 10),
axis.title = element_text(size = 16)) +
scale_x_continuous(breaks = c(1999, 2002, 2006, 2010, 2014, 2019)) +
scale_color_discrete(breaks = c("left_right", "gal_tan", "immigration"),
labels = c("Left-Right", "GAL-TAN", "Immigration")) +
ylab("Mean distance to radical right parties\n") + xlab("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment