我有每小时采样的数据,并相应地绘制数据;
x<-c("2016-05-09 09:00:00", "2016-05-09 10:00:00", "2016-05-09 11:00:00", "2016-05-10 12:00:00")
y<- c(2,NA,3,5)
df<-data.frame(x,y)
plot(df$x,df$y)
在x轴上,我只想将时间戳显示为年 - 月 - 日。另外,我只希望每个日期显示一个标签(即使我在同一日期有多个测量值)。有没有办法做到这一点?我更喜欢基本功能
亲切的问候,
答案 0 :(得分:1)
我想出了一个解决方案。也许它不是最好的,但它在这里:
我压制了x轴par(xaxt ='l)的绘图,然后我使用axis()创建了一个新的xaxis。我将从1开始的滴答测量到测量的总小时数(注意这是连续数据)24(每天的小时数),然后我使用labels = c添加了我自己的标签(“2016-05-09 “,..”在轴()中。
请注意,此解决方案有效,因为我只有大约10天的数据显示。我想这种方法不适合长时间系列。
#Hourly data
x<-c("2016-05-09 00:00:00","2016-05-09 01:00:00", "2016-05-09 02:00:00", "2016-05-09 03:00:00", "2016-05-09 04:00:00", "2016-05-09 05:00:00","2016-05-09 06:00:00","2016-05-09 07:00:00","2016-05-09 08:00:00","2016-05-09 09:00:00","2016-05-09 10:00:00","2016-05-09 11:00:00","2016-05-09 12:00:00","2016-05-09 13:00:00","2016-05-09 14:00:00","2016-05-09 15:00:00","2016-05-09 16:00:00","2016-05-09 17:00:00","2016-05-09 18:00:00","2016-05-09 19:00:00","2016-05-09 20:00:00","2016-05-09 21:00:00","2016-05-09 22:00:00","2016-05-09 23:00:00","2016-05-10 00:00:00")
y<-seq(1,length(x),by=1)
df<-data.frame(x,y)
#Suppress x-axis
plot(df$x,df$y,xaxt='n')
#Create new x-axis with ticks in 24 hour interval
axis(side=1,at=seq(1,length(x),by=24),labels=c("2016-05-09","2016-05-10"))
答案 1 :(得分:0)