Plot - 无效的xlim值

时间:2014-02-08 15:36:12

标签: r plot statistics

我想在一个简单的线条图中绘制两个矢量,然后想在0 x轴点添加一条线。

这就是我尝试实现它的方式:

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"))

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"))


timeLine <- c(-20:+20)
plot(sale,type="b", xlim=timeLine)

Error in plot.window(...) : invalid 'xlim' value
par(new=T)

plot(purchase,type="b", xlim=timeLine)

Error in plot.window(...) : invalid 'xlim' value
par(new=F)

但是我得到Error in plot.window(...) : invalid 'xlim' value。为什么我的timeLine错误实现?

我非常感谢你的回答!

1 个答案:

答案 0 :(得分:9)

提供给'xlim'的向量只需要有两个元素c(max,min)。试试这个:

timeLine <- c(-20 , +20)
plot(sale, type="b", xlim=timeLine)

然后绘制第二个项目你应该使用行(你不应该尝试重新指定xlim:

lines( purchase, type="b")

除非您发现ylimits未设置且'purchase'中的值范围与'sale'的值不重叠,因此您需要在hte中设置ylimits首先绘制cal:

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

这一切都可以通过以下方式更简单地处理:

matplot( cbind( sale, purchase), type="b")