plot_spark_color <- function(df) {
ggplot(df, aes(x = year, y = perc, color = line_color)) +
geom_line(size = 20) +
theme_void() +
scale_color_identity()
}
phd_degrees_other_plots_color <- phd_degrees_other |>
mutate(line_color = case_when(
field == "Education" ~ "#9D6C06",
field == "Engineering" ~ "#077DAA",
field == "Humanities and arts" ~ "#026D4E",
field == "Life sciences" ~ "#A39A09",
field == "Mathematics and computer sciences" ~ "#044F7E",
field == "Other" ~ "#954000",
field == "Psychology and social sciences" ~ "#984B77"
)) |>
nest(field_df = c(year, perc, line_color)) |>
mutate(plot = map(field_df, plot_spark_color)) |>
arrange(field)
phd_degrees_other |>
pivot_wider(names_from = year, values_from = perc) |>
arrange(field) |>
mutate(ggplot = NA, .after = field) |>
gt() |>
text_transform(
locations = cells_body(columns = ggplot),
fn = \(x) {
map(phd_degrees_other_plots_color$plot, ggplot_image, height = px(15), aspect_ratio = 4)
}
) |>
cols_width(ggplot ~ px(100)) |>
cols_align(align = "left", columns = field) |>
fmt_percent(columns = where(is.numeric), decimals = 0) |>
tab_style(style = cell_text(color = "#9D6C06"), locations = cells_body(rows = 1, columns = field)) |>
tab_style(style = cell_text(color = "#077DAA"), locations = cells_body(rows = 2, columns = field)) |>
tab_style(style = cell_text(color = "#026D4E"), locations = cells_body(rows = 3, columns = field)) |>
tab_style(style = cell_text(color = "#A39A09"), locations = cells_body(rows = 4, columns = field)) |>
tab_style(style = cell_text(color = "#044F7E"), locations = cells_body(rows = 5, columns = field)) |>
tab_style(style = cell_text(color = "#954000"), locations = cells_body(rows = 6, columns = field)) |>
tab_style(style = cell_text(color = "#984B77"), locations = cells_body(rows = 7, columns = field)) |>
cols_label(field = "Field", ggplot = "Trend") |>
tab_spanner(label = "Popular Bachelor's degrees over the years", columns = everything()) |>
tab_style(style = cell_text(weight = "bold"), locations = cells_column_spanners())