Hi,
I'm not sure if this is more of a gganimate
question or a ggplot2
question -- so I tagged them both :
Anyway, in scales_color_gradient2()
, which (by default) sets red to the low points, blue to the high points, and white to the midpoints, the midpoint =
argument is defaulted to 0.
In all examples I found only, when people wanted to change the midpoint to the median of the data, they did something where they'd set the midpoint outside of the ggplot call, and use it as a variable.
Is there a way to do this automatically?
My motivation is in gganimate. Over the different frames, the midpoint changes. In the example below, we can see that by the end, all points are blue, since all data is above the global median by then. But I'd love to set it, such that within each frame, the midpoint is set to the median.
Is this possible?
suppressPackageStartupMessages(library(tidyverse))
library(gganimate)
set.seed(22)
df1 <- tibble(id0 = 1:1000,
x0 = runif(1000, 10, 20),
y0 = runif(1000, 10, 20)) %>%
crossing(year0 = 2000:2019) %>%
mutate(z0 = 1.3 * x0 + 0.7 * y0 + rnorm(nrow(.), 0, 5),
z1 = 1.3 * x0 + 0.7 * y0 + 0.8 * (year0 - 2000) + rnorm(nrow(.), 0, 5))
mid0 <- median(df1$z0)
gg0 <- df1 %>%
ggplot(aes(x0, y0, color = z0)) +
geom_jitter(width = 0.5, height = 0.5) +
theme_light() +
scale_color_gradient2(midpoint = mid0)
gg0
mid1 <- median(df1$z1)
gg1 <- df1 %>%
ggplot(aes(x0, y0, color = z1)) +
geom_point(size = 4.5) +
theme_light() +
scale_color_gradient2(midpoint = mid0) +
transition_time(year0) +
labs(title = "Year: {frame_time}")
animate(gg1, nframes = 20, fps = 1)
Created on 2019-08-27 by the reprex package (v0.3.0)