How to make a one dimensional scatterplot of points in time? - r

I want to create a one dimensional scatterplot of points in time (range of about 5 hours), to visualise e.g. the time when I get up in the morning.
I tried
time=rep(Sys.time(),100)+round(3600*rnorm(100),1)
stripchart(as.numeric(time), main="stripchart", method="jitter", jitter = 2)
but that gives me
where I believe time is given in seconds since epoch. I'm interested in times of the day(8:02, 7:50,...) so time in seconds does not work for me. I need as.numeric however as I get 'invalid first argument' for leaving it out.

Plot the chart without the x-axis labels with xaxt="n" and add it afterwards with hours and minutes using axis. I'm using pretty to get the beginning of the hour.
time=rep(Sys.time(),100)+round(3600*rnorm(100),1)
stripchart(as.numeric(time), main="stripchart", method="jitter", jitter = 2, xaxt="n")
axis(side=1,pretty(time), format(pretty(time),"%H:%M"))

You could do this:
time=rep(Sys.time(),100)+round(3600*rnorm(100),1)
minutes <- (time - min(time))/60
stripchart(as.numeric(minutes), main="stripchart", method="jitter", jitter = 2)
Which yields this (x-axis in minutes):

Related

gnuplot frequency chart timestamp

I have a list of timestamps like shown at the end, and I am trying to make a histogram/frequency chart where the X-axis is the time and the y axis a count of the frequency. I tried doing something like this:
set term dumb
set xdata
set timefmt '%Y-%m-%dT%H:%M:%SZ'
set xrange ["16/04/2018":"14/04/2018"]
plot 'dat'
and in the data file
2018-04-16T14:54:38Z
2018-04-16T13:50:07Z
2018-04-16T12:30:45Z
2018-04-16T11:45:16Z
2018-04-16T11:44:11Z
2018-04-16T10:19:19Z
2018-04-16T10:13:25Z
2018-04-16T00:17:33Z
2018-04-15T23:10:15Z
2018-04-14T22:27:34Z
2018-04-14T23:11:29Z
2018-04-14T22:45:08Z
Result
As you can see the x axis does not represent the time.
EDIT:
With Christoph's fixed code, the result is a plot of the timestamp and row the number.
To better explain the problem I have:
I want to plot the time on the x-axis and I want to plot the number of occurrences of the timestamp on the y-axis and where the timestamps are binned to some value in hours and possibly instead of just drawing bars it would interpolate lines between the peaks of the bars.
I asked for you actual question, because the code you posted doesn't create any plot. Let's see:
If I execute only the code you posted, I get the error all points y value undefined!.
In plot time data on the x axis, you need set xdata time.
Having set that you would get as next error Need full using spec for x time data. Indeed, you must have a using when handling time data, something like
plot 'dat' using 1:0
Then, you would again get the error all points y value undefined!. This time, because the time strings given in set xrange must match those specified with set timefmt.
So, a working version of your code could be:
reset
set xdata time
set timefmt '%Y-%m-%dT%H:%M:%SZ'
set xrange ["2018-04-16":"2018-04-14"]
plot 'dat' using 1:0
This plots time on the xaxis, and the row number on the y-axis.

R microbenchmark and autoplot?

I have a couple of questions about microbenchmark and autoplot
Suppose this is my code:
library("ggplot2")
tm <- microbenchmark(rchisq(100, 0),rchisq(100, 1),rchisq(100, 2),rchisq(100, 3),rchisq(100, 5), times=1000L)
autoplot(tm)
What are the units in tm$time? how can i convert that to seconds?
How can I change the marks on the x axis to something like seq(from=0, to=100,by = 5)?
Thanks!
help(microbenchmark) gives:
1. ‘time’ is the measured execution time
of the expression in nanoseconds.
NANOseconds not milliseconds or microseconds.
So divide by 1000000000 to convert to seconds.
And for your second question, my first response is "why?". But its ggplot-based, so you can override bits by adding ggplot things:
autoplot(tm) + scale_y_log10(name="seq(whatever)")
Note the plot is rotated so the x-axis is the y-scale....
I've just thought you really mean "tick marks"? Slightly different but doable, but not really appropriate given the log axis. You can force a non-log axis with specified tick marks:
autoplot(tm) + scale_y_continuous(breaks=seq(0,10000,len=5),name="not a log scale")
You can keep the log scale and set the tick mark points:
autoplot(tm) + scale_y_log10(breaks=c(50,200,500))

How do I make R lattice xyplot ignore gaps in time and make a continuous timeseries plot?

