0

我试图在 9 个不同的面板中绘制 9 个站(A 到 I)的时间序列月平均温度数据。但我正在使用动物园来保持月年格式。我的简单数据集如下所示。

+--------+------+------+------+------+
| 时间 | 一个 | 乙| C | D |
+--------+------+------+------+------+
| 84 年 1 月 | 28.2 | 28.2 | 27.5 | 18.4 |
| 84 年 2 月 | 29.5 | 26.6 | 27.9 | 17.4 |
| 84 年 3 月 | 30.7 | 30.3 | 30.1 | 19.5 |
| 84 年 4 月 | 30.5 | 33.2 | 29.9 | 20.7 |
| 84 年 5 月 | 33.2 | 30.1 | 30.2 | 21 |
| 84 年 6 月 | 31.6 | 28.3 | 28.5 | 16.9 |
| 84 年 7 月 | 31.5 | 28.6 | 27.7 | 16.8 |
| 84 年 8 月 | 32.5 | 28.9 | 27.4 | 18.5 |
| 84 年 9 月 | 34 | 28.1 | 29.4 | 18.3 |
| 84 年 10 月 | 32.8 | 28.8 | 28.8 | 17.2 |
| 84 年 11 月 | 28.9 | 31.6 | 29 | 17.8 |
| 84 年 12 月 | 30.6 | 26.9 | 28.1 | 18.9 |
| 85 年 1 月 | 31.8 | 28.6 | 29.3 | 18.2 |
| 85 年 2 月 | 31.3 | 29.6 | 30.4 | 19.5 |
| 85 年 3 月 | 32 | 31.1 | 31.4 | 19.7 |
+--------+------+------+------+------+

该数据集可以通过以下代码访问。

structure(list(Time = structure(c(6L, 4L, 10L, 1L, 12L, 9L, 8L, 
2L, 15L, 14L, 13L, 3L, 7L, 5L, 11L), .Label = c("Apr-84", "Aug-84", 
"Dec-84", "Feb-84", "Feb-85", "Jan-84", "Jan-85", "Jul-84", "Jun-84", 
"Mar-84", "Mar-85", "May-84", "Nov-84", "Oct-84", "Sep-84"), class =     "factor"), 
A = c(28.2, 29.5, 30.7, 30.5, 33.2, 31.6, 31.5, 32.5, 34, 
32.8, 28.9, 30.6, 31.8, 31.3, 32), B = c(28.2, 26.6, 30.3, 
33.2, 30.1, 28.3, 28.6, 28.9, 28.1, 28.8, 31.6, 26.9, 28.6, 
29.6, 31.1), C = c(27.5, 27.9, 30.1, 29.9, 30.2, 28.5, 27.7, 
27.4, 29.4, 28.8, 29, 28.1, 29.3, 30.4, 31.4), D = c(18.4, 
17.4, 19.5, 20.7, 21, 16.9, 16.8, 18.5, 18.3, 17.2, 17.8, 
18.9, 18.2, 19.5, 19.7)), .Names = c("Time", "A", "B", "C", 
"D"), class = "data.frame", row.names = c(NA, -15L))

我使用以下代码将数据获取到动物园结构,然后进行绘图。图书馆(动物园)

dat$Time <- as.yearmon(dat$Time,format="%b-%y")

然后是下面的绘图代码。

plot(dat, col=Rainbow)

我需要在 X 轴上标注年份,在 Y 轴上标注温度值。我找到了一些方法来绘制它,但是当系列位于单独的 ts 对象中并在组合后绘制时,这些示例可用。但是数据在数据框中作为变量可用。我是 R 新手,如果有人可以帮助我,我将不胜感激。谢谢

4

3 回答 3

1
library(lubridate)
dat$Time <- as.POSIXct(dmy(paste("01-", dat$Time , sep ="")))

library(foqat)
geom_ts_batch(dat)

在此处输入图像描述

于 2022-02-02T14:13:05.633 回答
1

I think you'll get better results if you make a proper zoo object. For example, using your original dat

zz <- zoo(dat[-1], as.yearmon(dat$Time,format="%b-%y"))
plot(zz)
于 2016-03-10T05:19:18.463 回答
0

使用完整的数据集,您可以传递 X = dat[2:9]。对于 9 个图,制作 par(mfrow=c(9,1))。

par(mar=c(1,1,1,1))
par(mfrow=c(4,1))
apply(X = dat[2:5],MARGIN = 2,FUN = plot,x=dat$Time,type="l",col="blue",xlab="Years",ylab="temperature")
于 2016-03-10T04:06:39.877 回答