library(gdtools)
font_set_liberation()
library(ggplot2)
library(patchwork)
set_flextable_defaults(
font.family = "Liberation Sans",
font.size = 10,
big.mark = "",
border.color = "grey60"
)
# Adapted from
dataset <- data.frame(
team = c(
"FC Bayern Munchen", "SV Werder Bremen", "Borussia Dortmund",
"VfB Stuttgart", "Borussia M'gladbach", "Hamburger SV",
"Eintracht Frankfurt", "FC Schalke 04", "1. FC Koln",
"Bayer 04 Leverkusen"
),
matches = c(2000, 1992, 1924, 1924, 1898, 1866, 1856, 1832, 1754, 1524),
won = c(1206, 818, 881, 782, 763, 746, 683, 700, 674, 669),
lost = c( 363, 676, 563, 673, 636, 625, 693, 669, 628, 447)
)
dataset$win_pct <- dataset$won / dataset$matches * 100
dataset$loss_pct <- dataset$lost / dataset$matches * 100
dataset$team <- factor(dataset$team, levels = rev(dataset$team))
# -- dumbbell chart --
pal <- c(lost = "#EFAC00", won = "#28A87D")
df_long <- reshape(dataset, direction = "long",
varying = list(c("loss_pct", "win_pct")),
v.names = "pct", timevar = "type",
times = c("lost", "won"), idvar = "team"
)
p <- ggplot(df_long, aes(x = pct / 100, y = team)) +
stat_summary(
geom = "linerange", fun.min = "min", fun.max = "max",
linewidth = .7, color = "grey60"
) +
geom_point(aes(fill = type), size = 4, shape = 21,
stroke = .8, color = "white"
) +
scale_x_continuous(
labels = scales::percent,
expand = expansion(add = c(.02, .02))
) +
scale_y_discrete(name = NULL, guide = "none") +
scale_fill_manual(
values = pal,
labels = c(lost = "Lost", won = "Won")
) +
labs(x = NULL, fill = NULL) +
theme_minimal(base_family = "Liberation Sans", base_size = 10) +
theme(
legend.position = "top",
legend.justification = "left",
panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank()
)
# -- flextable --
ft_dat <- dataset[, c("matches", "win_pct", "loss_pct", "team")]
ft_dat$team <- as.character(ft_dat$team)
ft <- flextable(ft_dat)
ft <- border_remove(ft)
ft <- bold(ft, part = "header")
ft <- colformat_double(ft, j = c("win_pct", "loss_pct"),
digits = 1, suffix = "%"
)
ft <- set_header_labels(ft,
team = "Team", matches = "GP",
win_pct = "", loss_pct = ""
)
ft <- color(ft, color = "#28A87D", j = 2)
ft <- color(ft, color = "#EFAC00", j = 3)
ft <- bold(ft, bold = TRUE, j = 2:3)
ft <- italic(ft, italic = TRUE, j = 4)
ft <- align(ft, align = "right", part = "all")
ft <- autofit(ft)
# \dontshow{
cap <- ragg::agg_capture(width = 7, height = 6, units = "in", res = 150)
grDevices::dev.control("enable")
# }
print(
wrap_flextable(ft, flex_body = TRUE, just = "right") +
p + plot_layout(widths = c(1.1, 2))
)
# \dontshow{
raster <- cap()
dev.off()
plot(as.raster(raster))
init_flextable_defaults()
# }
Run the code above in your browser using DataLab