Skip to content

Instantly share code, notes, and snippets.

@timriffe
Created May 6, 2021 07:14
Show Gist options
  • Save timriffe/b5930e5bd6109f0232c1fc096329330d to your computer and use it in GitHub Desktop.
Save timriffe/b5930e5bd6109f0232c1fc096329330d to your computer and use it in GitHub Desktop.
unas maneras de visualizar una tabla Lexis de las tasas de fecundidad del Pais Vasco
library(tidyverse)
library(readxl)
library(colorspace)
# convertir a formato tidy y crear coordinatas centricas
ISF_tidy <-
read_excel("ISF_PC.xlsx", sheet = "simplified") %>%
pivot_longer(`2015-2011`:`1930-1926`, names_to = "YOB", values_to = "ASFR") %>%
filter(!is.na(ASFR)) %>%
rename(Mother_Cohort = Generaciones) %>%
separate(Mother_Cohort, into = c("MC_upper","MC_low"), convert = TRUE) %>%
separate(YOB, into = c("YOB_upper","YOB_low"), convert = TRUE) %>%
mutate(AgeCenter = (YOB_low + YOB_upper) / 2 - (MC_upper + MC_low) / 2,
YOBCenter = (YOB_low + YOB_upper) / 2,
MCCenter = (MC_upper + MC_low) / 2,
AgeCenter = YOBCenter - MCCenter)
a <-
ISF_tidy %>%
ggplot(aes(x = AgeCenter, y = ASFR, group = MCCenter, color = MCCenter)) +
geom_line(size = 1.5) +
labs(x = "Edad", y = "Tasa",
title = "Las tasas especificas por edad de las generaciones",
subtitle = "Colores oscuros son generaciones mas recientes\nLa area por debajo de cada curva es la decendencia final (DF)",color = "Generacion\nmaterna") +
annotate("text",x = 27, y = 360, label="error or real? ---> ") +
scale_color_continuous_sequential("ag_GrnYl")
b <-
ISF_tidy %>%
ggplot(aes(x = AgeCenter, y = ASFR, group = YOBCenter, color = YOBCenter)) +
geom_line(size = 1.5) +
labs(x = "Edad", y = "Tasa / mil",
title = "Las tasas especificas por edad de cada año",
subtitle = "Colores oscuros son años mas recientes\nLa area por debajo de cada curva es el ISF que lees en los periodicos",
color = "Año") +
scale_color_continuous_sequential("ag_Sunset")
cc <-
ISF_tidy %>%
ggplot(aes(x = YOBCenter, y = ASFR, group = MCCenter, color = MCCenter)) +
geom_line(size = 1.5) +
scale_color_continuous_sequential("ag_GrnYl") +
labs(title = "Las tasas especificas cada generacion (color de curva)\nsobre el tiempo",
subtitle = "la suma transversal de cada momento es el ISF\nLa area por debajo de cada curva es la DF",
x = "Año", y = "Tasa / mil",
color = "Generacion\nmaterna")
d <-
ISF_tidy %>%
ggplot(aes(x = MCCenter, y = ASFR, group = YOBCenter, color = YOBCenter)) +
geom_line(size = 1.5) +
scale_color_continuous_sequential("ag_Sunset") +
labs(title = "Las tasas especificas de cada año (color de curva)\nasignadas a las generaciones",
subtitle = "la suma transversal de cada momento es la DF\nLa area por debajo de cada curva es el ISF",
x = "Generacion materna", y = "Tasa / mil", color = "Año")
plot_grid(a, b, cc, d, labels = c('A', 'B', 'C', 'D'), label_size = 12)
e <-
ISF_tidy %>%
ggplot(aes(x = MCCenter, y = ASFR * 5 / 1000, group = YOBCenter, fill = YOBCenter)) +
geom_area(size = 1.5) +
labs(title = "Composicion temporal de las tasas de las generaciones",
subtitle = "Un corte vertical son las tasas de una generacion\nBandas de color indican cuando en el tiempo se produjeron",
fill = "Año", x = "Generacion materna", y = "Tasa / mil") +
scale_fill_continuous_sequential("ag_Sunset") +
annotate("rect",
xmin = 1975, xmax = 2000, ymin = 0, ymax = 270 * 5 / 1000,
fill = "#00000020") +
xlim(1895,2010)+
annotate("text", 1987, 278* 5 / 1000, label="decendencia no completa")
f <-
ISF_tidy %>%
ggplot(aes(x = YOBCenter, y = ASFR * 5 / 1000, group = MCCenter, fill = MCCenter)) +
geom_area(size = 1.5) +
labs(title = "Tasas de las generaciones sobre el tiempo",
subtitle = "Cada banda consiste de las tasas de una generacion
el perfil superior es el ISF, completamente observada aqui",
fill = "Generacion\nmaterna",
x = "Año", y = "Tasa / mil") +
xlim(1895,2010)+
scale_fill_continuous_sequential("ag_Sunset")
plot_grid(e,f,nrow = 2,labels = c('E','F'),label_size = 12)
# Tres tipos de Lexis
g <-
ISF_tidy %>%
ggplot(aes(x = YOBCenter, y = AgeCenter, fill = ASFR)) +
geom_tile() +
labs(title = "Superficie Lexis aproximada",
subtitle = "Edad-Periodo",
x = "Año", y = "Edad", fill = "Tasa") +
scale_fill_continuous_sequential("PurpOr")+
coord_fixed()
h <-
ISF_tidy %>%
ggplot(aes(x = MCCenter, y = AgeCenter, fill = ASFR)) +
geom_tile() +
labs(title = "Superficie Lexis aproximada",
subtitle = "Edad-Cohorte",
y = "Edad", x = "Generacion materna", fill = "Tasa") +
scale_fill_continuous_sequential("PurpOr") +
coord_fixed()
i <-
ISF_tidy %>%
ggplot(aes(x = YOBCenter, y = MCCenter, fill = ASFR)) +
geom_tile() +
labs(title = "Superficie Lexis aproximada",
subtitle = "Periodo-Cohorte",
x = "Año", y = "Generacion materna", fill = "Tasa") +
scale_fill_continuous_sequential("PurpOr")+
coord_fixed()
plot_grid(g,h,i, nrow=3,labels = c('G','H','I'), rel_heights = c(1,1,2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment