I am trying to fix unformated date-time axis in a real time histogram. I am using like example an adaptation from Time/Date real time plot in R.
n <- 5000
df <- data.frame(time = Sys.time()+1:n, y = runif(n))
window <- 1000
for(i in 1:(n-window)) {
flush.console()
df1 <- df[i:(i+window), ]
h <- hist(as.POSIXct(df1$time), breaks = "mins", yaxt = "n",
col = "gray", main = NULL, freq = TRUE, xlab = "",
plot = FALSE, format = "%d %H:%M")
plot(h, breaks = "mins", col = "gray", main = NULL, freq = TRUE,
xlab= "", format = "%d %H:%M")
Sys.sleep(0.01)
}
Also, I did tried with half success with the code below. But lamentably the x axis show date-time like a flash and datetime axis disappear if I decrease more Sys.sleep
, axis don't look so good like unformated date-time axis in my first example.
n <- 5000
df <- data.frame(time = Sys.time()+1:n, y = runif(n))
window <- 1000
for(i in 1:(n-window)) {
flush.console()
df1 <- df[i:(i+window), ]
x_at <- pretty(df1$time)
x_labels <- format(pretty(df1$time), "%d %H:%M")
hist(df1$time, df1$y,type='l', breaks = "mins", xaxt = 'n')
axis.POSIXct(side = 1, at = x_at, labels = x_labels)
Sys.sleep(0.01)
}
Could be another method to see the x axis in a better way?
The problem is that hist()
knows about time axes when you pass it a POSIXt object, but plot()
doesn't when you pass it a histogram object like h
. So just do everything in one step, not two:
n=5000
df=data.frame(time=Sys.time()+1:n,y=runif(n))
window=1000
for(i in 1:(n-window))
{
flush.console()
df1 <-df[i:(i+window),]
hist(as.POSIXct(df1$time), breaks="mins",
yaxt = "n", col="gray", main=NULL, freq = TRUE, xlab= "",
format = "%d %H:%M")
Sys.sleep(0.01)
}