set.seed(202501)
theme_piccia <- function() {
theme_minimal(base_size = 12) +
theme(
text = element_text(family = "Noah", colour = "#12051C"),
plot.title = ggtext::element_textbox_simple(
face = "bold",
size = rel(1.5),
margin = margin(12, 0, 12, 0, "pt")
),
axis.text = element_text(family = "Noah", colour = "#372D40"),
panel.grid = element_line(colour = "#FFFFFF"),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
axis.title = element_blank(),
plot.background = element_rect(colour = "#F7F4F6", fill = "#F7F4F6"),
# Give everything a bit more space to breathe
plot.margin = margin(rep(12, 4))
)
}
piccia_palette <- c(
purple = "#5c1c8d",
lilac = "#be77bf",
dark_pink = "#cd0d72",
light_pink = "#f76bcc",
orange = "#b4612b",
emerald = "#3ca465",
forest_green = "#1c4d3b",
blue_grey = "#55517e"
)
many_trees <- Orange |>
dplyr::mutate(Tree = as.character(Tree)) |>
rbind(dplyr::tibble(
Tree = c(rep("6", 7), rep("7", 7), rep("8", 7)),
age = rep(unique(Orange$age), 3),
circumference = c(
sort(sample(25:250, 7)),
sort(sample(25:250, 7)),
sort(sample(25:250, 7))
)
))
interactive_lines <- many_trees |>
dplyr::group_by(Tree) |>
dplyr::mutate(
total_growth = max(circumference) - min(circumference),
orchard = dplyr::case_when(
Tree %in% c("8", "7", "6", "4") ~ "Greenleaf Citrus Farm",
TRUE ~ "Purple Horizon Orchards"
),
plantation_year = dplyr::case_when(
Tree %in% c("6", "3") ~ 2021,
Tree %in% c("4", "2") ~ 2022,
Tree %in% c("7", "5") ~ 2023,
TRUE ~ 2024
),
tooltip_text = paste0(
"<b>Tree #",
Tree,
"</b> planted in ",
plantation_year,
"<br>in ",
orchard,
"<br><br><b>Age</b> ",
format(age, big.mark = ","),
" days | <b>Circumference</b> ",
circumference,
"mm"
)
) |>
ggplot(aes(x = age, y = circumference, colour = orchard, group = Tree)) +
geom_line(linewidth = 2.5, alpha = 0.5, colour = "grey") +
geom_line(aes(alpha = plantation_year), linewidth = 2.5) +
ggiraph::geom_point_interactive(
aes(tooltip = tooltip_text, data_id = Tree, alpha = plantation_year),
shape = 21,
size = 2.5,
fill = "#FFFFFF",
stroke = 2
) +
scale_alpha_continuous(range = c(0.05, 1), transform = "identity") +
labs(
title = "Charting the increase in tree circumference by age, by orchard and by year"
) +
scale_x_continuous(labels = function(x) paste(x, "days")) +
scale_y_continuous(labels = function(x) paste(x, "mm"), limits = c(0, 250)) +
scale_colour_manual(
values = c(
"Greenleaf Citrus Farm" = piccia_palette[["forest_green"]],
"Purple Horizon Orchards" = piccia_palette[["purple"]]
)
) +
ggtext::geom_textbox(
data = head(many_trees, 1),
aes(
x = 1000,
y = 225,
label = paste0(
"In <span style='color: ",
piccia_palette[["forest_green"]],
"'>**Greenleaf Citrus Farm**</span> the most recently planted tree (the darkest line) saw the most consitent growth..."
)
),
family = "Noah",
size = 4.5,
box.colour = NA,
fill = NA,
colour = "#372D40",
width = unit(14, "lines")
) +
ggtext::geom_textbox(
data = head(many_trees, 1),
aes(
x = 1400,
y = 85,
label = paste0(
"... while in <span style='color: ",
piccia_palette[["purple"]],
"'>**Purple Horizon Orchard**</span> we were back to square one."
)
),
family = "Noah",
size = 4.5,
box.colour = NA,
fill = NA,
colour = "#372D40"
) +
theme_piccia() +
theme(legend.position = "none", legend.title = element_blank())
ggiraph::girafe(
ggobj = interactive_lines,
options = list(
ggiraph::opts_tooltip(
opacity = 0.92,
css = "background-color:#372D40;font-size:0.9em;color:#f9f9f7;padding:0.9em;letter-spacing:0.03em;border-radius:10px;font-family:Noah;max-width:350px;"
),
ggiraph::opts_hover(css = "r:4pt;")
)
)