R gganimate transition_time() function

468 views Asked by At

I have the following dataframe where there are two distinct entries in 'stock' column:

glimpse(df_final_time_series)

Rows: 396
Columns: 3
$ date  <date> 2004-01-01, 2004-02-01, 2004-03-01, 2004-04-01, 200...
$ stock <chr> "vtv", "vtv", "vtv", "vtv", "vtv", "vtv", "vtv", "vt...
$ price <dbl> 31.70592, 32.52995, 31.80250, 31.43529, 31.66834, 32...

I have created a time series graph with the following codes:

    df_final_time_series %>% 
  ggplot(aes(x=stock, y=price)) +
  geom_line(aes(color = stock), size = 2) +
  geom_segment(aes(xend = stock, yend = price)) +
  geom_label(aes(label = price))+
  labs(title = "",
       subtitle = "Time-Series Performances of VTV and VUG",
       caption = "Source: Yahoo Finance") +
  gganimate::transition_reveal(as.Date(date)) + 
  ggthemes::theme_fivethirtyeight()

This code is working fine. I am now trying to replicate the graph with Year: YYYY in title changing as the data point changes.

I have tried to implement transition_time(year) and several other variations however encountering error There were 50 or more warnings (use warnings() to see the first 50) Warning messages:

1: Cannot get dimensions of plot table. Plot region might not be fixed 2: object 'year' not found 3: object 'year' not found ... 50: object 'year' not found

    df_final_time_series %>% 
  ggplot(aes(x=stock, y=price)) +
  geom_line(aes(color = stock), size = 2) +
  geom_segment(aes(xend = stock, yend = price)) +
  geom_label(aes(label = price))+
  transition_time(year)+
  labs(title = "Year: {year}",
       subtitle = "Time-Series Performances of VTV and VUG",
       caption = "Source: Yahoo Finance") +
  gganimate::transition_reveal(as.Date(date)) + 
  ggthemes::theme_fivethirtyeight()
1

There are 1 answers

1
Emanuel V On

Instead of {year}, have you tried {frame_time}? That is what the gganimate help pages show. See gganimate.com.