Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
添加一个天数和小时的向量以在R中绘图_R_Datetime_Plot_Posixlt - Fatal编程技术网

添加一个天数和小时的向量以在R中绘图

添加一个天数和小时的向量以在R中绘图,r,datetime,plot,posixlt,R,Datetime,Plot,Posixlt,我想添加简明的日期和时间信息到我的绘图在R 我将这个图添加到一篇研究论文中,当它缩小到适合模板时,它会丢失一些信息 我的实际日期时间范围为2017年7月20日18:15-2017年7月23日21:15 我想把日期缩写为天,比如星期四18:15和星期日21:15,中间有5天和时间 我可以用POSIXLT格式创建正确的范围,但它对于我的需要来说太大了 my.date <- seq(as.POSIXlt(strptime('20/07/2017 18:15',"%d/%m/%Y %H:%M"),

我想添加简明的日期和时间信息到我的绘图在R

我将这个图添加到一篇研究论文中,当它缩小到适合模板时,它会丢失一些信息

我的实际日期时间范围为2017年7月20日18:15-2017年7月23日21:15

我想把日期缩写为天,比如星期四18:15和星期日21:15,中间有5天和时间

我可以用POSIXLT格式创建正确的范围,但它对于我的需要来说太大了

my.date <- seq(as.POSIXlt(strptime('20/07/2017 18:15',"%d/%m/%Y %H:%M"),tz="GMT"), as.POSIXlt(strptime('23/07/2017 21:15',"%d/%m/%Y %H:%M"),tz="GMT"),length.out = 7)

my.date解决此问题的关键是将POSIX对象转换为所需格式的字符串。此处使用格式函数:
format(my.date,“%a%H:%M”)

下面是一个简单的例子:

my.date <- seq(strptime('20/07/2017 18:15',"%d/%m/%Y %H:%M"), 
               strptime('23/07/2017 21:15',"%d/%m/%Y %H:%M"), length.out = 7)

#x axis labels in the desired format
labels<-format(my.date, "%a %H:%M")

#simple example with base graphics
y<-2:8
plot(my.date,y, axes=FALSE)
#draw x and y axis
axis(1, at=my.date, labels=labels)
axis(2, at=y)

my.date使用
format(my.date,“%a%H:%M”)
将标签设置为所需的正式格式。@Dave2e,感谢您的快速回复,这非常有效,很乐意接受作为答案。