标签: gganimate

可以在 R 中使用 gganimate 为多边形填充设置动画吗?

我有县级数据记录了 2002 年至 2018 年在该县首次检测到入侵性害虫的年份。我使用 ggplot2 和地图包创建了一张地图,该地图包根据检测到害虫的年份用颜色填充县多边形。

**有没有办法使用 gganimate 包来制作这张地图的动画,第一帧只填充检测日期为 2002 年的多边形,第二帧填充检测日期为 2003 年或更早(所以 2002 和 2003)的多边形,检测日期为 2004 年或更早(2002、2003、2004)等的第三个框架?** 澄清:我希望所有县多边形始终可见并最初填充为白色,动画的每一帧都根据检测年份添加县填充。

我试过使用 transition_reveal(data$detect_year)与静态图一起但得到一个错误,即“沿数据必须是整数、数字、POSIXct、日期、difftime 或 orhms”。

这是可重现示例的一些代码:

library(dplyr)
library(purrr)
library(maps)
library(ggplot2)
library(gganimate)
# Reproducible example
set.seed(42)
map_df <- map_data("county") %>% 
   filter(region == "minnesota")
map_df$detection_year <- NA
# Add random detection year to each county
years <- 2002:2006
map_list <- split(map_df, f = map_df$subregion)
map_list <- map(map_list, function(.x) {
   .x$detection_years <- mutate(.x, detection_years = sample(years, 1))
})
# collapse list back to …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

3
推荐指数
1
解决办法
1498
查看次数

如何更快地渲染 ggplot2+gganimate+ggflags

我正在使用 ggplot2+gganimate+ggflags 制作绘图,但渲染速度非常慢:通常每秒 0.1 帧或低于 0.1 帧。因此,如果我有 300 帧,则需要很长时间。我注意到是 ggflags 导致它变得比其他方式慢得多。我可以做些什么来加快渲染速度?我正在使用 MacBook Pro。

我非常基本的代码设置如下所示:

plot <- data %>% ggplot2(aes(...)) + geom_flag(...) + lots of options + transition_time(year) 

anim_save("file.gif", plot, ...)
Run Code Online (Sandbox Code Playgroud)

r render ggplot2 gganimate

3
推荐指数
1
解决办法
1140
查看次数

将 gganimate 动画另存为 mp4 文件

有没有办法将制作的电影保存ffmmeg_rendererav_renderer单独的文件?我总是将主题作为数据嵌入到 html 文件中。

gganimate

2
推荐指数
1
解决办法
6114
查看次数

如何使用 gganimate 创建带有风动画的轮廓?

我为特定一天的每个小时创建了风动画(单击播放动画视频)。我不想显示 19 个点,而是想创建一个在整个区域每小时使用这 19 个点进行插值/外推的等高线图 - 就像使用 ArcGIS 和样条插值工具生成的等高线图一样。

PM轮廓

以下代码显示了我用来创建每小时风动画的 ggplot 和 gganimate。我只创建了一个小数据框作为完整 24 小时数据的子样本,因为我不熟悉将 csv 附加到此论坛中。有没有办法包含覆盖该区域的轮廓而不是geom_point

library(ggplot2)
library(ggmap)
library(gganimate)

