-
Notifications
You must be signed in to change notification settings - Fork 32
/
03_life_expectancy_at_birth.Rmd
72 lines (56 loc) · 2.71 KB
/
03_life_expectancy_at_birth.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
name: lifeexpect
# Life Expectancy Increases
To dramatically show the increases in life expectancy by country from 1960 to 2010, I plot the variable in 1960 versus itself in 2010. The line of equivilance (a 45% angle) is used as a reference and shows the result that you would see if there where no growth. The vertical distance from this line is the increase in life expectancy. I also superimpose a linear model on top of the scatter plot. You can see that the gains are greater for countries that started off with lower life expectancies.
```{r, echo = F}
# Public holiday data
data0 <- readxl::read_xlsx("raw_data/Life Expectancy at Birth.xlsx") %>%
filter(!is.na(Region), Gender == "Total") %>%
select(-Gender) %>%
mutate(CountryName = countrycode::countrycode(`Country Name`, "country.name", "country.name"))
Variation <- data0 %>%
group_by(`Country Name`) %>%
summarise(sd = sd(`Life Expectancy`, na.rm = T)) %>%
arrange(sd) %>%
filter(!is.na(sd))
data <- data0 %>% inner_join(Variation)
compare <- inner_join(
data0 %>% filter(Year == 1960) %>%
rename(`Life Expectancy 1960` = `Life Expectancy`) %>%
select(`Life Expectancy 1960`, `Country Code`),
data0 %>% filter(Year == 2010) %>% rename(`Life Expectancy 2010` = `Life Expectancy`)
) %>%
mutate(squared = `Life Expectancy 1960`^2)
```
A random sample from the data set:
```{r, echo = F}
knitr::kable(sample_n(compare, 5), format = "html")
```
---
```{r life_expect, eval = F, echo = F}
ggplot(compare) +
aes(x = `Life Expectancy 1960`) +
aes(y = `Life Expectancy 2010`) +
geom_point() +
geom_smooth(se = F, method = "lm") +
geom_abline(slope = 1, intercept = 0, lty = 2) +
aes(xend = `Life Expectancy 1960`) +
aes(yend = `Life Expectancy 1960`) +
geom_segment(mapping = aes(col = "Gain from 1960 to 2010")) +
geom_segment(mapping = aes(y = 0, col = "Country Expectancy in 1960")) +
scale_color_manual( breaks = c("Gain from 1960 to 2010", "Country Expectancy in 1960"), values = c("grey59", "grey30", "grey30")) +
geom_point(aes(y = `Life Expectancy 1960`), col = "grey30") +
geom_point() +
labs(subtitle = "@EvaMaeRey | source: data.worldbank.org", size = .7) +
labs(title = "Life Expectancy at Birth by Country") +
labs(col = "") +
theme(legend.title = element_blank()) +
theme_bw() +
xlim(c(20, 80))
```
```{r, echo = F, warning=F, message=F, eval = T, fig.show='hide'}
get_what_save_what <- "life_expect"
eval(parse(text = paste(knitr:::knit_code$get(get_what_save_what), collapse = "")))
ggsave(paste0("figures/", get_what_save_what, ".png"), dpi = 300)
```
`r paste(knitr::knit(text = partial_knit_chunks("life_expect")), collapse = "\n")`
---