Skip to content

Commit

Permalink
Merge pull request #2 from ikashnitsky/main
Browse files Browse the repository at this point in the history
upd appendix figures order
  • Loading branch information
ikashnitsky authored Mar 16, 2021
2 parents 7175527 + ace7984 commit 3219471
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 99 deletions.
Binary file modified out/appendix-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified out/appendix-7.png
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified out/appendix-8.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 43 additions & 43 deletions src/7-prepare-data-for-viz.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,48 @@ save(qxdiff, file = "dat/qxdiff.rda" %>% lp)



# data for sens plateau 0.7 -----------------------------------------------
# appendix six -- decomp changes in the sex gap ----------------

load("dat/gap_decomp.rda" %>% lp)
load("dat/years_max_gap.rda" %>% lp)

# create labels with the period years
years_period <- years_max_gap %>%
pivot_wider(names_from = mark, values_from = year) %>%
transmute(
country,
early = paste0(first, "-\n", mid),
late = paste0(mid, "-\n", last)
) %>%
pivot_longer(early:late, names_to = "period", values_to = "year_label")

# transform the dataset for plotting
df_gap_decomp <- gap_decomp %>%
pivot_longer(
contains("ctb"), names_prefix = "ctb_",
names_to = "period", values_to = "ctb"
) %>%
mutate(
age_group = age %>%
cut(c(0, 1, 15, 40, 60, 80, 111), right = FALSE)
) %>%
group_by(country, period, age_group) %>%
summarise(ctb = ctb %>% sum(na.rm = T)) %>%
ungroup() %>%
mutate(
age_group = age_group %>%
str_replace(",", "-") %>%
as_factor() %>%
lvls_revalue(c("0", "1-14", "15-39", "40-59", "60-79", "80+"))
) %>%
# attach labels
left_join(years_period) %>%
left_join(ids)

save(df_gap_decomp, file = "dat/df_gap_decomp.rda" %>% lp)


# appendix seven -- data for sens plateau 0.7 ---------------------

load("dat/decomp.rda" %>% lp)
load("dat/decomp_de_07.rda" %>% lp)
Expand Down Expand Up @@ -161,7 +202,7 @@ df_plateau <- decomp %>%
save(df_plateau, file = "dat/df_plateau.rda" %>% lp)


# appendix seven -- sensitivity check for age boundary 50 vs 40 ------------
# appendix eight -- sensitivity check for age boundary 50 vs 40 ------------

load("dat/gap33cntrs.rda" %>% lp)

Expand Down Expand Up @@ -196,44 +237,3 @@ df50 <- df %>%
)

save(df40, df50, file = "dat/df_40_50.rda" %>% lp)


# appendix eight -- decomp changes in the sex gap ----------------

load("dat/gap_decomp.rda" %>% lp)
load("dat/years_max_gap.rda" %>% lp)

# create labels with the period years
years_period <- years_max_gap %>%
pivot_wider(names_from = mark, values_from = year) %>%
transmute(
country,
early = paste0(first, "-\n", mid),
late = paste0(mid, "-\n", last)
) %>%
pivot_longer(early:late, names_to = "period", values_to = "year_label")

# transform the dataset for plotting
df_gap_decomp <- gap_decomp %>%
pivot_longer(
contains("ctb"), names_prefix = "ctb_",
names_to = "period", values_to = "ctb"
) %>%
mutate(
age_group = age %>%
cut(c(0, 1, 15, 40, 60, 80, 111), right = FALSE)
) %>%
group_by(country, period, age_group) %>%
summarise(ctb = ctb %>% sum(na.rm = T)) %>%
ungroup() %>%
mutate(
age_group = age_group %>%
str_replace(",", "-") %>%
as_factor() %>%
lvls_revalue(c("0", "1-14", "15-39", "40-59", "60-79", "80+"))
) %>%
# attach labels
left_join(years_period) %>%
left_join(ids)

