Intro Thoughts

Status Quo

library(tidyverse)
library(legendry)
## Warning: package 'legendry' was built under R version 4.4.1
gapminder::gapminder %>% 
  filter(continent == "Europe") %>% 
  filter(year %in% c(1957, 2002)) %>% 
  ggplot() + 
  facet_wrap(~year) + 
  aes(y = country, weight = pop) + 
  geom_bar()

gapminder::gapminder %>% 
  filter(continent == "Europe") %>% 
  filter(year %in% c(1957, 2002)) %>% 
  ggplot() + 
  facet_wrap(~year) + 
  aes(y = country, weight = pop) + 
  geom_bar() 

last_plot() +
  aes(y = tidytext::reorder_within(country, pop, year))

last_plot() + 
  tidytext::scale_y_reordered()

last_plot() + 
  scale_y_discrete()
## Scale for y is already present.
## Adding another scale for y, which will replace the existing scale.

last_plot() + 
  aes(y = fct_reorder(country, pop))

last_plot() + 
  aes(y = interaction(year, country, sep = "___") |> fct_infreq(w = 1/pop))

last_plot() + 
  tidytext::scale_y_reordered()
## Scale for y is already present.
## Adding another scale for y, which will replace the existing scale.

last_plot() + 
  aes(y = interaction(year, country, sep = "___") |> fct_infreq(w = 1/pop)) + 
  scale_y_discrete(labels = function(x) str_remove(x, "^.+___"))
## Scale for y is already present.
## Adding another scale for y, which will replace the existing scale.

last_plot() + 
  aes(y = interaction(year, country) |> fct_infreq(w = 1/pop)) + scale_y_discrete()
## Scale for y is already present.
## Adding another scale for y, which will replace the existing scale.

last_plot() + 
  scale_y_discrete(labels = function(x) str_remove(x, ".+\\."))
## Scale for y is already present.
## Adding another scale for y, which will replace the existing scale.

last_plot() + 
  facet_wrap(~ year, scales = "free_y")

gapminder::gapminder %>% 
  filter(continent == "Oceania") %>% 
  filter(year %in% c(1957, 2002)) %>% 
  ggplot() + 
  facet_wrap(~ year) + 
  aes(y = country, weight = pop) + 
  geom_bar() 

last_plot() + 
  aes(y = fct_infreq(interaction(year, country), w = pop)) 

last_plot() + 
  facet_wrap(~ year, scales = "free_y")

last_plot() + 
  scale_y_discrete(labels = function(x) str_remove(x, ".+\\."))

last_plot() + 
  aes(y = fct_infreq(interaction(year, country), w = 1/pop)) 

gapminder::gapminder %>% 
  filter(continent == "Americas") %>% 
  filter(year %in% c(1957, 2002)) %>% 
  ggplot() + 
  facet_wrap(~ year) + 
  aes(x = pop, y = interaction(year, country, sep = "___") |> fct_reorder(pop)) + 
  geom_col() 

last_plot() + 
  facet_wrap(~ year, scales = "free_y")

last_plot() +
  scale_y_discrete(labels = function(x) str_remove(x, ".+___")) + # don't show year part of factor
  labs(y = NULL)

read.csv("https://raw.githubusercontent.com/EvaMaeRey/mytidytuesday/refs/heads/main/2024-11-19-gg-prefixes/exported_funs_exts_ggplot2_tidyverse_org.csv") |>
  mutate(prefix_short = fun_exported |> str_extract(".+?_")) |>
  filter(prefix_short %in% c("geom_", "scale_", "stat_", "theme_", "layer_", "coord_", "position_")) |>
  group_by(user) |> 
  filter(n() >= 70) |>
  filter(!is.na(prefix_short)) |>
  ggplot() + 
  facet_wrap(~user, scales = "free_y") +
  aes(y = interaction(user, prefix_short) |> fct_infreq()) +
  geom_bar() + 
  scale_y_discrete(labels = function(x) str_remove(x, ".+\\.")) +
  coord_trans(y = "reverse", ylim = c(1,6)) + 
  labs(y = NULL) + 
  theme_gray(paper = ggplot2:::col_mix("lavender", "white", .9),
             ink = "grey10") + 
  theme(axis.ticks = element_blank())

compute_panel_rank <- function(data, scales, width = .7, n = 5){
  
  data |> 
    mutate(x = rank(y)) |> 
    arrange(x) |>
    slice(1:n) 
  
}


ggplot(palmerpenguins::penguins) + 
  aes(y = bill_length_mm) + 
  geom_bar(stat = statexpress::qstat(compute_panel_rank))

compute_panel_rank <- function(data, scales, width = .7, n = 5){
  
  data|>
    mutate(xmax = x,
           xmin = 0,
           ymin = rank(-x, ties.method = "random") - width/2,
           ymax = ymin + width,
           label = ranked) |>
    arrange(ymin) |>
    dplyr::select(-x) |>
    slice(1:n)
  
}




