数据文件的日期格式为1975M1,1975M2,... 2011M12,用于时间序列数据.当使用R绘制此数据时,我希望x轴显示刻度轴上的月份.
对于要正确阅读的日期,我尝试将M替换为 - 获得%Y-%m格式,但对于来自hydroTSM软件包的drawTimeAxis来说似乎不太好,这可能需要%Y-%M-%d格式.它给出了刻度维度的维数不正确的错误.
另一种解析和格式化数据的方法x$newdate <- strptime(as.character(x$date), "%Y-%m"),然后format(x$newdate,""%Y-%m")
也不会读取日期并给出错误...所有NA.
date < - as.Date(data [,1]字符串不是标准明确格式的错误,而ts < - read.zoo(xts,as.yearmon(x [,1]))给出了数据不良的错误行.
请提供有关如何使用日期信息读取此数据的解决方案.
数据文件的一小部分
date x x2
1975M1 112.44 113.12
1975M2 113.1 114.36
1975M3 115.04 114.81
1975M4 117.65 115.35
1975M5 119.5 116.92
1975M6 121.4 118.56
1975M7 120.64 118.97
1975M8 119.12 119.84
1975M9 118.91 120.59
1975M10 120.58 122.3
1975M11 121.26 123.35
1975M12 122.34 123.33
Run Code Online (Sandbox Code Playgroud)
更新:到目前为止的答案通过在xts包中使用%YM%m或添加获取标准格式的日期来解决正确读取日期的问题.定制刻度轴仍然是个问题.drawTimeAxis给出了尺寸误差,并且绘图命令没有显示超过一年的数据或其他数据的月度标签.任何定制刻度轴的方法?
A5C*_*2T1 18
也许你没有as.yearmon()正确使用,因为以下适用于我(使用datGavin的答案):
library(zoo)
dat$date <- as.yearmon(dat$date, "%YM%m")
Run Code Online (Sandbox Code Playgroud)
因此,努力使事情正确地绘制:
你的数据:
dat <- read.table(text = "date x x2
1975M1 112.44 113.12
1975M2 113.1 114.36
1975M3 115.04 114.81
1975M4 117.65 115.35
1975M5 119.5 116.92
1975M6 121.4 118.56
1975M7 120.64 118.97
1975M8 119.12 119.84
1975M9 118.91 120.59
1975M10 120.58 122.3
1975M11 121.26 123.35
1975M12 122.34 123.33", header = TRUE)
Run Code Online (Sandbox Code Playgroud)转换为xts使用as.yearmon()"zoo"包.
library(xts) # Will also load zoo
dat.xts <- xts(dat[-1],
order.by = as.yearmon(dat$date, "%YM%m"))
dat.xts
# x x2
# Jan 1975 112.44 113.12
# Feb 1975 113.10 114.36
# Mar 1975 115.04 114.81
# Apr 1975 117.65 115.35
# May 1975 119.50 116.92
# Jun 1975 121.40 118.56
# Jul 1975 120.64 118.97
# Aug 1975 119.12 119.84
# Sep 1975 118.91 120.59
# Oct 1975 120.58 122.30
# Nov 1975 121.26 123.35
# Dec 1975 122.34 123.33
Run Code Online (Sandbox Code Playgroud)绘制数据:
plot.zoo(dat.xts)
Run Code Online (Sandbox Code Playgroud)

plot.zoo(dat.xts,
plot.type="single",
col = c("red", "blue"))
Run Code Online (Sandbox Code Playgroud)

下面是一些可以使用的示例数据(在SO上提问时,通常很好地分享这些样本数据,因为这样可以让其他人更容易复制和解决您的问题).请注意,对于此示例,我们已跳过使用"xts"包,因为它不是必需的.
set.seed(1)
dat <- data.frame(date = paste0(rep(1975:1977, each = 12),
"M", rep(1:12, times = 3)),
x1 = runif(36, min = 100, max = 140),
x2 = runif(36, min = 100, max = 140))
library(zoo) # xts is actually unnecessary if this is all you're doing
# Convert your data to a `zoo` object
dat.z <- zoo(dat[-1], order.by = as.yearmon(dat$date, "%YM%m"))
Run Code Online (Sandbox Code Playgroud)
这是通过以下方式获得的默认图plot(dat.z, screen = 1, col = 1:2):

根据您的评论,听起来您想要每月标签.
绘制数据,但用x抑制x轴 xaxt = "n"
plot(dat.z, screen = 1, col = 1:2, xaxt = "n")
Run Code Online (Sandbox Code Playgroud)做一些设置工作,每个月都有一个标签.(参见?plot.zoo,修改地点.)
tt <- time(dat.z)
# The following is just the sequence 1:36.
# If you wanted only every third month plotted,
# use a sequence like ix <- seq(1, length(tt), 3)
ix <- seq_along(tt)
# What format do you want for your labels.
# This yields abbreviated month - abbreviated year
fmt <- "%b-%y"
labs <- format(tt, fmt) # Generate the vector of your labels
Run Code Online (Sandbox Code Playgroud)将轴添加到绘图中.可能需要一些实验来找到适合所有东西的正确尺寸.las = 2使标签垂直于轴,如果您确实需要为每年的每个月添加标签,则需要这样做.
axis(side = 1, at = tt[ix], labels = labs[ix],
tcl = -0.7, cex.axis = 0.7, las = 2)
Run Code Online (Sandbox Code Playgroud)这是最后的情节:

顺便说一句,如果你得到类似日期1977.15等等,你可能想要阅读这个问题的一些答案,例如,看看@joran的使用pretty().
| 归档时间: |
|
| 查看次数: |
17359 次 |
| 最近记录: |