Skip to content

Instantly share code, notes, and snippets.

@klmr
Created February 23, 2017 12:20
Show Gist options
  • Save klmr/dec27b5e0fc525fbe9ebb3f182b24d91 to your computer and use it in GitHub Desktop.
Save klmr/dec27b5e0fc525fbe9ebb3f182b24d91 to your computer and use it in GitHub Desktop.
Ordering pillows online
library(dplyr)
library(tidyr)
library(ggplot2)
stages = c('Ordered', '?', 'In transit', 'Arrived', 'Returned')
make_stages = function (status)
factor(status, levels = stages, ordered = TRUE)
pillows = tibble::tribble(
~Provenance, ~LastStatus,
'Amazon', 'Arrived',
'Amazon', 'In transit',
'Panda', '?',
'Panda', '?'
) %>%
mutate(Which = row_number()) %>%
mutate(LastStatus = make_stages(LastStatus)) %>%
group_by(Which) %>%
mutate(Stage = list(make_stages(stages[seq_len(as.integer(LastStatus))]))) %>%
mutate(Timeline = list(c(seq_len(as.integer(LastStatus) - 1), length(stages)))) %>%
unnest(Stage, Timeline) %>%
complete(Timeline = seq_along(stages), nesting(Provenance, LastStatus)) %>%
mutate(Stage = zoo::na.locf(Stage)) %>%
group_by(Timeline, Stage) %>%
mutate(NumberJoint = n()) %>%
ungroup()
theme_set(theme_minimal() +
theme(panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank()))
ggplot(pillows) +
aes(Timeline, Stage) +
geom_path(aes(size = lead(NumberJoint), group = Which, color = Provenance), lineend = 'round') +
scale_y_discrete(drop = FALSE) +
ylab('') +
scale_color_manual(values = c(Amazon = '#FF7F00', Panda = '#8BAE00'),
guide = guide_legend(title = '')) +
scale_size_continuous(guide = FALSE) +
theme(axis.text.x = element_blank())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment