0

I am trying to use the gganimate package for the first time and want to use the transition_reveal function. However when my ggplot looks the same with or without the transition_reveal function. My code in below. TIA!

China<- ggplot(data = df.China.co2, aes(x=Year, 
                                        y=China_emissions, 
                                        group = 1))+
  geom_line(color = "red", size = 1.75) + 
  geom_point(color = "red", size = 2.5) +
  ggtitle("China CO2 Emissions, Yearly") + labs(x = "Year",
                                                y = "CO2 Emissions (million tonnes)") +
  theme(axis.title.y = element_text(size=10, color = "black")) +
  theme(axis.text = element_text(size = 10)) +
  theme(plot.title = element_text(size = 16, face = "bold", hjust=0, color = "black"))+
  **transition_reveal(Year)**

ggplotly(China)

this is my data:

structure(list(Year = c(1971, 1972, 1973, 1974, 1975, 1976, 1977, 
1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 
1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 
2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
), China_emissions = c(809.6, 861.9, 895.9, 915.4, 1062, 1105.5, 
1249.9, 1410.1, 1431.5, 1419.8, 1407.1, 1465.9, 1540.8, 1678.8, 
1726.9, 1830.8, 1969.3, 2118, 2199.1, 2244.1, 2360.7, 2468.6, 
2669.8, 2781.2, 3022.1, 3195.6, 3133.2, 3197.3, 3090.5, 3077.2, 
3124.2, 3347.8, 3869.8, 4592.8, 5103.1, 5644.7, 6071.8, 6549, 
6846.3, 7258.5)), class = "data.frame", row.names = c(NA, -40L
))
stefan
  • 90,330
  • 6
  • 25
  • 51
bea123456
  • 1
  • 1
  • 1
    Welcome to SO! To help us to help you could you please make your issue reproducible by sharing a sample of your **data**? See [how to make a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Simply type `dput(df.China.co2)` into the console and copy & paste the output starting with `structure(....` into your post. – stefan Dec 28 '20 at 15:13
  • ... additionally keep in mind that `ggplotly` is for converting a ggplot to a plotly object. It will not work with gganimate, i.e. you get the same interactive chart with or without using `transition_reveal`. – stefan Dec 28 '20 at 15:15
  • 1
    Thank you so much Stefan! I didn't realise that ```ggplotly``` and ```gganimate``` can't work together. Now I am no longer calling ```ggplotly``` the ```transition_reveal``` works. I was using ```ggplotly``` so that when you hover your mouse over the graph it tells you the values, is there a way to incorporate this with a moving plot? I suppose it may be difficult seeing as the line is constantly moving. – bea123456 Dec 28 '20 at 16:08
  • 1
    With gganimate a tooltip is not possible (not sure about plotly but as you said it probably doesn't make much sense). With gganimate you can add a moving label to your chart using e.g. `geom_text(aes(label = China_emissions))`. Or you can add a subtitle in labs using `subtitle = "Year: {round(frame_along)}, CO2 Emissions: {filter(df.China.co2, Year == round(frame_along)) %>% pull(China_emissions)}"` which will show the year and the emission level. – stefan Dec 28 '20 at 16:27

1 Answers1

0

@stefan comments fixed the issue if you don't need the plotly features.

Since you mentioned you would like to add plotly features to the animation, I am just adding this solution for completeness.

I don't think ggplotly works with gganimate anymore (I think it did in a previous version of gganimate -- see this link).

So if you want to create this type of animation in plotly, I think you need to do it directly in the plotly API. But in your example, for drawing the line with the path of the point through time, you need to change the data frame a little so each frame will have all past data up to the current point in time (check this plotly tutorial).

# create data frame with accumulated data
df.acum <- plyr::ldply(.data=df$Year, 
                       .fun=function(x){
                           df.new <- df %>% filter(Year <= x) %>% mutate(frame=x)})
df.acum <- df.acum %>% dplyr::arrange(frame, Year)

plot_ly() %>%
   add_trace(data=df.acum, x = ~Year, y = ~China_emissions, frame = ~frame, color=I("red"), text="China", type='scatter', mode = 'markers+lines', line = list(simplyfy = F), hoverinfo='text+x+y') %>% 
   animation_opts(frame = 100, transition = 0, redraw = TRUE)

enter image description here

kikoralston
  • 1,176
  • 5
  • 6