Intro Thoughts

Status Quo

library(tidyverse)

Experiment

library(tidyverse)
library(legendry)
## Warning: package 'legendry' was built under R version 4.4.1
data.frame(
  category = c("Meat", "Meat", "Vegetables", "Vegetables", "Dairy"),
  food     = c("Beef", "Chicken", "Carrots", "Onions", "Cheese"),
  gram     = c(85,  85, 150, 210, 225),
  calories = c(245, 185, 45, 80, 240)) %>%
  mutate(food_category = interaction(food, category)) |>
  mutate(category_food = paste(category, food)) |>
  ggplot() +
  theme_classic() +
  aes(x = food_category,
      y = calories) +
  geom_col() +
  aes(fill = category_food) +
  guides(x = "axis_nested") +
  guides(fill = "legend_group") +
  labs(x = NULL, fill = NULL) +
  guides(y = guide_axis_nested(
    key_range_manual(0, 100, "low calories"))
    ) +
  theme(axis.text.y.left =
          element_text(angle = 90, hjust = 0.5))

# In addition, we might want to annotate the ruling president at the time
head(presidential)
## # A tibble: 6 × 4
##   name       start      end        party     
##   <chr>      <date>     <date>     <chr>     
## 1 Eisenhower 1953-01-20 1961-01-20 Republican
## 2 Kennedy    1961-01-20 1963-11-22 Democratic
## 3 Johnson    1963-11-22 1969-01-20 Democratic
## 4 Nixon      1969-01-20 1974-08-09 Republican
## 5 Ford       1974-08-09 1977-01-20 Republican
## 6 Carter     1977-01-20 1981-01-20 Democratic
ggplot(economics) +
  aes(date, unemploy) + 
  geom_line() +
  guides(x = primitive_bracket(key = 
                                 key_range_map(presidential, 
                                               start = start, 
                                               end = end, 
                                               name = name),
                               bracket = "square")
         ) ->
  p1

air_dates <- data.frame(
  year = as.Date(c("1974-01-01", "1985-01-01", "1994-01-01", "2008-01-01")),
  title = c("Little House\non the Prairie", "The Golden\nGirls", 
            "Friends", "Breaking\nBad")
)

head(air_dates)
##         year                        title
## 1 1974-01-01 Little House\non the Prairie
## 2 1985-01-01            The Golden\nGirls
## 3 1994-01-01                      Friends
## 4 2008-01-01                Breaking\nBad
# Let's say we want to display when the shows started with arrows
tv_show_guide <- guide_axis_base(
  key = key_map(air_dates, aesthetic = year, label = title),
  # Local theme for long arrows as ticks
  theme = theme(
    axis.ticks.length = unit(14, "pt"),
    axis.ticks = element_line(arrow = arrow(length = unit(2, "mm")))
  )
)

# We'd like to place these arrows ontop of a regular guide
ontop <- compose_ontop("axis_base", tv_show_guide)

# In addition, we might want to annotate the ruling president at the time
president_guide <- primitive_bracket(
  key = key_range_map(presidential, start = start, end = end, name = name),
  bracket = "square"
)

ggplot(economics, aes(date, unemploy)) +
  geom_line() +
  guides(x = compose_stack(ontop, president_guide))

Closing remarks, Other Relevant Work, Caveats