Skip to content

Instantly share code, notes, and snippets.

@harkanwals
Created September 23, 2019 13:03
Show Gist options
  • Save harkanwals/b7d7faf80d2fc54671ee1243ded709c9 to your computer and use it in GitHub Desktop.
Save harkanwals/b7d7faf80d2fc54671ee1243ded709c9 to your computer and use it in GitHub Desktop.
ethnicity_slope_chart_nz
require(tidyr)
require(dplyr)
require(ggrepel)
require(ggplot2)
require(kableExtra)
tibble::tribble(
~ethnicity_group, ~y2013, ~y2018,
"European", 74, 70.2,
"Māori", 14.9, 16.5,
"Asian", 11.8, 15.1,
"Pacific peoples", 7.4, 8.1,
"Middle Eastern/Latin American/African", 1.2, 1.5,
"Other ethnicity", 1.7, 1.2
) -> eth_change
eth_change %>% pivot_longer(-ethnicity_group, names_to= "year", values_to = "proportion", names_prefix="y") %>% mutate(year = as.double(year)) -> eth_change
colors <- c("#ffa600","#003f5c", "#444e86", "#955196", "#dd5182", "#ff6e54", )
ggplot(data = eth_change, aes(x = year, y = proportion, group = ethnicity_group, color =ethnicity_group)) +
geom_point(aes( alpha = 1), size = 4) +
scale_color_manual(values = colors) +
geom_line(aes (alpha = 1), size = 1) +
geom_text_repel(data = eth_change %>% filter(year == 2013),
aes(label = paste0(ethnicity_group, ", ", proportion, "%")) ,
hjust = "left",
size = 4,
nudge_x = -.45,
direction = "y") +
geom_text_repel(data = eth_change %>% filter(year == 2018) ,
aes(label = paste0(ethnicity_group, ", ", proportion, "%")) ,
hjust = "right",
size = 4,
nudge_x = .5,
direction = "y") +
# move the x axis labels up top
scale_x_continuous(position = "top", breaks=c(2013, 2018)) +
theme_bw() +
# Format tweaks
# Remove the legend
theme(legend.position = "none") +
# Remove the panel border
theme(panel.border = element_blank()) +
# Remove just about everything from the y axis
theme(axis.title.y = element_blank()) +
theme(axis.text.y = element_blank()) +
theme(panel.grid.major.y = element_blank()) +
theme(panel.grid.minor.y = element_blank()) +
# Remove a few things from the x axis and increase font size
theme(axis.title.x = element_blank()) +
theme(panel.grid.major.x = element_blank()) +
theme(panel.grid.minor.x = element_blank()) +
theme(axis.text.x.top = element_text(size=12)) +
# Remove x & y tick marks
theme(axis.ticks = element_blank()) +
# Format title & subtitle
theme(plot.title = element_text(size=14, hjust = 0.5)) +
theme(plot.subtitle = element_text(hjust = 0.5)) +
labs(
title = "Change in NZ ethnic composition",
subtitle = "When a person reported more than one ethnic group, there were counted twice.",
caption = "Source: StatsNZ, 2018 Census"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment