如何改变X轴?

时间:2012-07-16 09:23:26

标签: r graph plot

我正在尝试使用5分钟的价格观察,在2011年12月至2012年6月的6个月期间用R图表覆盖黄金价格走势,这意味着我有5913个观测值  我尝试修改x轴,使标记在底部的日期如下:

  rt = dat$mid
    x11()
    plot(rt, type="l", col="blue", main="Gold Price", xlab="TBC", 
ylab="Price", ylim=range(rt), xaxt="n")

axis(1, at=1:7, lab=c("Dec '11", "Jan '12", 
    "Feb '12", "Mar '12", "Apr '12", "May '12", "June '12"))

但这根本不会返回任何轴。

默认轴范围为:0 1000 2000 3000 4000 5000 6000

我还尝试将at更改为at=1:6000,但这没有任何帮助。谁能告诉我如何实现这一目标?感谢

我应该补充一下,我已经广泛搜索了如何做到这一点的信息,但我似乎能够找到除此之外的所有轴。

根据要求提供的一些数据:

    Date    mid day returns
1   12/12/2011 00:00    1714.600    2011-12-12  NA
2   12/12/2011 00:05    1714.360    2011-12-12  -0.0139984135
3   12/12/2011 00:10    1714.050    2011-12-12  -0.0180841848
4   12/12/2011 00:15    1713.780    2011-12-12  -0.0157534067
5   12/12/2011 00:20    1712.650    2011-12-12  -0.0659578652
6   12/12/2011 00:25    1713.375    2011-12-12  0.0423231010
7   12/12/2011 00:30    1713.325    2011-12-12  -0.0029182596
8   12/12/2011 00:35    1713.935    2011-12-12  0.0355969495
9   12/12/2011 00:40    1714.950    2011-12-12  0.0592029205
10  12/12/2011 00:45    1714.675    2011-12-12  -0.0160367387
11  12/12/2011 00:50    1714.625    2011-12-12  -0.0029160470
12  12/12/2011 00:55    1714.530    2011-12-12  -0.0055407236
13  12/12/2011 01:00    1713.970    2011-12-12  -0.0326673475
14  12/12/2011 01:05    1710.730    2011-12-12  -0.1892137102
15  12/12/2011 01:10    1709.525    2011-12-12  -0.0704625854
16  12/12/2011 01:15    1708.865    2011-12-12  -0.0386146700
17  12/12/2011 01:20    1709.995    2011-12-12  0.0661039084
18  12/12/2011 01:25    1710.205    2011-12-12  0.0122799836
19  12/12/2011 01:30    1709.100    2011-12-12  -0.0646330198
20  12/12/2011 01:35    1709.050    2011-12-12  -0.0029255591
21  12/12/2011 01:40    1708.840    2011-12-12  -0.0122882831
22  12/12/2011 01:45    1709.250    2011-12-12  0.0239900062
23  12/12/2011 01:50    1709.240    2011-12-12  -0.0005850536
24  12/12/2011 01:55    1709.455    2011-12-12  0.0125778989
25  12/12/2011 02:00    1709.630    2011-12-12  0.0102366569
26  12/12/2011 02:05    1709.835    2011-12-12  0.0119901798
27  12/12/2011 02:10    1709.835    2011-12-12  0.0000000000
28  12/12/2011 02:15    1710.025    2011-12-12  0.0111115660
29  12/12/2011 02:20    1709.880    2011-12-12  -0.0084797677
30  12/12/2011 02:25    1709.540    2011-12-12  -0.0198864136
31  12/12/2011 02:30    1709.530    2011-12-12  -0.0005849544
32  12/12/2011 02:35    1709.660    2011-12-12  0.0076041402
33  12/12/2011 02:40    1709.735    2011-12-12  0.0043867409
34  12/12/2011 02:45    1710.465    2011-12-12  0.0426875628
35  12/12/2011 02:50    1709.465    2011-12-12  -0.0584807308
36  12/12/2011 02:55    1709.880    2011-12-12  0.0242736549
37  12/12/2011 03:00    1709.915    2011-12-12  0.0020469063
38  12/12/2011 03:05    1709.525    2011-12-12  -0.0228107527
39  12/12/2011 03:10    1709.415    2011-12-12  -0.0064347429
40  12/12/2011 03:15    1709.140    2011-12-12  -0.0160886691
41  12/12/2011 03:20    1708.450    2011-12-12  -0.0403793322
42  12/12/2011 03:25    1708.030    2011-12-12  -0.0245867093
43  12/12/2011 03:30    1692.650    2011-12-12  -0.9045311445
44  12/12/2011 03:35    1696.150    2011-12-12  0.2065628685
45  12/12/2011 03:40    1695.875    2011-12-12  -0.0162145032
46  12/12/2011 03:45    1699.075    2011-12-12  0.1885153506
47  12/12/2011 03:50    1698.605    2011-12-12  -0.0276659369
48  12/12/2011 03:55    1698.460    2011-12-12  -0.0085367810
49  12/12/2011 04:00    1700.215    2011-12-12  0.1032755504
50  12/12/2011 04:05    1697.800    2011-12-12  -0.1421418332
51  12/12/2011 04:10    1696.000    2011-12-12  -0.1060757952
52  12/12/2011 04:15    1694.940    2011-12-12  -0.0625195394
53  12/12/2011 04:20    1692.315    2011-12-12  -0.1549927907
54  12/12/2011 04:25    1694.240    2011-12-12  0.1136848631
55  12/12/2011 04:30    1696.505    2011-12-12  0.1335989782
56  12/12/2011 04:35    1694.550    2011-12-12  -0.1153033623
57  12/12/2011 04:40    1693.495    2011-12-12  -0.0622778053
58  12/12/2011 04:45    1693.550    2011-12-12  0.0032476687
59  12/12/2011 04:50    1693.020    2011-12-12  -0.0313001063
60  12/12/2011 04:55    1693.225    2011-12-12  0.0121078067
61  12/12/2011 05:00    1692.800    2011-12-12  -0.0251031816
62  12/12/2011 05:05    1692.850    2011-12-12  0.0029536426
63  12/12/2011 05:10    1692.775    2011-12-12  -0.0044304966
64  12/12/2011 05:15    1692.500    2011-12-12  -0.0162468337
65  12/12/2011 05:20    1693.585    2011-12-12  0.0640858122
66  12/12/2011 05:25    1693.790    2011-12-12  0.0121037677
67  12/12/2011 05:30    1693.165    2011-12-12  -0.0369063071
68  12/12/2011 05:35    1692.640    2011-12-12  -0.0310118276
69  12/12/2011 05:40    1691.665    2011-12-12  -0.0576189219
70  12/12/2011 05:45    1691.165    2011-12-12  -0.0295610482
71  12/12/2011 05:50    1689.865    2011-12-12  -0.0768996468
72  12/12/2011 05:55    1691.065    2011-12-12  0.0709863884
73  12/12/2011 06:00    1692.140    2011-12-12  0.0635492107
74  12/12/2011 06:05    1692.180    2011-12-12  0.0023638427
75  12/12/2011 06:10    1691.795    2011-12-12  -0.0227543053
76  12/12/2011 06:15    1691.860    2011-12-12  0.0038419993
77  12/12/2011 06:20    1691.790    2011-12-12  -0.0041375438
78  12/12/2011 06:25    1690.965    2011-12-12  -0.0487768116
79  12/12/2011 06:30    1689.850    2011-12-12  -0.0659604291
80  12/12/2011 06:35    1690.205    2011-12-12  0.0210055754
81  12/12/2011 06:40    1691.025    2011-12-12  0.0485030605
82  12/12/2011 06:45    1690.900    2011-12-12  -0.0073922396
83  12/12/2011 06:50    1692.000    2011-12-12  0.0650329622
84  12/12/2011 06:55    1690.885    2011-12-12  -0.0659200677
85  12/12/2011 07:00    1691.195    2011-12-12  0.0183319141
86  12/12/2011 07:05    1690.470    2011-12-12  -0.0428782869
87  12/12/2011 07:10    1690.075    2011-12-12  -0.0233690131
88  12/12/2011 07:15    1690.525    2011-12-12  0.0266224932
89  12/12/2011 07:20    1690.525    2011-12-12  0.0000000000
90  12/12/2011 07:25    1690.325    2011-12-12  -0.0118313442
91  12/12/2011 07:30    1690.890    2011-12-12  0.0334199396
92  12/12/2011 07:35    1692.000    2011-12-12  0.0656243650
93  12/12/2011 07:40    1691.150    2011-12-12  -0.0502490293
94  12/12/2011 07:45    1690.765    2011-12-12  -0.0227681655
95  12/12/2011 07:50    1686.855    2011-12-12  -0.2315240759
96  12/12/2011 07:55    1686.200    2011-12-12  -0.0388371970
97  12/12/2011 08:00    1687.240    2011-12-12  0.0616581313
98  12/12/2011 08:05    1687.505    2011-12-12  0.0157048903
99  12/12/2011 08:10    1688.815    2011-12-12  0.0775992836
100 12/12/2011 08:15    1688.675    2011-12-12  -0.0082901800

2 个答案:

答案 0 :(得分:3)

这是我评论的扩展。我会制作一些假数据并绘制它:

> t = seq(as.Date("2011-11-1"),as.Date("2012-06-1"),by=as.difftime("0:15:00"))
> length(t)
[1] 20449
> v = rnorm(length(t))
> plot(t,v)
> plot(t,v,type="l")

我的x轴很好地标有日期。我每隔15分钟得到20,449个......

从你的数据文件(我不得不添加一些逗号,因为我认为标签分隔符丢失了,你有行号):

> gold=read.csv("gold.csv",sep=",",as.is=TRUE)
> gold$Tim = strptime(gold$Date, "%d/%m/%Y %H:%M")
> head(gold)
  row                Date      mid           day         returns
1   1  12/12/2011 00:00   1714.600    2011-12-12              NA
2   2  12/12/2011 00:05   1714.360    2011-12-12   -0.0139984135
3   3  12/12/2011 00:10   1714.050    2011-12-12   -0.0180841848
                       Tim
 1 2011-12-12 00:00:00
 2 2011-12-12 00:05:00
 3 2011-12-12 00:10:00

现在摘要(黄金)告诉我“回归”是一个角色,因为在开始时这个恼人的NA。我们来解决这个问题:

> gold$returns=as.numeric(gold$returns)

然后简单地说:

> plot(gold$Tim,gold$returns,type="l")

您可能想要调查时间序列数据的包,例如zoo或xts,或者读取从R网页链接的时间序列或财务任务视图......

答案 1 :(得分:1)

实际上我猜你得到的是一个x轴:它只是太短而无法读取(即只标记前七个点)。

当您更改为1:6000时,您应该收到类似的错误消息 “轴中的错误(...):'at'和'labels'长度不同5913!= 7”

尝试类似轴(blahblah, at = floor(seq(from = 1, to = 5913, length.out = 7)) ,blahblah)

如果重要,你应该调整seq函数中的参数,以确保刻度确实在你期望的位置。