r - How can I plot an irregular time series so the x axis points are equidistant? -
i have irregular time series. each row represents x number of ticks, (datetime, price, volume) aggregated show hloc, using first tick of each group set datetime 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 data goes on weekends , includes night trading activity low, records unevenly spaced time-wise. want include date/time information general reference on chart, maintain evenly distributed bars in second plot below.
instead of this:
i want this, date/times instead of row numbers:
is there way overlay x-axis in second chart date/time column (or example, every 10th entry) vertically?
i tried suggestion convert time factor, resulting plot not connect points plot(close ~ as.factor(time), data = dttickssum, type ="s")
just expanding on @senoro 's answer, , using 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")
## using factor dttickssum$fac_time <- as.factor(dttickssum$time) plot(close ~ fac_time, data = dttickssum, type ="s")
and using ggplot2
library(ggplot2) ggplot(data=dttickssum, aes(x=fac_time, y=close, group=1)) + geom_line() + theme_bw()
reference: using groups in ggplot::geom_line()
Comments
Post a Comment