r - 使用 gganimate 为 geom_bar 设置动画的问题
问题描述
我遇到了 gganimate 的问题,我需要一些帮助。
我试图显示一个条形图,然后在几年内对其进行动画处理,但我没有输出,也没有显示错误。有错误还是我错过了什么?
我正在使用有关欧盟工作事故的数据。我的 R 版本是:3.6.1 (2019-07-05)
希望下面的代码演示了这个问题
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}")
解决方案
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)
由reprex 包(v0.3.0)于 2019 年 12 月 2 日创建
推荐阅读
- javascript - 如何在不知道 NextJS 上的静态路径的情况下生成动态页面?
- linux - 如何存储 find 生成的文件,即实际文件和内容作为文件而不是 find 的控制台输出?
- lua - 如何使用 IUP THEME 属性/DEFAULTTHEME 全局属性
- php - 在 Laravel 中以同名保存会话安全吗?
- reactjs - 如何在不刷新页面的情况下在 ReactJS 中重新加载?
- c++14 - 为什么我在二叉树的对角遍历代码中出现 SIGBART 错误?
- winapi - ShowWindow 调用可防止通过 SetLayeredWindowAttribute 更改 alpha
- bash - 如何找出一个数字与文件中下一个相同数字的出现之间有多少行?
- angular - 如果条件失败,Angular 11
- reactjs - 反应原生 setInterval useState