补间/变形的形状的非平滑颜色过渡

时间:2019-02-19 01:59:02

标签: r gganimate

我希望将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

1 个答案:

答案 0 :(得分:1)

我认为您可能必须将颜色设为连续值,以便可以使用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")