Skip to content

Instantly share code, notes, and snippets.

@mschnetzer
Created February 16, 2025 07:39
Show Gist options
  • Save mschnetzer/d4825c2fc8aa3882d9a1630359622444 to your computer and use it in GitHub Desktop.
Save mschnetzer/d4825c2fc8aa3882d9a1630359622444 to your computer and use it in GitHub Desktop.
Korruptionsindex 2024
library(tidyverse)
library(readxl)
library(gt)
library(gtExtras)
library(gtUtils)
library(countrycode)
library(colorspace)
# Download https://images.transparencycdn.org/images/CPI2024-Results-and-trends.xlsx
data <- read_xlsx("CPI2024-Results-and-trends.xlsx",
sheet = "CPI Historical", skip = 1)
tabdata <- data |>
mutate(Change = lag(Rank) - Rank,
`Country / Territory` = countrycode(ISO3, origin = "iso3c", destination = "country.name.de")) |>
mutate(Timeline = list(`CPI score`), .by = ISO3) |>
filter(Year == 2024, Rank <= 5 | ISO3 %in% c("AUT", "DEU", "USA") | Rank >= 178) |>
arrange(Rank) |>
select(Rang = Rank, Änderung = Change, ISO3, Land = `Country / Territory`,
Index = `CPI score`, Timeline)
tabdata |>
gt() |>
fmt_flag(columns = ISO3, height = "1.1em") |>
gt_fa_rank_change(Änderung, font_color = "match", fa_type = "arrow",
palette = c("darkolivegreen", "darkgrey", "firebrick")) |>
gt_plt_bar_pct(Index, labels = T, scaled = T, fill = "cadetblue",
background = lighten("cadetblue", 0.4)) |>
gt_plt_sparkline(Timeline, same_limit = F, type = "default",
palette = c("black", "black", rep("transparent", 3))) |>
gt_highlight_rows(rows = Land == "Österreich",
fill = "cadetblue", alpha = 0.2) |>
cols_merge(columns = c(ISO3, Land), pattern = "{1} {2}") |>
cols_align(align = "center", columns = Index) |>
cols_label(Änderung = "", ISO3 = "Land", Index = "Index 2024", Timeline = "Index seit 2012") |>
tab_header(title = html("Politskandale und Freunderlwirtschaft"),
subtitle = html("Österreich 2024 mit historischem Tief bei Korruptionsindex")) |>
tab_source_note(source_note = html("<p style='text-align:right;'>Daten: Transparency International. Grafik: @matschnetzer</p>")) |>
gt_theme_538() |>
tab_style(style = "padding-right:15px;",
locations = cells_body(columns = Änderung)) |>
gtsave("korruptionsindex.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment