Status Quo
height_pattern <- c(-1,1, -.5, .5)
hjust_pattern <- c(0,0, 1,1)
library(tidyverse)
data.frame(x = runif(10)) %>%
mutate(event_descr = paste("event", LETTERS[1:10])) ->
timeline_df
timeline_df %>%
arrange(x) %>%
mutate(xend = x) %>%
mutate(yend = 0) %>%
mutate(y = rep(height_pattern, nrow(.))[1:nrow(.)]) %>%
mutate(hjust = rep(hjust_pattern, nrow(.))[1:nrow(.)]) %>%
ggplot() +
aes(x = x , y = y, xend = xend, yend = yend, label = event_descr ,
hjust = hjust) +
geom_point() +
geom_point(aes(y = 0)) +
geom_segment() +
geom_hline(yintercept = 0) +
geom_label()
compute_timeline <- function(data, scales, height_pattern = c(-1,1, -.5, .5), hjust_pattern = c(0,0, 1,1) ){
data |>
arrange(x) %>%
mutate(xend = x) %>%
mutate(yend = 0) %>%
mutate(y = rep(height_pattern, nrow(.))[1:nrow(.)]) %>%
mutate(hjust = rep(hjust_pattern, nrow(.))[1:nrow(.)])
}
timeline_df |>
compute_timeline()
## x event_descr xend yend y hjust
## 1 0.001319909 event F 0.001319909 0 -1.0 0
## 2 0.165754905 event C 0.165754905 0 1.0 0
## 3 0.219511486 event A 0.219511486 0 -0.5 1
## 4 0.276765377 event B 0.276765377 0 0.5 1
## 5 0.353398225 event J 0.353398225 0 -1.0 0
## 6 0.379800423 event D 0.379800423 0 1.0 0
## 7 0.397065089 event E 0.397065089 0 -0.5 1
## 8 0.496512251 event I 0.496512251 0 0.5 1
## 9 0.764547850 event G 0.764547850 0 -1.0 0
## 10 0.782640573 event H 0.782640573 0 1.0 0
StatTimeline <- ggproto("StatTimeline", Stat, compute_panel = compute_timeline)
timeline_df |>
ggplot() +
aes(x = x, label = event_descr) +
geom_hline(yintercept = 0) +
layer(GeomSegment, StatTimeline, position = "identity") +
layer(GeomPoint, StatTimeline, position = "identity") +
layer(GeomLabel, StatTimeline, position = "identity") +
layer(GeomPoint, StatTimeline, position = "identity",
params = list(y = 0))
geom_timeline_basic <- function(){
list(
geom_hline(yintercept = 0),
layer(GeomSegment, StatTimeline, position = "identity"),
layer(GeomPoint, StatTimeline, position = "identity"),
layer(GeomLabel, StatTimeline, position = "identity"),
layer(GeomPoint, StatTimeline, position = "identity",
params = list(y = 0))
)
}
timeline_df |>
ggplot() +
aes(x = x, label = event_descr) +
geom_timeline_basic()
data.frame(time = hms::as_hms(c("08:30:00",
"09:20:00",
"10:00:00")),
event = c("Arrival",
"Drive to Bus Station",
"Bus Arrives")) %>%
ggplot() +
aes(x = time,
label = paste(time, event, sep = "\n")) +
geom_timeline_basic() +
labs(title = "Thurs 27")