suppressPackageStartupMessages({
library(tidyverse)
library(cowplot)
library(ggrepel)
library(directlabels)
library(geomtextpath)
theme_set(theme_cowplot())
})
options(repr.plot.width=16,repr.plot.height=10)
Labeling Data Series#
data#
Labels at the end#
see: https://ggrepel.slowkow.com/articles/examples
mutate(data,
label=ifelse(date==max(date, na.rm=T), city, NA),
.by=city
) |>
ggplot(aes(x=date, y=median, color=city)) +
geom_line() +
geom_label_repel(aes(label=label), size=5, min.segment.length = 0, max.overlaps = Inf, box.padding = 1, segment.color='darkgray', nudge_x=0.3, hjust = "left",direction='y') +
guides(color='none') +
scale_y_sqrt()
Labels at the peak (y-axis)#
mutate(data,
label=ifelse(median==max(median, na.rm=T), city, NA),
.by=city
) |>
ggplot(aes(x=date, y=median, color=city)) +
geom_line() +
geom_label_repel(aes(label=label), size=5, min.segment.length = 0, max.overlaps = Inf, box.padding = 1, segment.color='darkgray', nudge_x=0.1, hjust = "left",direction='y') +
guides(color='none') +
scale_y_sqrt()
geomtextpath#
see: https://cran.r-project.org/web/packages/geomtextpath/vignettes/geomtextpath.html
directlabels#
see: https://cran.r-project.org/web/packages/directlabels/vignettes/examples.html
data |>
ggplot(aes(x=date, y=median, color=city, label=city)) +
geom_line() +
geom_dl(method = list('right.polygons', cex=1)) +
guides(color='none') +
scale_y_sqrt()