格尼玛特图,点保持不变,线变淡 [英] gganimate plot where points stay and line fades

查看:47
本文介绍了格尼玛特图,点保持不变,线变淡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是静态图的可复制示例,我想对其进行动画处理(我想展示MCMC采样器的行为方式).

Here is a reproducible example of a static plot, which I want to animate (I want to show how a MCMC sampler behaves).

library(tidyverse)
library(gganimate)

set.seed(1234)
plot_data <- tibble(x=cumsum(rnorm(100)),
                    y=cumsum(rnorm(100)),
                    time=1:length(x)) 

ggplot(data=plot_data,
       aes(x=y, y=x)) +
  geom_point() + geom_line()

我想看到的是绘制点时它们是可见的,然后一点点褪色(即alpha从1变为0.3),而会有一行仅显示最近的历史(理想情况下)淡入淡出,以显示最近的历史,淡入淡出的程度最少,并且退后几步完全消失).

What I'd like to see is the points being visible when they are drawn and a bit faded (i.e. alpha goes from e.g. 1 to 0.3) afterwards, while there would be a line that only shows the recent history (and ideally fades showing the most recent history the least faded and more than a few steps back totally disappearing).

以下内容或多或少地达到了我想要的点数(因此,在某种意义上,我只想向连接最后几个点的部分添加渐变线-在某些帧上渐变得更慢的点会更好):

The following achieves more or less what I want for my points (so in a sense I just want to add fading lines to this connecting the last few points - points fading more slowly across some frames would be even nicer):

ggplot(data=plot_data,
       aes(x=y, y=x)) +
  geom_point() +
  transition_time(time) +
  shadow_mark(past = T, future=F, alpha=0.3)

我正在努力的是如何为两个几何添加两种不同的行为,例如点和线.例如.在下面的点消失(我不想让它们消失),而线条也不会褪色(我希望它们消失).

What I am struggling with is how to add two different behaviors for two geoms e.g. point and line. E.g. in the below the points disappear (I don't want them to) and the lines do not fade (I want them to).

p <- ggplot(data=plot_data,
       aes(x=y, y=x)) +
  geom_point() +
  transition_time(time) +
  shadow_mark(past = T, future=F, alpha=0.3)

p + geom_line() +
  transition_reveal(along = time) +
  shadow_mark(past = T, future=F, alpha=0.3) 

推荐答案

我无法使用内置的 shadow _ * 函数来一次控制多个行为.它似乎仅适用于最新的一种.(使用gganimate 1.0.3.9000)

I had trouble using the built-in shadow_* functions to control more than one behavior at a time; it seemed to just apply the most recent one. (Using gganimate 1.0.3.9000)

解决此问题的一种方法是手动计算转换.例如,我们可以将数据复制100次,每帧复制一份,然后分别为我们的点层和段层分别指定alpha.

One way to get around this is to calculate the transitions manually. For instance, we could copy the data 100 times, one copy for each frame, and then specify the alpha for our points layer and the alpha for our segment layer separately.

plot_data %>%
  uncount(100, .id = "frame") %>%
  filter(time <= frame) %>%
  arrange(frame, time) %>%
  group_by(frame) %>%
  mutate(x_lag = lag(x), 
         y_lag = lag(y),
         tail = last(time) - time,
         # Make the points solid for 1 frame then alpha 0.3
         point_alpha = if_else(tail == 0, 1, 0.3),
         # Make the lines fade out over 20 frames
         segment_alpha = pmax(0, (20-tail)/20)) %>%
  ungroup() %>%

  ggplot(aes(x=y, y=x, xend = y_lag, yend = x_lag, group = time)) +
  geom_segment(aes(alpha = segment_alpha)) +
  geom_point(aes(alpha = point_alpha)) +
  scale_alpha(range = c(0,1)) +
  guides(alpha = F) +
  transition_manual(frame)

(对于此渲染,我将其包裹在 animate([上方]中,宽度= 600,高度= 400,类型="cairo"))

(For this render, I wrapped it in animate( [everything above], width = 600, height = 400, type = "cairo"))

这篇关于格尼玛特图,点保持不变,线变淡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