动画点直方图,通过观察构建观察(在 R 中使用 gganimate)

max*_*max 20 r data-visualization ggplot2 gganimate

我想从正态分布中采样点,然后使用gganimate包一个一个地建立一个点图,直到最后一帧显示完整的点图。

适用于约 5,000 - 20,000 个点的更大数据集的解决方案至关重要。

这是我到目前为止的代码:

library(gganimate)
library(tidyverse)

# Generate 100 normal data points, along an index for each sample 
samples <- rnorm(100)
index <- seq(1:length(samples))

# Put data into a data frame
df <- tibble(value=samples, index=index)
Run Code Online (Sandbox Code Playgroud)

df 看起来像这样:

> head(df)
# A tibble: 6 x 2
    value index
    <dbl> <int>
1  0.0818     1
2 -0.311      2
3 -0.966      3
4 -0.615      4
5  0.388      5
6 -1.66       6
Run Code Online (Sandbox Code Playgroud)

静态图显示了正确的点图:

# Create static version
plot <- ggplot(data=df, mapping=aes(x=value))+
          geom_dotplot()
Run Code Online (Sandbox Code Playgroud)

但是,该gganimate版本没有(见下文)。它只将点放在 x 轴上,而不是堆叠它们。

plot+
  transition_reveal(along=index)
Run Code Online (Sandbox Code Playgroud)

静态图

在此处输入图片说明

与此类似的东西将是理想的:信用:https : //gist.github.com/thomasp85/88d6e7883883315314f341d2207122a1 在此处输入图片说明

Tje*_*ebo 17

另一种选择是用另一个几何图形绘制点。您需要首先对您的数据进行一些计数(和分箱),但它不需要使您的数据更长。

例如,您可以使用geom_point,但挑战在于正确设置点的尺寸,使它们接触/不接触。这取决于设备/文件大小。

但你也可以ggforce::geom_ellipse用来画你的点:)

geom_point(设备尺寸的试错)

library(tidyverse)
library(gganimate)

set.seed(42)
samples <- rnorm(100)
index <- seq(1:length(samples))
df <- tibble(value = samples, index = index)

bin_width <- 0.25

count_data <- # some minor data transformation
  df %>%
  mutate(x = plyr::round_any(value, bin_width)) %>%
  group_by(x) %>%
  mutate(y = seq_along(x))

plot <-
  ggplot(count_data, aes(group = index, x, y)) + # group by index is important
  geom_point(size = 5)

p_anim <- 
  plot +
  transition_reveal(index)

animate(p_anim, width = 550, height = 230, res = 96)
Run Code Online (Sandbox Code Playgroud)

geom_ellipse(完全控制点大小)

library(ggforce)
plot2 <- 
  ggplot(count_data) +
  geom_ellipse(aes(group = index, x0 = x, y0 = y, a = bin_width/2, b = 0.5, angle = 0), fill = 'black') +
  coord_equal(bin_width) # to make the dots look nice and round

p_anim2 <- 
  plot2 +
  transition_reveal(index) 

animate(p_anim2) 
Run Code Online (Sandbox Code Playgroud)

在您提供给 thomas 令人惊叹的示例的链接中进行更新,您可以看到他使用了类似的方法 - 他使用 geom_circle 而不是 geom_ellipse,我之所以选择 geom_ellipse,是因为可以更好地控制垂直和水平半径。

要获得“落水”效果,您将需要transition_states很长的持续时间和每秒许多帧。

p_anim2 <- 
  plot2 +
  transition_states(states = index, transition_length = 100, state_length = 1) +
  shadow_mark() +
  enter_fly(y_loc = 12) 

animate(p_anim2, fps = 40, duration = 20) 
Run Code Online (Sandbox Code Playgroud)

reprex 包(v0.3.0)于 2020 年 4 月 29 日创建

一些灵感来自:ggplot dotplotgeom_dotplot 的正确用法是什么?


ste*_*fan 5

尝试这个。基本思想是将 obs 分组到帧,即按索引分割,然后将样本累积到帧中,即在第 1 帧中仅显示第一个 obs,在第 2 帧中 obs 1 和 2,..... 也许有是实现这一目标的更优雅的方式,但它有效:

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

set.seed(42)

# example data
samples <- rnorm(100)
index <- seq(1:length(samples))

# Put data into a data frame
df <- tibble(value=samples, index=index)

# inflated df. Group obs together into frames
df_ani <- df %>% 
  split(.$index) %>% 
  accumulate(~ bind_rows(.x, .y)) %>% 
  bind_rows(.id = "frame") %>% 
  mutate(frame = as.integer(frame))
head(df_ani)
#> # A tibble: 6 x 3
#>   frame  value index
#>   <int>  <dbl> <int>
#> 1     1  1.37      1
#> 2     2  1.37      1
#> 3     2 -0.565     2
#> 4     3  1.37      1
#> 5     3 -0.565     2
#> 6     3  0.363     3

p_gg <- ggplot(data=df, mapping=aes(x=value))+
  geom_dotplot()
p_gg
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
Run Code Online (Sandbox Code Playgroud)

p_anim <- ggplot(data=df_ani, mapping=aes(x=value))+
  geom_dotplot()

anim <- p_anim + 
  transition_manual(frame) +
  ease_aes("linear") +
  enter_fade() +
  exit_fade()
anim
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
Run Code Online (Sandbox Code Playgroud)

reprex 包(v0.3.0)于 2020 年 4 月 27 日创建


che*_*123 5

我认为这里的关键是想象你将如何手动创建这个动画,也就是说你将一次一个观察添加点到生成的点图。考虑到这一点,我在这里使用的方法是创建一个ggplot由绘图层数 = 观察数组成的对象,然后通过transition_layer.

# create the ggplot object
df <- data.frame(id=1:100, y=rnorm(100))

p <- ggplot(df, aes(y))

for (i in df$id) {
  p <- p + geom_dotplot(data=df[1:i,])
}

# animation
anim <- p + transition_layers(keep_layers = FALSE) +
    labs(title='Number of dots: {frame}')
animate(anim, end_pause = 20, nframes=120, fps=20)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

请注意,我设置keep_layers=FALSE为避免过度绘图。如果你绘制初始ggplot对象,你会明白我的意思,因为第一个观察被绘制了 100 次,第二次被绘制了 99 次......等等。

对于更大的数据集进行缩放呢?

由于帧数 = 观察数,您需要调整可扩展性。在这里,只需保持 # frames 不变,这意味着您必须让代码将帧分组为段,我正在通过seq()函数执行此操作,指定length.out=100. 另请注意,在新示例中,数据集包含n=5000. 为了将点图保持在框架中,您需要使点的大小非常小。我可能把这里的点弄得太小了,但你懂的。现在#frames = 观察组的数量。

df <- data.frame(id=1:5000, y=rnorm(5000))

p <- ggplot(df, aes(y))

for (i in seq(0,length(df$id), length.out=100)) {
  p <- p + geom_dotplot(data=df[1:i,], dotsize=0.08)
}

anim <- p + transition_layers(keep_layers=FALSE) +
  labs(title='Frame: {frame}')

animate(anim, end_pause=20, nframes=120, fps=20)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明