B.Sarah
B.Sarah

Reputation: 139

Issue with animating geom_bar using gganimate

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}")

Upvotes: 2

Views: 487

Answers (1)

M--
M--

Reputation: 29109

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)

Upvotes: 3

Related Questions