动画点直方图,通过观察建立观察(在R中使用gganimate) [英] Animated dot histogram, built observation by observation (using gganimate in R)

查看:78
本文介绍了动画点直方图,通过观察建立观察(在R中使用gganimate)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

适用于约5,000-20,000点的较大数据集的解决方案必不可少。



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

 库(gganimate)
库(tidyverse)

#生成100正常数据点以及每个样本的索引
样本<-rnorm(100)
索引<-seq(1:length(samples))

#放入数据进入数据框
df<-tibble(值=样本,索引=索引)

df如下:

 > head(df)
#小动作:6 x 2
价值指数
< 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

静态图显示正确的点图:

 #创建静态版本
图<-ggplot(data = df,mapping = aes(x = value))+
geom_dotplot()

但是, gganimate 版本没有(见下文)。

  plot + 
transition_reveal(along =索引)





类似于以下内容的比较理想:
赠送金额:

解决方案

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



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



但是您也可以只使用 ggforce :: geom_ellipse 来绘制点:)



geom_point (设备尺寸的尝试和错误)

 库(tidyverse)
库(gganimate)

set.seed(42)
样本<-rnorm (100)
索引<-seq(1:length(samples))
df <-tibble(值=样本,索引=索引)

bin_width <- 0.25

count_data<-#一些次要数据转换
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))+#按索引分组很重要
geom_point(size = 5)

p_anim<-
图+
transition_reveal(index)

动画(p_anim,宽度= 550,高度= 230,分辨率= 96)



geom_ellipse (完全控制点大小)

 库(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)#使点看起来很漂亮并且圆整

p_anim2<-
plot2 +
transition_reveal(index)

动画(p_anim2)



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



要获得下降滴效果,您将需要 transition_states 且持续时间长且每秒需要很多帧。

  p_anim2<-
plot2 +
transition_states(状态=索引,transition_length = 100,state_length = 1)+
shadow_mark()+
enter_fly(y_loc = 12)

动画(p_anim2,fps = 40,持续时间= 20)



reprex软件包(v0 .3.0)



一些灵感来自于:


I would like to sample points from a normal distribution, and then build up a dotplot one by one using the gganimate package until the final frame shows the full dotplot.

A solution that works for larger datasets ~5,000 - 20,000 points is essential.

Here is the code I have so far:

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)

The df looks like this:

> 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

The static plot shows the correct dotplot:

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

However, the gganimate version does not (see below). It only puts the dots on the x-axis and doesn't stack them.

plot+
  transition_reveal(along=index)

Something similar to this would be ideal: Credit: https://gist.github.com/thomasp85/88d6e7883883315314f341d2207122a1

解决方案

Another option is to draw the points with another geom. you will need to do some counts on your data first (and binning) but it doesn’t require making your data longer.

For example, you can use geom_point, but the challenge will be to get the dimensions of your points right, so they touch/do not touch. This depends on the device / file size.

But you can also just use ggforce::geom_ellipse to draw your dots:)

geom_point (trial and error with device dimensions)

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)

geom_ellipse (Full control of point size)

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) 

update in the link which you provide to thomas's amazing example, you can see that he uses a similar approach - he uses geom_circle instead of geom_ellipse, which I chose because of better control for both vertical and horizontal radius.

To get the "falling drops" effect, you will need transition_states and a long duration and many frames per second.

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) 

Created on 2020-04-29 by the reprex package (v0.3.0)

some inspiration from: ggplot dotplot: What is the proper use of geom_dotplot?

这篇关于动画点直方图,通过观察建立观察(在R中使用gganimate)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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