I am using gganimate with colorspace to demonstrate partial pooling in Bayesian hierarchical models, and I like the aesthetic transition I have accomplished, but I would prefer for the fill legend for "Prior" to appear as two discrete boxes (as it would with scale_fill_discrete) but I seem to need to pretend my distribution field is continuous in order to trigger the nice transition that scale_fill_continuous_sequential() is enabling.
library(tidyverse)
library(colorspace)
library(gganimate)
library(ggplot2)
set.seed(42)
SIMS <- 1e3
df <-
tibble(
a = rbeta(SIMS, 600, 400),
b = rbeta(SIMS, 650, 350)
) |>
pivot_longer(
everything(),
names_to = 'distribution',
values_to = 'values'
) |>
mutate(distribution = factor(distribution, levels = c('a','b')))
df |>
ggplot(aes(
x = values,
fill = as.integer(distribution)
)) +
geom_density() +
colorspace::scale_fill_continuous_sequential(
palette = "Blues 3",
begin = 0.1, end = .9,
breaks = c(1,2)
) +
transition_states(
states = distribution,
transition_length = 1,
state_length = 1,
wrap = TRUE) +
labs(fill = "Prior")
