更改x轴上的数据缩放

时间:2014-02-09 12:53:28

标签: r statistics

我正在绘制我的数据:

(dput(sale))
structure(c(-0.049668136, 0.023675638, -0.032249731, -0.071487224, 
-0.034017265, -0.031278933, -0.052070721, -0.034305542, -0.019041209, 
-0.050459175, -0.017315808, -0.012787003, -0.03341208, -0.045078144, 
-0.036638132, -0.036533367, -0.012683656, -0.014388251, -0.006775188, 
-0.037153807, -0.008941402, -0.011760677, -0.005077979, -0.041187417, 
-0.001966554, -0.028822067, 0.021828558, 0.016208791, -0.026897492, 
-0.032107207, -0.008496522, -0.028027096, -0.013746662, -0.004545603, 
-0.005679941, -0.004614187, 0.004083014, -0.012624954, -0.016362079, 
-0.006350167, -0.019551277), na.action = structure(42:45, class = "omit"))
 [1] -0.049668136  0.023675638 -0.032249731 -0.071487224 -0.034017265
 [6] -0.031278933 -0.052070721 -0.034305542 -0.019041209 -0.050459175
[11] -0.017315808 -0.012787003 -0.033412080 -0.045078144 -0.036638132
[16] -0.036533367 -0.012683656 -0.014388251 -0.006775188 -0.037153807
[21] -0.008941402 -0.011760677 -0.005077979 -0.041187417 -0.001966554
[26] -0.028822067  0.021828558  0.016208791 -0.026897492 -0.032107207
[31] -0.008496522 -0.028027096 -0.013746662 -0.004545603 -0.005679941
[36] -0.004614187  0.004083014 -0.012624954 -0.016362079 -0.006350167
[41] -0.019551277
attr(,"na.action")
[1] 42 43 44 45
attr(,"class")
[1] "omit"
(dput(purchase))
structure(c(0.042141187, 0.075875128, 0.090953485, 0.050951625, 
0.082566915, 0.184396833, 0.136625887, 0.042725409, 0.135028692, 
0.13201904, 0.093634104, 0.16776844, 0.13645719, 0.201365036, 
0.227589832, 0.236473792, 0.269064385, 0.200981722, 0.144739536, 
0.145256493, 0.040205545, 0.031577107, 0.014767345, 0.005843065, 
0.034805051, 0.082493053, 0.010572227, 0.000645763, 0.033368236, 
0.024326153, 0.038601182, 0.025446045, 0.000556418, 0.017201608, 
0.008316872, 0.059722053, 0.059695415, 0.076940829, 0.067650014, 
0.002029566, 0.008466334), na.action = structure(42:45, class = "omit"))
 [1] 0.042141187 0.075875128 0.090953485 0.050951625 0.082566915 0.184396833
 [7] 0.136625887 0.042725409 0.135028692 0.132019040 0.093634104 0.167768440
[13] 0.136457190 0.201365036 0.227589832 0.236473792 0.269064385 0.200981722
[19] 0.144739536 0.145256493 0.040205545 0.031577107 0.014767345 0.005843065
[25] 0.034805051 0.082493053 0.010572227 0.000645763 0.033368236 0.024326153
[31] 0.038601182 0.025446045 0.000556418 0.017201608 0.008316872 0.059722053
[37] 0.059695415 0.076940829 0.067650014 0.002029566 0.008466334
attr(,"na.action")
[1] 42 43 44 45
attr(,"class")
[1] "omit"

timeLine <- c(-20 , +20)
plot(sale,type="b", xlim=timeLine, ylim=c(-.1,.4) )
lines( purchase, type="b")

abline(v=0, col="black")

我得到的情节看起来像那样:

enter image description here

情节的错误是缩放。我的图表应从-20开始,应该到+20,而-20, -19, -18, ..., +19, +20这样的每个数据点都是图中的一个点。在我导出的csv表中,我有一行包含这些值。我的问题是,如何从-20开始,以便每个数据点都是+20的整数?是否也可以显示从-20+20的所有整数?

我非常感谢你的回答!

更新

轴的缩放:

enter image description here

1 个答案:

答案 0 :(得分:1)

By,默认情况下,当x中未指定plot时,会根据索引(从1开始)绘制值。您必须为x轴创建一个矢量。

timeLine <- c(-20 , 20)

# this command generates a sequence from -20 to 20
timeSeq <- Reduce(seq, timeLine)

# now, this sequence is passed to `x`
plot(sale, x = timeSeq, type = "b", xlim = timeLine, ylim = c(-.1, .4) )
lines(purchase, x = timeSeq, type = "b")

abline(v = 0, col = "black")

enter image description here


更新:如何显示所有x轴标签?

如果减小尺寸(cex.axis)并增加绘图宽度,则可以显示所有x轴标签。这是一个例子。

png("plot.png", width = 1000)
plot(sale,type="b", x = timeSeq, xlim=timeLine, ylim=c(-.1,.4),
     xaxt = "n")
lines( purchase, type="b", x = timeSeq)
abline(v=0, col="black")
axis(side = 1, at = timeSeq, cex.axis = 0.75)
dev.off()

enter image description here