compute_panel_rank_label <- function(data, scales, width = .7, y_nudge = -.05, n = 5){
  
  data|>
    mutate(y = rank(-x, ties.method = "random") - width/2 + y_nudge,
           x = 0,
           label = ranked) |>
    arrange(y) |>
    slice(1:n)
  
}

compute_panel_rank_value_label <- function(data, scales, width = .7,  n = 5){
  
  data|>
    mutate(y = rank(-x, ties.method = "random"),
           x = x,
           label = x) |>
    arrange(y) |>
    slice(1:n) |>
    mutate(large = x > quantile(range(x), .5) ) |>
    mutate(hjust = large)
  
}


library(statexpress)

contrast <- function(colour) {
  out   <- rep("grey20", length(colour))
  light <- farver::get_channel(colour, "l", space = "hcl")
  out[light < 50] <- "grey80"
  out
}

# from https://github.com/teunbrand/ggplot_tricks
# aes_autocontrast <- aes(colour = after_scale(contrast(large)))

gapminder::gapminder |>
  filter(year > 2000) |>
  ggplot() + 
  aes(ranked = country, x = pop) + 
  geom_rect(stat = qstat_panel(compute_panel_rank)) + # geom_rbar
  qlayer(stat = qstat_panel(compute_panel_rank_label),
        geom = qproto_update(GeomText, aes(hjust = 0, vjust = 0,
                                      fill = "transparent",
                                      linewidth = 0,
                                      color = from_theme(ink)))) + #geom_rbar_text
  qlayer(geom = qproto_update(GeomLabel, 
                              aes(fill = "transparent",
                                  linewidth = 0)),
         stat = qstat_panel(compute_panel_rank_value_label)) +
  facet_wrap(~year) + 
  coord_trans(y = "reverse") 

last_plot() + 
  ggchalkboard::theme_chalkboard(base_size = 13) 

last_plot() + 
  ggchalkboard::theme_chalkboard(base_size = 13, base_theme = theme_gray) + 
  theme(axis.text = element_blank(),
        axis.ticks = element_blank()) + 
  labs(title = "Top 5 populations among countries, 2002 & 2007",
       x = NULL)

fct_rank <- function(x, by){
  
   rank(-by) |> interaction(x, lex.order = T)
  
}

fct_rank_within <- function(x, by, within){
  
  data.frame(x, by, within) |>
  mutate(rank = fct_rank(x, by), .by = within) |>
  mutate(ranked = interaction(within, rank)) |>
  pull(ranked)
  
}

gapminder::gapminder |>
  filter(continent == "Oceania") |>
  filter(year > 2000) |>
  select(country, pop, year) |>
  mutate(ranked = fct_rank(country, pop))
## # A tibble: 4 × 4
##   country          pop  year ranked       
##   <fct>          <int> <int> <fct>        
## 1 Australia   19546792  2002 2.Australia  
## 2 Australia   20434176  2007 1.Australia  
## 3 New Zealand  3908037  2002 4.New Zealand
## 4 New Zealand  4115771  2007 3.New Zealand
gapminder::gapminder |>
  filter(continent == "Oceania") |>
  filter(year > 2000) |>
  select(country, pop, year) |>
  mutate(ranked = fct_rank_within(country, pop, year))
## # A tibble: 4 × 4
##   country          pop  year ranked            
##   <fct>          <int> <int> <fct>             
## 1 Australia   19546792  2002 2002.1.Australia  
## 2 Australia   20434176  2007 2007.1.Australia  
## 3 New Zealand  3908037  2002 2002.2.New Zealand
## 4 New Zealand  4115771  2007 2007.2.New Zealand
# fct_rank_within <- function(x, by, within){
#   
#   for(i in 1:len)
#   rank(-by) |> interaction(x) |> fct_reorder(by) |> interaction(within)
#   
# }

gapminder::gapminder |>
  filter(continent == "Americas") |>
  filter(year > 2000) |>
  ggplot() + 
  aes(y = fct_rank(country, pop), 
      x = pop) +
  geom_col() +
  coord_trans(y = "reverse") # coord_page

last_plot() +
  facet_wrap(~year, scales = "free_y")

last_plot() + 
  scale_y_discrete(labels = function(x) str_remove(x, "\\d+\\.")) 

gapminder::gapminder |>
  filter(continent == "Americas") |>
  filter(year > 2000) |>
  ggplot() + 
  aes(y = fct_rank_within(country, pop, year), 
      x = pop) +
  geom_col() +
  coord_trans(y = "reverse") # coord_page

last_plot() +
  facet_wrap(~year, scales = "free_y")

last_plot() + 
  scale_y_discrete(labels = function(x) str_remove(x, ".+\\.")) 

Experiment

tidytext::scale_y_reordered
## function (..., labels = reorder_func, sep = deprecated()) 
## {
##     if (lifecycle::is_present(sep)) {
##         lifecycle::deprecate_warn("0.3.3", "scale_y_reordered(sep = )", 
##             "reorder_func(sep = )")
##     }
##     ggplot2::scale_y_discrete(labels = labels, ...)
## }
## <bytecode: 0x7fb74bbf9f88>
## <environment: namespace:tidytext>

Closing remarks, Other Relevant Work, Caveats