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

时间:2015-12-10 01:41:35

标签: r time plot

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 enter image description here

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.

2 个答案:

答案 0 :(得分:2)

使用xaxt="n"绘制没有x轴标签的图表,然后使用axis将其添加数小时和分钟。我正在使用pretty来获取小时的开头。

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

enter image description here

答案 1 :(得分:1)

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):

enter image description here