2016-01-21 60 views
1

我有一个不规则的时间序列。每一行代表x数量的时钟(日期时间,价格,音量)聚合显示HLOC,使用每组的第一个打勾设置该行的日期时间。如何绘制一个不规则的时间序列,这样x轴点是等距的?

"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 
... 

由于该数据会在周末进行,并且包括活动较少的夜间交易,因此记录的时间间隔非常不均匀。我想在图表中包含一般参考的日期/时间信息,但保持均匀分布的条形,如下面的第二个图所示。

取而代之的是:

enter image description here

我希望它看起来是这样,但随着日期/时间,而不是行号:

enter image description here

是否有某种方式来覆盖第二个图表中的x轴与日期/时间列(或例如每10个条目)垂直?

我试过的建议,时间转换成一个因素,但由此产生的阴谋没有连接点 图(关闭〜as.factor(时间),数据= dtTicksSum,类型=“S”) enter image description here

+3

尝试的日期/时间转换为一个因素? – tospig

回答

1

就扩大对@SenorO的回答,并使用通过您所提供

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

和数据ggplot2

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

enter image description here

参考:using groups in ggplot::geom_line()

+0

我在使用ggplot2编码时得到一个线图(组= 1)。没有运气的一个使用情节。也许它与Win10x64或RStudio有关。 –

+0

@DougEdmunds也许 - 奇怪的是,它会有不同的表现... – tospig

1

正如所指出的评价tospig,这个作品(测试以验证):

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

as.factor将匹配时间顺序创建整数代码。

+0

当我试图说我没有得到连接点,只是分离点。当我尝试使用ggplot ggplot(dtTicksSum,aes(as.factor(Time),Close))+ geom_line() 它没有绘制图形,而是给出了一条消息: geom_path:每个组只包含一个观察值。你需要调整团体审美吗? –

+1

@DougEdmunds你在你的问题中没有说你想使用'ggplot2'。答案会有所不同。 – 2016-01-21 01:39:54

+0

我尝试了剧情和ggplot;既不连接点。我在问题的最后添加了一个分解时间图。 –

相关问题