Skip to content

Instantly share code, notes, and snippets.

@aliaksandrkazlou
Created July 10, 2023 18:09
Show Gist options
  • Save aliaksandrkazlou/abf89e916aceed8f47d5dff9f74f9d33 to your computer and use it in GitHub Desktop.
Save aliaksandrkazlou/abf89e916aceed8f47d5dff9f74f9d33 to your computer and use it in GitHub Desktop.
CH_age.R
#### Libraries ----
library(haven)
library(tidyverse)
library(RColorBrewer)
#### Data ----
df_10 <- read_sav("~/Downloads/Chatham House - Belarus - X.sav")
df_11 <- read_sav("~/Downloads/Chatham House - Belarus - XI.sav")
df_12 <- read_sav("~/Downloads/Chatham House - Belarus - XII.sav")
df_13 <- read_sav("~/Downloads/Chatham House - Belarus - XIII.sav")
df_14 <- read_sav("~/Downloads/Chatham House - Belarus - XIV.sav")
df_15 <- read_sav("~/Downloads/Chatham House - Belarus - XV.sav")
df_10 <- df_10 %>%
mutate(age_group = A4int)
# List of all dataframes
dfs <- list(df_10, df_11, df_12, df_13, df_14, df_15)
# Assign wave number
dfs <- lapply(1:length(dfs), function(i) {
dfs[[i]] %>%
group_by(age_group) %>%
summarise(n = n()) %>%
mutate(freq = n / sum(n)) %>%
mutate(wave = i+9) %>%
select(age_group, freq, wave)
})
# Combine all dataframes
df_all <- bind_rows(dfs)
# Convert wave to integer
df_all$wave <- as.integer(as.character(df_all$wave))
# Format age
df_all$age_group <- as.character(df_all$age_group)
df_all$age_group[df_all$age_group == '1'] <- "[18-24]"
df_all$age_group[df_all$age_group == '2'] <- "[25-34]"
df_all$age_group[df_all$age_group == '3'] <- "[35-44]"
df_all$age_group[df_all$age_group == '4'] <- "[45-54]"
df_all$age_group[df_all$age_group == '5'] <- "[55-99]"
# Convert frequency to percentage
df_all$freq <- df_all$freq * 100
# Set up a color palette
my_palette <- c("#D7191C", "#FDAE61", "#ABDDA4", "#2B83BA", "#581845")
# Subset the first and last waves for the oldest age group
df_labels <- df_all %>%
group_by(age_group) %>%
filter(wave %in% c(min(wave), max(wave))) %>%
filter(age_group == "[55-99]")
#### Plot ----
ggplot(df_all, aes(x=wave, y=freq, group=age_group, color=age_group)) +
geom_line(size = 1) +
scale_color_manual(values = my_palette) +
geom_text(data = df_labels, aes(label = paste0(round(freq, 1), "%")),
check_overlap = FALSE, vjust = -1, nudge_y = 0.1, size = 8, show_guide = FALSE) +
theme_minimal() +
theme(
plot.background = element_rect(fill = "white"),
panel.grid.major = element_line(size = 0.1, linetype = 'solid', colour = "black"),
axis.title = element_text(colour = "black", face="bold", size = 16),
axis.text = element_text(colour = "black", face="bold", size = 16),
axis.title.x = element_text(face="bold", size = 16),
axis.title.y = element_text(face="bold", size = 16),
legend.position="bottom",
legend.text = element_text(face="bold", size = 16),
title = element_text(face="bold", size = 16)
) +
labs(x = "Хваля", y = "%", color = "Узрост") +
ggtitle("Стукрутра выбарак Chatham House", subtitle = "З 06-06-2022 па 27-03-2023")
ggsave("ch_age.png", width = 12, height=7, device='png', dpi=700)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment