r - 在 r 中使用 strptime 选择特定日期

标签 r time-series strptime

我有一个带有标题的文本文件数据集

YEAR MONTH DAY value

从 1/6/2010 到 14/7/2012 每小时运行一次。我使用以下命令打开并绘制数据:

data=read.table('example.txt',header=T)
time = strptime(paste(data$DAY,data$MONTH,data$YEAR,sep="-"), format="%d-%m-%Y")
plot(time,data$value)

但是,绘制数据时,x 轴仅显示 2011 年和 2012 年。enter image description here 。我怎样才能保留 2011 年和 2012 年的标签,同时添加一些特定的月份,例如如果我想要三月、六月和九月?

我已在此链接上提供数据

https://dl.dropbox.com/u/107215263/example.txt

最佳答案

您需要使用函数axis.POSIXct来按照您的意愿格式化和处理日期标签:

plot(time,data$value,xaxt="n") #Skip the x-axis here
axis.POSIXct(1, at=pretty(time), format="%B %Y")

enter image description here

要查看所有可能的格式,请参阅?strptime
您当然可以使用参数 at 将刻度线放置在您想要的任何位置,例如:

axis.POSIXct(1, at=seq(time[1],time[length(time)],"3 months"), 
             format="%B %Y")

enter image description here

关于r - 在 r 中使用 strptime 选择特定日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15781980/

相关文章:

r - 日期时间转换并仅提取时间

r - 沿日期刻度线放置网格

r - 沿 geom_segment() 的 ggplot2 颜色渐变

r - 在 R 中仅将图例标题的一部分加粗

r - mutate_impl(.data, dots) 中的错误 : Evaluation error: Only year, Date 类索引允许季度、月、周和日期间

python - Python 中用户定义的 croston 函数

r - 转换为日期和剥离时间?

r - 在图例下方添加其他文本(R + ggplot)

r - 将函数应用于因子(参与者)的每个级别,以根据 R 中标准差中的均值距离去除异常值

mysql - 每小时获取按日期时间分组的最高值记录