0

I wish to smoothly morph the colour of the 5 pointed star to the colour of the ten pointed star.

Currently, the color transition is abrupt, not smooth. I think I am using

geom_sf(aes(fill = ...)) 

incorrectly but am unsure how to proceed. Here is what I have so far.

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
ixodid
  • 2,180
  • 1
  • 19
  • 46

1 Answers1

1

I think you may have to make the colour a continuous value so you can use 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")
Marius
  • 58,213
  • 16
  • 107
  • 105