tweenr/transformr 形状的非平滑颜色过渡

Non-smooth colour transition of shape with tweenr/transformr

我希望将 5 角星的颜色平滑地变形为 10 角星的颜色。

目前,颜色过渡突然,不平滑。我想我正在使用

geom_sf(aes(fill = ...)) 

不正确,但不确定如何进行。这是我目前所拥有的。

library(transformr)
library(gganimate)
library(tidyverse)


star5_st <- sf::st_sfc(poly_star(n = 5, st = TRUE))
star10_st <- sf::st_sfc(poly_star(n = 10, st = TRUE))

# Move shape 2 over to the right a bit
star10_st <- star10_st + c(2.0, 0)

# Start state df  
df1 <- data.frame(
  id = c(1),
  geo = c(star5_st),
  fill_col = c("black")
)

# End state df  
df2 <- data.frame(
  id = c(1),
  geo = c(star10_st),
  fill_col = c("green")
)

morph <- tween_sf(df1, df2,
                  ease = 'linear', 
                  nframes = 12, 
                  id = id)

# Create frames
aa <- ggplot(morph) +
  geom_sf(aes(geometry = geometry, fill = fill_col)) +
  transition_time(time = .frame) +
  theme_void() +
  theme(legend.position = "none") +
  coord_sf(datum = NA) +
  scale_colour_identity() 

show <- animate(aa, nframes = 40, fps = 20, 
                renderer = gifski_renderer(loop = FALSE), start_pause = 5)


show

我想你可能必须让颜色成为一个连续的值,这样你就可以使用 scale_fill_gradient:

df1 <- data.frame(
    id = c(1),
    geo = c(star5_st),
    fill_col = 0
)

# End state df
df2 <- data.frame(
    id = c(1),
    geo = c(star10_st),
    fill_col = 1
)

morph <- tween_sf(df1, df2,
                  ease = 'linear',
                  nframes = 12,
                  id = id)

# Create frames
aa <- ggplot(morph) +
    geom_sf(aes(geometry = geometry, fill = fill_col)) +
    transition_time(time = .frame) +
    theme_void() +
    theme(legend.position = "none") +
    coord_sf(datum = NA) +
    scale_fill_gradient(low = "black", high = "green")