I would like to include labels with % growth over the columns of a chart, as in the picture below:
A chart with arrows over the columns, connecting them by pairs, showing the % growth as label.
So far I've only been able to connect them with straight or curved lines, not these more clean looking "boxes".
There might be an add-in that provides a default for these as a simple function, but it's not too complicated to make our own.
Here, I start with some fake data, then add some helper columns. We can feed this into ggplot2 layers that provide the columns, the arrows (here prettier using ggarrow
), and the label boxes. I use dplyr::reframe
here to specify four coordinates for each original observation, corresponding to the the four points of each arrow.
library(ggplot2); library(dplyr)
# fake data
data.frame(x = 1:3, y = 4:6) |>
# helper columns
mutate(next_x = lead(x),
next_y = lead(y),
chng_y = scales::percent(next_y / y - 1)) |>
ggplot(aes(x, y)) +
geom_col() +
ggarrow::geom_arrow(aes(group = row),
linewidth = 0.6,
# for each row before the last, specify four coordinates
data = ~mutate(., row = row_number()) |>
filter(!is.na(next_y)) |>
reframe(x = rep(c(x + 0.05, next_x - 0.05), each = 2),
y = c(y * 1.05,
pmax(y, next_y) * 1.15,
pmax(y, next_y) * 1.15,
next_y * 1.05), .by = row)) +
geom_label(aes(x = (x + next_x)/2,
y = pmax(y, next_y) * 1.15,
label = chng_y))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With