site <- c(1:18, 1:18)    
date <- data.frame(date=c(rep(as.POSIXct("2018-06-07 00:00:00"),18),rep(as.POSIXct("2018-06-07 01:00:00"),18)))    
long <- c(171.2496,171.1985, 171.2076, 171.2236,171.2165,171.2473,171.2448,171.2416,171.2243,171.2282,171.2344,171.2153,171.2532,171.2444,171.2443,171.2330,171.2356,171.2243)   
lati <- c(-44.40450,-44.38520,-44.38530,-44.38750,-44.39195,-44.41436,-44.38798,-44.38934,-44.37958,-44.37836,-44.37336,-44.37909,-44.40801, -44.40472,-44.39558,-44.40971,-44.39577,-44.39780)    
PM <- c(57,33,25,48,34,31,52,48,31,51,44,21,61,53,49,34,60,18,41,26,28,26,26,18,32,28,27,29,22,16,34,42,37,28,33,9)    
ws <- c(0.8, 0.1, 0.4, 0.4, 0.2, 0.1, 0.4, 0.2, 0.3, 0.3, 0.2, 0.7, NaN, 0.4, 0.3, 0.4, 0.3, 0.3, 0.8, 0.2, 0.4, 0.4, 0.1, 0.5, 0.5, 0.2, 0.3, 0.3, 0.3, 0.4, NaN, 0.5, 0.5, …
Run Code Online (Sandbox Code Playgroud)

r contour ggplot2 gganimate

2
推荐指数
1
解决办法
1155
查看次数

ggraph中相同网络的不同布局之间的平滑过渡

我想使用不同的布局为同一个网络设置动画,并使布局之间平滑过渡。我想在gganimate框架内执行此操作。

library(igraph)
library(ggraph)
library(gganimate)

set.seed(1)
g <- erdos.renyi.game(10, .5, "gnp")
V(g)$name <- letters[1:vcount(g)]
l1 <- create_layout(g, "kk")
l2 <- create_layout(g, "circle")
l3 <- create_layout(g, "nicely")
long <- rbind(l1,l2,l3)
long$frame <- rep(1:3, each =10)
Run Code Online (Sandbox Code Playgroud)

按照这种ggplot方法,我以长格式(long)存储节点位置,frame并向每个布局添加一个变量。我试图使其与下面的代码一起使用,它可以正常工作并且几乎是我想要的。但是,我似乎找不到找到包括这些边缘的方法:

ggplot(long, aes(x, y, label = name, color = as.factor(name), frame = frame)) +
  geom_point(size = 3, show.legend = F) +
  geom_text(show.legend = F) +
  transition_components(frame)
Run Code Online (Sandbox Code Playgroud)

我还尝试添加边缘为,geom_segment但最终在节点保持移动的同时使它们变为静态。这就是为什么我使用该ggraph程序包并失败的原因:

ggraph(g, layout = "manual", node.position = long) + …
Run Code Online (Sandbox Code Playgroud)

r igraph ggplot2 gganimate ggraph

2
推荐指数
1
解决办法
251
查看次数

帧之间的gganimate变化轴

我正在尝试使用 gganimate 绘制 NHL 前三名得分手随着时间的推移。目前,我有一个柱状图,其中 x 轴显示球员姓名,y 轴显示每个球员的进球数。这是我所拥有的静态版本:

library(ggplot2)

data <- data.frame(name=c("John","Paul","George","Ringo","Pete","John","Paul","George","Ringo","Pete"),
     year = c("1997", "1997", "1997", "1997", "1997", "1998", "1998","1998","1998", "1998"),
     goals = c(50L, 35L, 29L, 5L, 3L, 3L, 5L, 29L, 36L, 51L))

data <- data %>%
  arrange(goals) %>%
  group_by(year) %>%
  top_n(3, goals)

ggplot(data, 
  aes(x = reorder(name, goals), y=goals)) +
  geom_col() +
  facet_wrap(data$year) +
  coord_flip()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我想要的是只显示前 3 名球员。换句话说,一年进入前三名但第二年退出前三名的球员不应该出现在第二帧中。最终产品应如下所示:

https://www.youtube.com/watch?v=nYjpZcPhoqU

r ggplot2 gganimate

2
推荐指数
1
解决办法
434
查看次数

使用 gganimate() 沿日期对点和回归线进行动画处理

我正在尝试对点和黄土回归线进行动画处理,以便它们在一年中同时出现/显示,但我返回了一个错误,如下所述,带有 reprex。

这将是理想的动画:https://user-images.githubusercontent.com/1775316/49728400-f0ba1b80-fc72-11e8-86c5-71ed84b247db.gif 不幸的是,我发现这个的线程没有附带的代码。

在这里查看我的 reprex 问题:

#Animate points and regression loess line along with dates at the same time
library(MASS) #for phones reprex dataset
phones <- data.frame(phones) #Prepare reprex data
library(ggplot2) #for plotting
library(gganimate) #for animation

#Animation
ggplot(phones, aes(x = year, y = calls)) +
  geom_point() + geom_smooth(method = "loess", colour = "orange",
                             se = FALSE) +
  transition_time(year) + shadow_mark() + ease_aes("linear")
Run Code Online (Sandbox Code Playgroud)

这会返回错误:

Error in `$<-.data.frame`(`*tmp*`, "group", value = "") : 
  replacement has 1 row, data has 0 …
Run Code Online (Sandbox Code Playgroud)

animation regression r ggplot2 gganimate

2
推荐指数
1
解决办法
994
查看次数

当 x 轴是日期时,将图像插入 ggplot + gganimate

我想使用 gganimate 对一些数据进行动画处理。以他们的 github 页面为例,我对其进行了一些更改以反映我的情况。X 轴是日期,我希望所有框架的徽标都位于相同位置。

可重现的代码:

library(magick)
library(gapminder)
library(ggplot2)
library(rsvg)
library(gganimate)

tiger <- image_read_svg('http://jeroen.github.io/images/tiger.svg', width = 400)

(p <- ggplot(gapminder, aes(year, lifeExp, size = pop, colour = country)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  annotation_raster(tiger, ymin = 75, ymax = 100, xmin = 1965, xmax = 2005) )

# here the animate part (not needed just for ilustrative purposes)

p + labs(title = 'Year: {frame_time}', x …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

1
推荐指数
1
解决办法
1628
查看次数

使用 gganimate 并出现各种错误

我正在尝试使用gganimate以下方法创建动画:

library(ggplot2)
library(ggthemes)
library(gifski)
library(gganimate)

load("covid-19-es.Rda")
casos <- ggplot(data,aes(x=Fecha))+geom_point(aes(y=casos,color="Casos"))+geom_point(aes(y=salidas,color="Salidas"))+theme_tufte()+transition_states(Fecha,transition_length=2,state_length=1)+labs(title='Day: {frame_time}')
animate(casos, duration = 5, fps = 20, width =800, height = 600, renderer=gifski_renderer())
anim_save("casos.png")
Run Code Online (Sandbox Code Playgroud)

使用的数据文件在这里

最初我使用 geom_lines 而不是 geom_point,但这产生了一个错误:

Error in seq.default(range[1], range[2], length.out = nframes) : 
  'from' must be a finite number
Run Code Online (Sandbox Code Playgroud)

Error in transform_path(all_frames, next_state, ease, params$transition_length[i],  : 
  transformr is required to tween paths and lines
Run Code Online (Sandbox Code Playgroud)

要么不喜欢台词,要么不喜欢台词。切换到点,并按照 gganimate 问题中的建议创建文件。但是,这会产生不同类型的错误:

Error: Provided file does not exist
Run Code Online (Sandbox Code Playgroud)

我真的无法弄清楚,因为我根本没有提供任何文件。无论如何试图保存会产生

Error: The animation object does not specify …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

1
推荐指数
1
解决办法
846
查看次数

将 gganimate 与行一起使用时出现“错误:提供的文件不存在”

作为这个问题的后续,我尝试按照评论的建议使用geom_lineand transition_reveal。由于使用多个geom_line语句似乎与 gganimate 发生冲突(发出有关在组中使用单个元素而不渲染任何内容的警告),因此我尝试将所有内容收集到单个列和单个 ggplot2 语句中。

library(ggplot2)
library(transformr)
library(gifski)
library(gganimate)
library(tidyr)

load("covid-19-es.Rda")
data <- gather(data,Tipo,Cuantos,c(casos,salidas))
my_plot <- ggplot(data,aes(x = Fecha, y = Cuantos, group= Tipo, color=Tipo)) + 
  geom_line() +
  transition_reveal(Fecha) + ease_aes("linear")+
  labs(title='Day: {closest_state}')

animate(
  plot = my_plot,
  render = gifski_renderer(),
  height = 600,
  width = 800, 
  duration = 10,
  fps = 20)

anim_save('gifs/casos-salidas-linea.gif')
Run Code Online (Sandbox Code Playgroud)

使用的数据文件在这里。我在使用 animate 时收到很多警告,但它最终被无用的消息杀死(再次):

Error: Provided file does not exist
Run Code Online (Sandbox Code Playgroud)

归根结底,我需要的是使用 制作折线图动画ggplot2。如果还有其他方法的话,非常欢迎

使用的版本

  • 3.6
  • ggplot …

r ggplot2 gganimate

1
推荐指数
1
解决办法
1330
查看次数

标签 统计

gganimate ×10

ggplot2 ×9

r ×9

animation ×1

contour ×1

ggraph ×1

igraph ×1

regression ×1

render ×1