-
-
Save mschnetzer/875c0bbf047be3257c258b5011e1ac1d to your computer and use it in GitHub Desktop.
library(tidyverse) | |
library(eurostat) | |
library(countrycode) | |
library(gt) | |
library(gtExtras) | |
data <- get_eurostat("prc_hicp_manr", filters = list(coicop = "CP00")) | |
ea.avg <- data |> filter(geo == "EA") |> slice_max(time, n=1) |> pull(values) | |
findat <- data |> | |
left_join(codelist |> select(eurostat, country.name.de), by = c("geo"="eurostat")) |> | |
filter(time >= "2019-01-01", geo %in% c(eurostat::ea_countries$code, "HR")) |> | |
select(geo, country.name.de, time, values) |> | |
drop_na() | |
trend <- findat |> summarise(trend = list(values), .by = geo) | |
plotdat <- findat |> slice_max(time, n=1, by = geo) |> | |
mutate(abw = values - ea.avg) |> | |
left_join(trend) | |
plotdat |> arrange(desc(values)) |> | |
mutate(geo = ifelse(geo == "EL", "GR", geo)) |> | |
gt() |> | |
fmt_number(columns = values, locale = "de", decimals = 1, pattern = "{x}%") |> | |
fmt_number(columns = abw, locale = "de", decimals = 1, force_sign = T) |> | |
fmt_date(columns = time, rows = everything(), date_style = "yM", locale = "en") |> | |
fmt_flag(columns = geo, height = "1.5em") |> | |
gt_plt_sparkline(column = trend, same_limit = F, | |
palette = c(rep("black", 2), rep("transparent", 3))) |> | |
cols_label(geo = "", country.name.de = "", | |
time = "Monat", values = "Inflation", abw = "Abw. Ø") |> | |
cols_width(country.name.de ~ px(150), values ~ px(100), abw ~ px(80)) |> | |
cols_align(align = "center", columns = c(time, values, abw)) |> | |
data_color(columns = values, method = "numeric", palette = "Reds", alpha = 0.9) |> | |
gt_highlight_rows(rows = geo == "AT", columns = c(geo, country.name.de, time), | |
target_col = country.name.de, bold_target_only = T, fill = "transparent") |> | |
tab_header(title = html("Inflationsraten in der Eurozone")) |> | |
tab_footnote(footnote = "Harmonisierte Verbraucherpreisindizes", | |
locations = cells_column_labels(values)) |> | |
tab_footnote(footnote = "Abweichung zum Durchschnitt der Eurozone", | |
locations = cells_column_labels(abw)) |> | |
tab_source_note(source_note = html("<p style='text-align:right;'>Daten: Eurostat. Grafik: @matschnetzer</p>")) |> | |
gt_theme_538() |> | |
tab_options(heading.title.font.size = 24, footnotes.padding = 0, | |
footnotes.font.size = 10, | |
source_notes.font.size = 10) |> | |
gtsave(filename = "eu_inflation_table.html") |
Hi Luis,
seems like the class of the time variable is factor but should be date. Actually, eurostat::get_eurostat
has a default option time_format = "date"
but perhaps it did not work in your setup.
Try installing the latest version of the eurostat package (remotes::install_github("ropengov/eurostat")
) and reload the data (and clean your cache before eurostat::clean_eurostat_cache()
) or otherwise manually change the class to date.
Best, Matthias
Hi Matthias,
thanks for all your support. After trying all your suggestions the one that solved the problem was to change the class of time variable to date, by using: data$time <- ymd(paste0(as.character(data$time), "-01"))
Everything works fine.
Thanks a lot, looking forward to your next project,
Luis
Glad to read that it worked out. For more "efficient" coding you could even use the truncated
option in ymd
to parse incomplete dates:
mutate(time = ymd(time, truncated =2))
Best, MS
Hi, thanks for sharing the code, I`m trying to replicate the code in other to learn, and a warning occurs in RStudio:
Warning message:
There was 1 warning in
filter()
.ℹ In argument:
time >= "2019-01-01"
.Caused by warning in
Ops.factor()
:! ‘>=’ not meaningful for factors
Could you please help me how to solve this warning?
Thank you,
Luis Barqueira