Real Time, Auto Updating, Incremental Plot in R

Real time, auto updating, incremental plot in R

Do you want to do something like this?

n=1000
df=data.frame(time=1:n,y=runif(n))
window=100
for(i in 1:(n-window)) {
flush.console()
plot(df$time,df$y,type='l',xlim=c(i,i+window))
Sys.sleep(.09)
}

Going through your code:

# for(i in 1: length(temp$Time[1:10000])) { 
for (i in 1:10000) # The length of a vector of 10000 is always 10000
flush.console()
# plot(temp$Time[i:i+100],temp$Open[i:i+100],
# Don't bother subsetting the x and y variables of your plot.
# plot() will automatically just plot those in the window.
plot(temp$Time,temp$Open,
xlim=c(as.numeric(temp$Time[i]),as.numeric(temp$Time[i+150]))
# Why are you setting the y limits dynamically? Shouldn't they be constant?
# ,ylim=c(min(temp$Open[i:i+100]),max(tmep$Open[i:i+120])))
Sys.sleep(.09)
}

Real-time auto incrementing ggplot in R

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))
}
})

Incrementally add seconds of a timestamp column grouped by ID in R

One solution with data.table:

For the data:

library(data.table)
df <- as.data.table(df)
df$Timestamp <- as.POSIXct(df$Timestamp, format='%m/%d/%Y %H:%M:%S')
df[, Interval := as.numeric(difftime(Timestamp, .SD[1, Timestamp], units='secs') + 1) , by=ID]

which outputs:

> df
Timestamp ID v1 Interval
1: 2015-01-27 18:28:16 A 1.70 1
2: 2015-01-27 18:28:17 A 1.71 2
3: 2015-01-27 18:28:19 A 1.77 4
4: 2015-01-27 18:28:20 A 1.79 5
5: 2015-01-27 18:28:23 A 1.63 8
6: 2015-01-28 22:43:08 B 7.20 1
7: 2015-01-28 22:43:09 B 7.26 2
8: 2015-01-28 22:43:13 B 7.16 6
9: 2015-01-28 22:43:15 B 7.18 8
10: 2015-01-28 22:43:16 B 7.18 9

Then for ggplot:

library(ggplot2)
ggplot(df, aes(x=Interval, y=v1, color=ID)) + geom_line()

and the graph:

Sample Image

3D scatter plot in real time

This can be accomplished using the R packages scatterplot3d and animation. The latter requires installation of ImageMagick.

Here is some code that accomplishes something like what you want to do.

require(animation)
require(scatterplot3d)

# Get some example data
n <- 10
dt <- data.frame(birth = rnorm(n, 50, 20),
age = sample(1:100, n, replace=TRUE),
cause = sample(1:3, n, replace=TRUE),
race = sample(1:5, n, replace=TRUE),
bweight = rnorm(n, 1000, 200),
blen = rnorm(n, 300, 20))

# Starting and final timepoint
st <- 1
ft <- 150

# All the timepoints to evaluate
times <- st:ft

# Matrices that show for each timepoint whether a person is alive or dead.
born <- outer(dt$birth, times, "<")
dead <- outer(dt$birth + dt$age, times, "<")

# Matrix is 0 for not yet born, 1 for living, 2 for dead.
status <- born + dead

# If dead, this contains the status of the cause.
deadcause <- matrix(dt$cause, nc=length(times), nrow=n) * dead + born

# Function to get animations
anim <- function(dt, times) {
for(i in seq_along(times)) {

# Remove those not yet born.
dtcur <- dt * born[, i]

scatterplot3d(x=dtcur$race,
y=dtcur$bweight,
z=dtcur$blen,
color=deadcause[, i],
angle = 135, type = "h",
main=paste("At time", i),
xlim=c(0,5),
ylim=c(0,1500),
zlim=c(0,500))
animation::ani.pause()
}
}

# Save the gif to current directory.
saveGIF(expr = anim(dt, times), interval=.1, outdir = getwd())

Real time plot in MATLAB

You can add a call to DRAWNOW to force the plot to update. See the reference page. Note that DRAWNOW causes the graphics event queue to be flushed, which may cause callbacks etc. to be executed.



Related Topics



Leave a reply



Submit