Skip to content

Instantly share code, notes, and snippets.

@expersso
Created June 20, 2016 12:53
Show Gist options
  • Save expersso/d046bb33624c20ec906c8b6478d437eb to your computer and use it in GitHub Desktop.
Save expersso/d046bb33624c20ec906c8b6478d437eb to your computer and use it in GitHub Desktop.
Remake of FT chart on ageing
library(tidyr)
library(plyr)
library(dplyr)
library(stringr)
library(ggplot2)
library(scales)
library(ggthemes)
data <-
"
country,double,triple
France (1865-2022) ,115,42
Sweden (1890-2015),85,40
United Kingdom (1930-2030),45,55
Australia (1938-2037),73,26
United States (1944-2033),69,20
Spain (1947-2028),45,36
Hungary (1941-2021),53,27
Poland (1966-2024),45,13
Chile (1999-2041),26,16
Brazil (2012-2050),21,17
Tunisia (2007-2044),24,13
Japan (1970-2007),25,12
Thailand (2003-2038),21,14
China (2001-2035),23,11
South Korea (2000-2027),18,9
"
df <- read.csv(textConnection(data)) %>%
separate(country, c("country", "time"), " \\(") %>%
mutate(time = str_replace(time, "\\)", "")) %>%
separate(time, c("start", "end"), "-") %>%
mutate_each(funs(as.integer), start, end) %>%
mutate(mid = start + double) %>%
select(country,
"7" = start,
"14" = mid,
"21" = end) %>%
gather(perc, year, -country) %>%
mutate(perc = as.integer(perc),
country = revalue(country, c("United States" = "US",
"United Kingdom" = "UK",
"South Korea" = "S. Korea"))
)
p <-
ggplot(df, aes(x = year, y = perc, color = country)) +
geom_line() +
geom_point(alpha = 0.8, size = 1.8) +
geom_text(
aes(label = country),
filter(df, perc == 7),
angle = -45,
size = 3,
hjust = 0,
nudge_x = 2,
nudge_y = -0.5
) +
geom_text(
aes(label = country),
filter(df, perc == 21),
angle = -45,
size = 3,
hjust = 1,
nudge_x = -2,
nudge_y = 1
) +
scale_y_continuous(
labels = function(x) paste0(x, "%"),
limits = c(3, 25),
breaks = c(0, 7, 14, 21),
expand = c(0, 0)
) +
scale_x_continuous(
limits = c(1860, 2055),
breaks = c(seq(1860, 2020, 20), 2050),
expand = c(0, 0)) +
scale_color_manual(
values = c(few_pal("medium")(7), colorblind_pal()(8)[-5], "grey40")
) +
theme(
panel.grid.minor = element_blank(),
legend.position = "none",
axis.ticks = element_line(color = "grey70"),
axis.text = element_text(color = "grey50"),
text = element_text(color = "grey50"),
panel.background = element_rect(fill = "grey98"),
plot.caption = element_text(size = rel(0.6), hjust = 1,
margin = unit(c(3, 0, 1, 0), "mm"))
) +
labs(x = NULL, y = NULL,
title = "Emerging markets are ageing at a rapid rate",
subtitle = "% of population aged 65 and over",
caption = paste0("Source: Kinsella and Gist 1995; ",
"\"An Aging World: 2015\", US Census Bureau")
)
ggsave("age.svg", p, width = 7, height = 4, dpi = 300)
@expersso
Copy link
Author

age

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment