Skip to content

Instantly share code, notes, and snippets.

@ikashnitsky
Last active May 4, 2022 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikashnitsky/97ad17202c2a7fbde5d4bbd1563d0aa2 to your computer and use it in GitHub Desktop.
Save ikashnitsky/97ad17202c2a7fbde5d4bbd1563d0aa2 to your computer and use it in GitHub Desktop.
RotateTheDamnPlot example, Figure 2 from doi.org/10.4054/DemRes.2022.46.29, see https://twitter.com/ikashnitsky/status/1521960898440613889
#===============================================================================
# 2022-05-04 -- twitter
# Improve plot
# https://twitter.com/ikashnitsky/status/1521960898440613889
# Ilya Kashnitsky, ilya.kashnitsky@gmail.com, @ikashnitsky
#===============================================================================
# Corker, J., Jamaica, Rossier, C., & Zan, L. M. (2022). Fertility among better-off women in sub-Saharan Africa: Nearing late transition levels across the region. Demographic Research, 46(29), 849–864. https://doi.org/10.4054/DemRes.2022.46.29
# Improve figure 2 -- RotateTheDamnPlot
library(tidyverse)
library(magrittr)
library(hrbrthemes)
# read and reshape the data
raw <- read_lines(
"https://gist.githubusercontent.com/ikashnitsky/97ad17202c2a7fbde5d4bbd1563d0aa2/raw/c2899dadcf89719c8d342bfdbb99980b603ca701/the-data-table-2.txt",
skip = 48, skip_empty_rows = T
)
dhs <- raw %>%
matrix(ncol = 15, byrow = T) %>%
as_tibble() %>%
set_colnames(
c(
"country",
"all_1549",
"all_2049",
"some_secondary_school+better_off",
"some_secondary_school+other",
"upper_welth_quintile+better_off",
"upper_welth_quintile+other",
"completed_secondary_school+better_off",
"completed_secondary_school+other",
"upper_wealth_decile+better_off",
"upper_wealth_decile+other",
"some_tertiary_education+better_off",
"some_tertiary_education+other",
"upper_wealth_quintile+better_off",
"upper_wealth_quintile+other"
)
) %>%
mutate(
country = country %>% str_remove_all('"')
) %>%
mutate_at(2:14, .funs = as.numeric) %>%
arrange(`completed_secondary_school+better_off`) %>%
mutate(
country = country %>% as_factor
)
# plot
dhs %>%
ggplot(aes(y = country))+
geom_hline(
data = . %>% filter(country == "Pooled sample"),
aes(yintercept = country),
size = 5, color = "#eaeaea"
)+
geom_point(
aes(x = `completed_secondary_school+better_off`),
color = "#024281"
)+
geom_point(
aes(x = `some_secondary_school+better_off`),
color = "#BC6C82"
)+
geom_point(
aes(x = `all_2049`),
color = "#edc177"
)+
geom_text(
data = tibble(
label = c(
"Completed\nsecondary\neducation",
"Some\nsecondary\neducation",
"All women\naged\n20-49"
)
),
x = c(4.3, 5.9, 6.9), y = 29,
aes(label = label),
vjust = 0, size = 3,
color = c("#024281", "#BC6C82", "#edc177"),
family = font_rc, lineheight = .8
)+
coord_cartesian(ylim = c(0, 33), xlim = c(0, 8))+
theme_minimal(base_family = font_rc)+
theme(
axis.text.y = element_text(face = 2, size = 10)
)+
labs(
x = "Total fertility rate",
y = NULL
)
All
All
Some secondary
school (1)
Upper wealth quintile (2)
Completed
secondary school (3)
Upper wealth
decile (4)
Some tertiary
education (5)
Upper wealth
quintile
(6)
Country
15-49
20-49
Better-off
Other Better-off
Other Better-off
Other Better-off Other Better-off
Other
Better-off
Other
Pooled sample
5.1
5.5
4.0
6.3
3.7
6.0
3.2
5.8
3.1
5.8
2.7
5.7
2.9
5.6
Benin 2017-2018
5.7
6.1
4.6
6.6
4.6
6.5
3.3
6.2
3.8
6.3
3.2
6.2
3.3
6.2
Burkina Faso 2010
6.0
6.4
3.3
6.8
4.0
7.2
3.1
6.5
3.0
6.8
3.3
6.5
2.7
6.6
Burundi 2016-2017
5.5
5.9
4.2
6.6
4.6
6.2
3.6
5.9
3.4
6.1
3.0
5.9
2.9
6.0
Cameroon 2018-2019
4.8
5.1
4.1
6.3
3.2
5.7
3.1
5.5
3.1
5.4
3.0
5.4
3.0
5.2
DR Congo 2014
6.6
6.9
5.9
7.9
5.1
7.5
4.3
7.4
4.1
7.3
2.9
7.1
3.6
7.1
"Côte d'Ivoire 2012"
5.0
5.2
2.8
5.8
3.3
5.8
2.3
5.5
2.9
5.5
2.2
5.3
3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.5
5.5
1.2
5.4
2.4
5.4
Tanzania 2015-2016
5.2
5.5
3.9
6.2
3.3
6.4
3.6
6.0
2.8
5.9
3.0
5.6
2.8
5.7
Togo 2014
4.8
5.0
3.7
5.8
3.5
5.6
2.8
5.2
3.3
5.3
2.6
5.2
2.9
5.2
Uganda 2016
5.4
5.8
4.5
6.5
4.1
6.4
3.6
6.1
3.6
6.1
3.6
6.0
3.3
5.9
Zambia 2018-2019
4.7
4.9
3.8
6.1
3.2
5.5
2.7
5.5
2.8
5.2
2.4
5.1
2.7
5.1
Zimbabwe 2015
4.0
4.4
4.1
5.5
2.7
5.1
2.2
4.7
2.2
4.6
2.2
4.6
2.0
4.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment