1
votes

I have an irregular time series. Each row represents x number of ticks, (DateTime, Price, Volume) aggregated to show HLOC, using the first tick of each group to set the DateTime for the row.

"Time","Open","High","Low","Close","SumVol","SumTicks"
...
2016-01-15 11:14:43,4.74,4.74,4.7325,4.735,298,250
2016-01-15 11:14:56,4.735,4.735,4.7275,4.7325,475,250
2016-01-15 11:16:49,4.7325,4.76,4.7275,4.7575,903,250
2016-01-18 17:00:15,4.7575,4.765,4.75,4.755,327,250
2016-01-18 17:02:17,4.755,4.7575,4.7375,4.7375,398,250
2016-01-18 17:05:23,4.7375,4.7375,4.715,4.7175,395,250
2016-01-18 17:08:56,4.7175,4.72,4.7125,4.7175,509,250
2016-01-18 17:22:06,4.7175,4.725,4.7175,4.7175,326,250
2016-01-18 17:57:25,4.7175,4.7225,4.7125,4.7125,349,250
2016-01-18 18:33:55,4.7125,4.725,4.7125,4.725,293,250
2016-01-18 20:54:43,4.725,4.735,4.725,4.7275,272,250
2016-01-18 22:49:55,4.7275,4.73,4.72,4.7225,335,250
2016-01-19 00:14:32,4.7225,4.73,4.7225,4.73,430,250
...

Because this data goes over weekends and includes night trading where the activity is low, the records are very unevenly spaced time-wise. I want to include the date/time information for general reference on the chart, but maintain evenly distributed bars as in the second plot below.

Instead of this:

enter image description here

I want it to look like this, but with date/times instead of row numbers:

enter image description here

Is there some way to overlay the x-axis in the second chart with the date/time column (or for example, every 10th entry) vertically?

I tried the suggestion to convert time into a factor, but the resulting plot does not connect the points plot(Close ~ as.factor(Time), data = dtTicksSum, type ="s") enter image description here

2
try converting the date/time to a factor?tospig

2 Answers

1
votes

As pointed out by tospig in the comments, this works (tested to verify):

plot(Close ~ as.factor(Time), data = dtTicksSum)

as.factor will create the integer codes in matching temporal order.

1
votes

Just expanding on @SenorO 's answer, and using the data you've provided

dtTicksSum <- data.frame(dateTime = readLines(textConnection("2016-01-15 11:14:43,4.74,4.74,4.7325,4.735,298,250
2016-01-15 11:14:56,4.735,4.735,4.7275,4.7325,475,250
2016-01-15 11:16:49,4.7325,4.76,4.7275,4.7575,903,250
2016-01-18 17:00:15,4.7575,4.765,4.75,4.755,327,250
2016-01-18 17:02:17,4.755,4.7575,4.7375,4.7375,398,250
2016-01-18 17:05:23,4.7375,4.7375,4.715,4.7175,395,250
2016-01-18 17:08:56,4.7175,4.72,4.7125,4.7175,509,250
2016-01-18 17:22:06,4.7175,4.725,4.7175,4.7175,326,250
2016-01-18 17:57:25,4.7175,4.7225,4.7125,4.7125,349,250
2016-01-18 18:33:55,4.7125,4.725,4.7125,4.725,293,250
2016-01-18 20:54:43,4.725,4.735,4.725,4.7275,272,250
2016-01-18 22:49:55,4.7275,4.73,4.72,4.7225,335,250
2016-01-19 00:14:32,4.7225,4.73,4.7225,4.73,430,250")))

library(tidyr)

dtTicksSum <- dtTicksSum %>%
    separate(dateTime, into=c("Time","Open","High","Low","Close","SumVol","SumTicks"), sep=",")

dtTicksSum$Time <- as.POSIXct(dtTicksSum$Time)
plot(Close ~ Time, data = dtTicksSum, type ="s") 

enter image description here

## using a factor 
dtTicksSum$fac_time <- as.factor(dtTicksSum$Time)
plot(Close ~ fac_time, data = dtTicksSum, type ="s") 

enter image description here

and using ggplot2

library(ggplot2)
ggplot(data=dtTicksSum, aes(x=fac_time, y=Close, group=1)) +
    geom_line() +
    theme_bw()

enter image description here

Reference: using groups in ggplot::geom_line()