标签: gganimate

是否有可能生成随机数字的gganimate图?

我希望生成一个gganimate对象,显示特定范围内的n个随机点.另一个限制是它应该绘制2^n点,意味着2, 4, 8, 16, 32, 64...点.我这样做是为了计算小数,pi但我想绘制这个动画,这样我就可以展示它如何以更好的方式给出更多的随机数来改善结果.

这是我到目前为止:

results <- c()
for(i in c(1:20)) {
  r <- 1
  limit <- 2^i
  points <- data.frame(
    x = runif(limit, -r, r), 
    y = runif(limit, -r, r))
  points$d <- sqrt(points$x^2 + points$y^2)
  points$type <- ifelse(points$d < r, "c", "s")
  picalc <- 4 * length(points$type[points$type=="c"]) / limit
  error <- pi - picalc
  label <- paste0('Pi calc : ', round(picalc, 6), '\nError : ', round(error, 6))
  iter <- …
Run Code Online (Sandbox Code Playgroud)

pi r ggplot2 gganimate

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

gganimate 和(有时)空方面的问题

我正在观察一些gganimate我无法解释的行为,我想了解我做错了什么(或者它是否是一个错误)。

例如,这是一个非常简单的数据集及其绘图:

library(dplyr) # dplyr_0.7.8
library(tidyr) # tidyr_0.8.2 

crossing(p = 1:2, 
         t = seq(0, 1, len = 30),
         s = c(0, .5)) %>%
  mutate(x = t,
         y = t^p) %>%
  filter(t > s) ->
  Z

library(ggplot2) # ggplot2_3.1.0

Z %>%
  ggplot(aes(x,y)) +
  facet_wrap(~s) +
  geom_point()
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,第二个方面 (s=0.5) 仅具有 x > 0.5 的数据,其中(根据 tibble Z 的构造方式)来自 t > 0.5。

如果要为上述数据设置动画(t用作时间),我希望动画的前半部分的第二个方面为空,然后在后半部分显示与第一个方面相同的方面。然而:

library(gganimate) # gganimate_1.0.2
Z %>%
  ggplot(aes(x, y, group = interaction(p,s))) +
  facet_wrap(~s) +
  geom_point() +
  transition_time(t) + …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

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

gganimate 不会显示移动点的轨迹

我正在尝试为我制作的 ggplot 创建动画,但它没有使用 transition_time 创建轨迹,我已经阅读并看到人们使用 transition_reveal 但收到此错误消息

png::readPNG(frames 1 , native = TRUE) 中的错误:无法打开 /var/folders/_z/y8zrlqwj4fs2wgkxyrztvqhr0000gn/T//Rtmp2zZJvG/1a492ff72039/gganim_plot0001 或更多警告(有 s50 使用警告。 ) 查看前 50 个)

下面是带有函数 transition_time 的 gif

在此处输入图片说明

在最后一帧,我试图让它看起来像我在下面制作的原始 ggplot

在此处输入图片说明

这是我的一部分数据

library(ggplot2)
library(dplyr)
library(gganimate)

RCT_NIH_mod <- structure(list(Year.Published = c(1993, 1993, 1993, 1993, 1993, 
1993, 1993, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1995, 1995, 
1995, 1995, 1995, 1995, 1995), group = structure(c(2L, 3L, 4L, 
5L, 6L, 1L, 7L, 2L, 3L, 4L, 5L, 6L, 1L, 7L, 2L, 3L, 4L, 5L, 6L, 
1L, 7L), …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

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

gganimate:结合transition_layers和geom_smooth

如何将geom_smooth(method =“ lm)函数与gganimate()的transition_layers结合使用,以便随着各个条的向上偏移/增长,出现geom_smooth的线性线,如下所示:geom_smooth线所需外观示例唯一的不同是,在我的情况下,随着线的出现,条形图会向上移动而不是点。

通过使用gganimate的transition_layers函数,通过向上漂移而出现的条形电流效果很好。

但是,我无法弄清楚如何添加geom_smooth线,因此随着条形的增长而出现。现在,该行显示在结尾处,如下所示。

有关动画的当前外观,请参见下文。

这是我的问题的简单代表:

#Df for reprex
library(ggplot2)
library(tidyverse)

year <- as.numeric(c(1996:2002,
                     1996:2002,
                     1996:2002))
c <- c(39, 40, 67, 80, 30, 140, 90, 23, 100, 123,
       140, 1, 2, 1, 13, 3, 3, 30, 1, 3, 3)
df <- data.frame(year, c) %>%
  select(year, c) %>%
  arrange(year)

#Static plot
(static_plot <- ggplot(data = df) +
    geom_bar(data = df %>% filter(year == 1996), stat="identity", position ="stack",
             aes(x = year, y = c)) +
    geom_bar(data = …
Run Code Online (Sandbox Code Playgroud)

plot animation r ggplot2 gganimate

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

is there any way to create an animation (gganimate) with ranges as transition times?

Is there any way to make gganimate work for transition times that are ranges of years? In my data I have three time points, two of which are ranges as shown below.

data:

Year   rate   group
2012-2014   7   Other CT, White 
2015-2017   11  Other CT, White 
2018    3   Fairfield, Black    
2018    2   Fairfield, Hispanic
Run Code Online (Sandbox Code Playgroud)

here's an example of the code for the ggplot that I'd like to animate

data %>% ggplot(aes(y = rate, x = group)) +
  geom_col() +
  coord_flip() …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

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

在 R 中放慢 gganimate

我如何减慢下面的情节?我正试着慢慢地度过这些日子。这是按天显示的三年数据。每天应该是大约 0.25 秒。

在此处输入图片说明

我用来构建这个图的代码如下:

library(tidyverse)
library(gganimate)

df_daily %>%
    ggplot(aes(longitude, latitude)) +
    geom_point(aes(alpha = a)) +
    transition_time(flu_day) +
    ease_aes('linear', interval = 0.001) +
    labs(title = 'Date: {frame_time}')
Run Code Online (Sandbox Code Playgroud)

r gganimate

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

标签 统计

gganimate ×6

r ×6

ggplot2 ×5

animation ×1

pi ×1

plot ×1