使用补间/变压器的形状不平滑的颜色过渡

问题描述 投票:0回答:1

我希望将5角星的颜色平滑地变成十角星的颜色。

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

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
r gganimate
1个回答
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")
© www.soinside.com 2019 - 2024. All rights reserved.