prdel99
prdel99

Reputation: 363

R ggplotly animated geom_tile

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~.) 
  )

Upvotes: 0

Views: 143

Answers (1)

Kat
Kat

Reputation: 18714

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

Upvotes: 1

Related Questions