I have an xts series that I'm trying to plot. This series contains of intra-day date for a month with gaps in the data on the weekend. I use xyplot (lattice) in R to plot the time series and am very pleased with the results.
Unfortunately the plots keep the weekend gaps. I'd like to ignore the weekend gaps and make my timeseries plot continuous and would appreciate if someone pointed me in the right direction.
The current command is :
xyplot(close~MyTime, type='l', col='black',ylab='',xlab='', main='Test')
I tried JohnPaul's method and it 'nearly' works. The labels while present, don't render correctly. The last label only goes up to the 3rd of January, while the actual data extends all the way up to February. The command used was:
PlotOrd<-order(Mytime)
xyplot(close~PlotOrd, type='l', col='black',ylab='',xlab='', main='Close',scales=list( x=list( labels=MyTime)) )
If I understand this correctly, what you wish to do is have the weekends not appear in the plot at all. One way to do this is to make another vector which is the order in which you want close plotted - a vector that does not include weekends. Assuming that weekends are not included in time this should work:
PlotOrd<-order(Mytime)
xyplot(close~PlotOrd, type='l', col='black',ylab='',xlab='', main='Test')
This will give you the correct plot, but your x axis tick labels will just be the number from PlotOrd. If you want to keep them as dates add a scales argument for the x axis, like so:
xyplot(close~PlotOrd, type='l', col='black',ylab='',xlab='', main='Test',
scales=list( x=list( labels=Mytime )) )
EDIT
One way to control the axis labels is to use the at argument as well. It is kind of clunky here and I wish I could come up with a more elegant idea, but this will work:
xyplot(close~PlotOrd, type='l', col='black',ylab='',xlab='', main='Test',
scales=list( x=list(at=c(50,100,150,200), labels=Mytime[c(50,100,150,200)] )) )
This will put ticks at observations 50,100,150 and 200, and will give corresponding values from MyTime as the label. The downside is you have to write in the ticks yourself. You could add some code to make a sequence of values. Say you want a label every 15 days. If you figure out how many measurements that corresponds to, you can make a sequence of numbers that far apart (call it MyTicks). Then feed to at=MyTicks and labels=MyTime[MyTicks]. Still it would be nicer to have lattice just pick the ticks for you...

Time Series Trend

I have a time series over 10 years with no seasonal changes (only one value per year) and I try to detect a trend. I dont really understand how to do so.
I read that the moving average is used in this case
What I did so far:
CharFS <- read.csv("./DataInvestigation/TEST.csv", header=TRUE, sep=";")
ma.1 <- rep(1/5,5)
ma.2 <- rep(1/25,25) #How do I know what I should take?
ma.3 <- rep(1/81,81)
CharMA <- filter(CharFS, ma.1, sides=2)
plot(CharFS)
lines(ma.1, lty=2, col="blue")
lines(ma.2, lty=2, col="blue")
lines(ma.3, lty=2, col="blue")
However, no line shows up. I assume that ma.1,ma.2,ma.3 are wrong is but I dont know how to adjust it to my data, any ideas?
Or is there a better way to get a trend when there are no seasons involved? Would it be possible with a normal plot and then add a line? This did not work either when I tried it though.
Thanks in advance!
It would be useful if you could show us the plot of your data with the moving averages lines. Or maybe provide some data.
The reason they don't show up in the plot might have something to do with range of the data you use. What is the mean of CharFS? Try to plot CharFS and then add a horizontal line for the mean of the time series.
With regard to trends in time series data: basically when you have a trend in the data you will see that the data moves in a certain direction (e.g. GDP figures) and does not revolve around a certain mean (e.g. GDP growth). See the two examples that I have plotted below.
Since you're using R this might be a helpful resource using time series data: Little Book of R for Time Series

Is there a method to omit time intervals in plot.zoo function of R?

I have a time series of zoo and POSIXct class using second intervals. However, as is usually the case in financial time series, there are often long time gaps without information (ex. overnight). When using plot.zoo, the useful (populated) intervals appear scrunched relative to the unused intervals. Is there some way to simply omit time intervals in the plotting function of plot.zoo(). P.S. It's possible to just simply plot the continuous time series (plot(ts(obj)), but then the time information is lost on the x axis.
#AndresT Thanks for the idea, but the data is already filtered. It is the plot.zoo() function that appears to be padding unused sample intervals to keep the total x range
active. Pls see example below.
library(zoo)
st<-Sys.time()
t<-c(st, st+500, st+1000, st+1500, st+2000, st+90000, st+95000, st+100000)
rn<-runif(7)
zr<-zoo(rn, order.by=t)
plot.zoo(zr,type='o')
compare to the uniform spacing from plot(ts(data))
layout(1:2)
plot.zoo(zr, type='o')
plot(ts(zr),type='o')
So I would like to basically have the 2nd ts plot, but labeled with the corresponding x-axis labels from the 1st plot.zoo() function.
Try this:
library(quantmod)
chartSeries(zr)

Resources