Skip to content

Instantly share code, notes, and snippets.

@gorkang
Created March 27, 2021 18:05
Show Gist options
  • Save gorkang/3c219c4c408c21ca3ded3d18509ea21c to your computer and use it in GitHub Desktop.
Save gorkang/3c219c4c408c21ca3ded3d18509ea21c to your computer and use it in GitHub Desktop.
library(tidyverse)
library(haven)
DF = read_sav("3312.sav") # Datos de http://www.cis.es/cis/opencm/ES/1_encuestas/estudios/ver.jsp?estudio=14551
DIC = lapply(DF, attr, which = "label") %>% as_tibble() %>% pivot_longer(1:ncol(.)) %>% rename(nombre = value)
DF_plot =
DF %>%
sjlabelled::remove_all_labels() %>%
as_tibble() %>%
select(SEXO, EDAD, P10, matches("P11")) %>%
pivot_longer(3:ncol(.)) %>%
left_join(DIC, by = "name") %>%
mutate(EDAD = as.integer(EDAD),
SEXO = as.factor(
case_when(
SEXO == 1 ~ "Hombre",
SEXO == 2 ~ "Mujer")),
value =
case_when(
value == 2 ~ 0,
value == 1 ~ 1),
nombre =
case_when(
nombre == "Se ha sentido muy preocupado/a por muchas cosas sin poder controlarlo" ~ "Preocupado/a por muchas cosas sin poder controlarlo",
nombre == "Incidencia de ataques de ansiedad desde que comenzó la pandemia" ~ "Ataques de ansiedad desde que comenzó la pandemia",
TRUE ~ nombre
)) %>%
group_by(SEXO, EDAD, nombre, name) %>%
summarise(value = mean(value, na.rm = TRUE),
N = n())
PLOT = DF_plot %>%
ggplot(aes(EDAD, value, color = nombre, linetype = SEXO, shape = SEXO)) +
geom_point(alpha = .4, aes(size = N), show.legend = FALSE) +
geom_smooth(se = FALSE) +
theme_minimal(base_size = 12) +
theme(legend.position = "none") +
scale_y_continuous(labels = scales::percent_format(), n.breaks = 10) +
scale_x_continuous(n.breaks = 10, limits = c(0,90)) +
facet_wrap(~nombre, nrow = 2) +
labs(caption = "Línea discontínua: mujeres; Línea continua: hombres\nDatos: CIS; @gorkang")
ggsave(PLOT, filename = "plot.png", dpi = 150, width = 20, height = 12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment