Skip to content

Instantly share code, notes, and snippets.

@expersso
Created April 26, 2018 10:44
Show Gist options
  • Save expersso/1783dad6b8fded3f192b337c49010d69 to your computer and use it in GitHub Desktop.
Save expersso/1783dad6b8fded3f192b337c49010d69 to your computer and use it in GitHub Desktop.
Slope chart for Reporters Without Borders Press Freedom Index
library(tidyverse)
df <-
"https://rsf.org/sites/default/files/import_good_-_index_2018_pour_import.csv" %>%
read_csv(locale = locale(decimal_mark = ",")) %>%
select(`Overall Score 2016`, `Score 2017`, ISO, "country" = EN_country) %>%
mutate(d = `Score 2017` - `Overall Score 2016`) %>%
gather(year, value, -ISO, -country, -d) %>%
mutate(
year = as.numeric(gsub("[^0-9]", "", year)),
country = case_when(
.$ISO == "PRK" ~ "North Korea",
.$ISO == "CZE" ~ "Czechia",
.$ISO == "RUS" ~ "Russia",
.$ISO == "SYR" ~ "Syria",
.$ISO == "IRN" ~ "Iran",
.$ISO == "GBR" ~ "UK",
TRUE ~ .$country
)
)
selected_countries <- df %>%
filter(year == 2017) %>%
filter(rank(value) <= 5 |
rank(-value) <= 5 |
rank(-abs(d)) <= 20 |
ISO %in% c("USA", "GBR", "CHN", "DEU", "FRA", "RUS"))
df <- df %>%
mutate(
highlight = ISO %in% selected_countries$ISO,
label = sprintf(" %s %0.1f ", country, value, 1),
direction = case_when(
.$d > 0 ~ "up",
.$d < 0 ~ "down",
.$d == 0 ~ "no change"
)
)
ggplot(df, aes(
x = year,
y = value,
group = country,
color = direction
)) +
geom_line(
mapping = aes(size = highlight),
alpha = 0.75
) +
geom_point(
data = filter(df, highlight),
size = 1.5
) +
geom_text(
mapping = aes(label = label),
data = filter(df, highlight),
hjust = "outward",
size = 3
) +
scale_color_manual(values = c("#0f5499", "grey50", "#7f062e")) +
scale_x_continuous(breaks = range(df$year), expand = c(1, 0),
sec.axis = sec_axis(~., breaks = range(df$year))) +
scale_y_reverse(expand = c(0.02, 0), breaks = NULL) +
scale_size_manual(values = c(0.1, 0.5)) +
theme_minimal() +
theme(
legend.position = "none",
panel.grid.minor = element_blank(),
plot.title = element_text(face = "bold"),
plot.background = element_rect(fill = "#fff1e5"),
plot.subtitle = element_text(color = "grey30"),
plot.caption = element_text(color = "grey30", hjust = 0, size = 7)
) +
labs(x = NULL,
y = NULL,
color = NULL,
size = NULL,
title = "Press freedom declined in 96 countries\nand increased in 76 countries in 2017",
subtitle = "Press Freedom Index (0 = best, 100 = worst)",
caption = "Source: Reporters Without Borders"
)
@expersso
Copy link
Author

image

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