在R中实时自动递增ggplot [英] Real-time auto incrementing ggplot in R

查看:121
本文介绍了在R中实时自动递增ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此主题回答了如何创建增量图R和实时效果。



我想用 ggplot2 代替R的基础。直接从该链接中的答案复制,我试过这个,但它不起作用:

  n = 1000 
df = data.frame(time = 1:n,y = runif(n))
window = 100
for(i in 1 :( n-window)){
print(i )
flush.console()
ggplot(df)+ geom_line(aes(x = time,y = y),size = 0.7)+ xlim(i,i + window)
Sys .sleep(.09)
}

我猜想 ggplot2 的工作方式不同。是否可以通过 ggplot2

创建实时更新效果理想情况下,我希望能够显示多个 geom_line s。但是我猜这个逻辑会是一样的。



另外,当R-Studio中的缩放图表时,是否有可能实现这一点?

解决方案

目前尚不清楚你将如何去做,但是这段代码会产生一个能顺利呈现的HTML动画。虽然需要一段时间(大约一分钟)来创建动画。

 库(动画)
库( ggplot2)
#你的数据
n < - 1000
df< - data.frame(时间= 1:n,y = runif(n))
窗口< 100
#创建动画
saveHTML({
for(i in 1:(n-window)){
print(ggplot(df)+ geom_line(aes(x =时间,y = y),size = 0.7)+ xlim(i,i + window))
}
})


This thread answers how to create incremental plots in R with a real-time effect.

I want to do the same thing with ggplot2 instead of R's base plot. Directly copying from the answer in that link, I tried this but it didn't work:

n=1000
df=data.frame(time=1:n,y=runif(n))
window=100
for(i in 1:(n-window)) {
  print(i)
  flush.console()
  ggplot(df) + geom_line(aes(x=time, y=y), size=0.7) + xlim(i,i+window)
  Sys.sleep(.09)
}

I guess the image rendering for ggplot2 works differently. Is it possible to create a real-time updating effect with ggplot2?

Ideally, I want to be able to display multiple geom_lines. But I guess the logic will be the same.

Also, is it possible to achieve this when the plot is "Zoom"ed in in R-Studio?

解决方案

It's not clear where you're going with this, but this code will produce an HTML animation that renders smoothly. It will take a while (about a minute) to create the animation though.

library(animation)
library(ggplot2)
# your data
n  <- 1000
df <- data.frame(time=1:n,y=runif(n))
window <- 100
# create the animation
saveHTML({
for(i in 1:(n-window)) {
  print(ggplot(df) + geom_line(aes(x=time, y=y), size=0.7) + xlim(i,i+window))
}
})

这篇关于在R中实时自动递增ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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