2

I am having an issue with gganimate and I would like some help, please.

I am trying to display a bar plot and then animate it over some years but I have no output and no error displayed. Is there a bug or am I missing something?

I am using data about accidents at work in the EU. My R version is: 3.6.1 (2019-07-05)

Hopefully, the following code demonstrates the problem

library(eurostat)
library(rvest)
library(tidyverse)
library(plotly)
library(gganimate)

# get Eurostat data: accidents at work
d_list <- get_eurostat('hsw_ph3_02')

## cast the list into an R data frame
df <- as.data.frame(d_list)

# get data 
data_UE27_time <- subset(df, df$wrkenv == "TOTAL" & df$geo =="EU27" & 
                             df$severity =="FAT" & df$sex == "M" & df$age == "TOTAL" &
                             df$unit == "NR" & df$nace_r2 != "TOTAL" & df$nace_r2 != "A_C-N", 
                         select=c(nace_r2,values,time))

#animated bar plot
gp <- ggplot(data = data_UE27_time, aes(x = nace_r2, y = values))+
  geom_bar(stat="identity") +
   transition_time(time) +
  labs(title = "Year: {frame_time}")
M--
  • 25,431
  • 8
  • 61
  • 93
B.Sarah
  • 119
  • 1
  • 3
  • 11

1 Answers1

3
library(dplyr)
library(magick)
library(png)
library(gganimate)

gp <- data_UE27_time %>% 
        as_tibble() %>% 
        ggplot(aes(x = nace_r2, y = values, group = time)) +
          geom_bar(stat="identity") +
          transition_time(time) +
          labs(title = "Year: {frame_time}")

animate(gp, nframes = 10)

Created on 2019-12-02 by the reprex package (v0.3.0)

M--
  • 25,431
  • 8
  • 61
  • 93
  • 3
    **To downvoter**, If you can remotely explain your reason, I will pick one of your top answers and will award 500 reps bounty to it. – M-- Dec 02 '19 at 18:29
  • Thank you very much @M-- :-) – B.Sarah Dec 02 '19 at 20:34
  • @B.Sarah You're welcome. I have to confess that I couldn't get plotly to animate barplot though. It could be a follow up question. Cheers. – M-- Dec 02 '19 at 20:38