首页 > 解决方案 > R中带有时间戳的实时自动递增ggplot

问题描述

继续这个线程:

R中的实时自动递增ggplot

library(animation)
library(ggplot2)

# your data
n  <- 200
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))
  }
})

以上作品精美

当我生成带有日期和时间的时间戳时。它不再起作用了。该图是静态的,而不是在 x 轴上沿时间移动。

timestamp <- seq(ISOdate(2016, 12, 01), ISOdate(2016, 12, 31), "hour")
df2 <- data.frame(timestamp, y = runif(length(timestamp)))
window <- 100


# create the animation
saveHTML({
  for (i in 1:(length(timestamp)-window)) {
    print(ggplot(df2) + 
            geom_line(aes(timestamp, y), size = 0.20) + 
            ggtitle("Real-time ggplot") +
            xlab("time") + 
            ylab("count"))

  }
})

标签: rggplot2real-time

解决方案


推荐阅读