Home > Software design >  R ggplotly animated geom_tile
R ggplotly animated geom_tile

Time:11-07

I have this dataframe

dt <-
data.frame(
        date = as.Date(c("2022-01-01", "2022-01-02", "2022-01-01", "2022-01-02", "2022-01-01", "2022-01-02", "2022-01-01", "2022-01-02")),
        seq = c(1, 1, 2, 2, 1, 1, 2, 2),
        days = c(1, 1, 1, 1, 2, 2, 2, 2),
        val = seq(1, 8))

I'd like to create animated geom_tile and geom_text plots in ggplotly where animation frames will be days. I created this code:

  ggplotly(
    ggplot(
      dt,
      aes(x = seq, y = date, fill = days))  
      geom_tile(frame = days)  
      geom_text(aes(label = val), frame = days)  
      facet_grid(days~.) 
  )

But I'm getting following warning and there is no animation at all.

Warning messages: 
1: Ignoring unknown parameters: frame  
2: Ignoring > unknown parameters: frame

In other words, expected result for first frame is

  ggplotly(
    ggplot(
      dt %>% 
      filter(days == 1),
      aes(x = seq, y = date, fill = days))  
      geom_tile(frame = days)  
      geom_text(aes(label = val), frame = days)  
      facet_grid(days~.) 
  ) 

and second frame should be

  ggplotly(
    ggplot(
      dt %>% 
      filter(days == 2),
      aes(x = seq, y = date, fill = days))  
      geom_tile(frame = days)  
      geom_text(aes(label = val), frame = days)  
      facet_grid(days~.) 
  )

CodePudding user response:

I'm not sure what your expected output is. If you want to set frame, do this in ggplot(aes()). However, you're not going to be able to facet on the same variable as you use for frame.

ggplotly(
  ggplot(dt, aes(x = seq, y = date, fill = days, frame = days))  
    geom_tile()  
    geom_text(aes(label = val)) 
)

enter image description here enter image description here

  • Related