gganimate:具有不同几何形状和时间点的两层

编程入门 行业动态 更新时间:2024-10-24 22:25:49
本文介绍了gganimate:具有不同几何形状和时间点的两层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问题类似于

The problem is similar to this question but here the two layers use different geometries, geom_tile and geom_point. The idea is to have tiles show up at different locations only in frames 2, 5, 8, and the point move along the diagonal in every frame.

When trying to run the following example, I get the error:

Error: time data must be the same class in all layers

Example

require(data.table) require(ggplot2) require(gganimate) # 3 tiles along x = 10-y; present at time points 2, 5, 8 dtP1 = data.table(x = c(1, 5, 9), y = c(9, 5, 1), t = c(2, 5, 8)) # 9 points along x=y; present at every time point dtP2 = data.table(x = 1:9, y = 1:9, t = 1:9) p = ggplot() + geom_tile(data = dtP1, aes(x = x, y = y), color = "#000000") + geom_point(data = dtP2, aes(x = x, y = y), color = "#FF0000") + gganimate::transition_time(t) + gganimate::ease_aes('linear') pAnim = gganimate::animate(p, renderer = av_renderer("~/test.mp4"), fps = 1, nframes = 9, height = 400, width = 400)

解决方案

Does the following work for you?

library(dplyr) p <- rbind(dtP1 %>% mutate(group = "group1"), dtP2 %>% mutate(group = "group2")) %>% tidyr::complete(t, group) %>% ggplot(aes(x = x, y = y)) + geom_tile(data = . %>% filter(group == "group1"), color = "black") + geom_point(data = . %>% filter(group == "group2"), color = "red") + ggtitle("{frame_time}") + # added this to show the frame explicitly; optional transition_time(t) + ease_aes('linear') animate(p, nframes = 9, fps = 1)

更多推荐

gganimate:具有不同几何形状和时间点的两层

本文发布于:2023-07-18 04:35:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141069.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两层   几何   形状   时间   gganimate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!