save(df_gap_decomp, file = "dat/df_gap_decomp.rda" %>% lp)
112 changes: 56 additions & 56 deletions src/8-figures.R
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,58 @@ ggsave(



# appendix six -- palteau 0.7 --------------------------------------------
# appendix six -- change of gap decomposition ---------------------------

load("dat/df_gap_decomp.rda" %>% lp)

df_gap_decomp %>%
ggplot(aes(ctb, age_group))+
geom_vline(
xintercept = 0, size = .5, color = "#999999"
)+
geom_text(
data = . %>% filter(period == "early"),
aes(label = year_label %>% str_wrap(5), color = period),
family = font_rc, fontface = 2,
x = 1.5, y = 1.75, size = 4, lineheight = .9, alpha = .1
)+
geom_text(
data = . %>% filter(period == "late"),
aes(label = year_label %>% str_wrap(5), color = period),
family = font_rc, fontface = 2,
x = -1.5, y = 1.75, size = 4, lineheight = .9, alpha = .1
)+
geom_col(
aes(fill = period, color = period),
stat="identity", position="dodge", color = NA, width =.5
)+
facet_wrap(~name, ncol = 5, dir = "v")+
scale_fill_manual(values = c("#003737FF", "#3FB3F7FF"))+
scale_color_manual(values = c("#003737FF", "#3FB3F7FF"))+
theme_minimal(base_family = font_rc, base_size = 14)+
theme(
legend.position = "none",
panel.grid.minor = element_blank(),
panel.grid.major = element_line(size = .1),
panel.ontop = T
)+
labs(
y = "Age group, years",
x = "Absolute contribution, years",
title = "Age-specific contributions to the change in the sex gap in life expectancy at birth" %>% str_wrap(60)
)

six_app <- last_plot()

ggsave(
"out/appendix-6.png" %>% lp,
six, width = 8, height = 10,
type = "cairo-png"
)



# appendix seven -- palteau 0.7 --------------------------------------------

load("dat/df_plateau.rda" %>% lp) # data is just for the Germany

Expand All @@ -910,17 +961,17 @@ df_plateau %>%
title = "Age-specific contribution to sex gap in life expectancy at birth",
subtitle = "Comparison of the two assumptions of plateau level, Germany")

six_app <- last_plot()
seven_app <- last_plot()

ggsave(
"out/appendix-6.png" %>% lp,
six_app, width = 6, height = 4.5,
"out/appendix-7.png" %>% lp,
seven_app, width = 6, height = 4.5,
type = "cairo-png"
)



# appendix seven -- sensitivity check for age boundary 50 vs 40 ------------
# appendix eight -- sensitivity check for age boundary 50 vs 40 ------------
load("dat/df_40_50.rda" %>% lp)

df40 %>% ggplot()+
Expand Down Expand Up @@ -965,57 +1016,6 @@ df40 %>% ggplot()+
title = "Average contribution per year of age in the age group")


seven_app <- last_plot()

ggsave(
"out/appendix-7.png" %>% lp,
seven_app, width = 8, height = 10,
type = "cairo-png"
)



# appendix eight -- change of gap decomposition ---------------------------

load("dat/df_gap_decomp.rda" %>% lp)

df_gap_decomp %>%
ggplot(aes(ctb, age_group))+
geom_vline(
xintercept = 0, size = .5, color = "#999999"
)+
geom_text(
data = . %>% filter(period == "early"),
aes(label = year_label %>% str_wrap(5), color = period),
family = font_rc, fontface = 2,
x = 1.5, y = 1.75, size = 4, lineheight = .9, alpha = .1
)+
geom_text(
data = . %>% filter(period == "late"),
aes(label = year_label %>% str_wrap(5), color = period),
family = font_rc, fontface = 2,
x = -1.5, y = 1.75, size = 4, lineheight = .9, alpha = .1
)+
geom_col(
aes(fill = period, color = period),
stat="identity", position="dodge", color = NA, width =.5
)+
facet_wrap(~name, ncol = 5, dir = "v")+
scale_fill_manual(values = c("#003737FF", "#3FB3F7FF"))+
scale_color_manual(values = c("#003737FF", "#3FB3F7FF"))+
theme_minimal(base_family = font_rc, base_size = 14)+
theme(
legend.position = "none",
panel.grid.minor = element_blank(),
panel.grid.major = element_line(size = .1),
panel.ontop = T
)+
labs(
y = "Age group, years",
x = "Absolute contribution, years",
title = "Age-specific contributions to the change in the sex gap in life expectancy at birth" %>% str_wrap(60)
)

eight_app <- last_plot()

ggsave(
Expand Down

0 comments on commit 3219471

Please sign in to comment.