R:在图中看不到线条

时间:2015-04-19 11:31:10

标签: r plot

我有一个名为anomaly的向量,它给出了输出:

 anomaly
 [1] -0.11 -0.13 -0.01 -0.04 -0.42 -0.23 -0.25 -0.45 -0.23  0.04 -0.22 -0.55
[13] -0.40 -0.39 -0.32 -0.32 -0.27 -0.15 -0.21 -0.25 -0.05 -0.05 -0.30 -0.35
[25] -0.42 -0.25 -0.15 -0.41 -0.30 -0.31 -0.21 -0.25 -0.33 -0.28 -0.02  0.06
[37] -0.20 -0.46 -0.33 -0.09 -0.15 -0.04 -0.09 -0.16 -0.11 -0.15  0.04 -0.05
[49]  0.01 -0.22 -0.03  0.03  0.04 -0.11  0.05 -0.08  0.01  0.12  0.15 -0.02
[61]  0.14  0.11  0.10  0.06  0.10 -0.01  0.01  0.12 -0.03 -0.09 -0.17 -0.02
[73]  0.03  0.12 -0.09 -0.09 -0.18  0.08  0.10  0.05 -0.02  0.10  0.05  0.03
[85] -0.25 -0.15 -0.07 -0.02 -0.09  0.00  0.04 -0.10 -0.05  0.18 -0.06 -0.02
[97] -0.21  0.16  0.07  0.13  0.27  0.40  0.10  0.34  0.16  0.13  0.19  0.35
[109]  0.42  0.28  0.49  0.44  0.16  0.18  0.31  0.47  0.36  0.40  0.71  0.43
[121]  0.41  0.56  0.70  0.66  0.60

还有一个名为MA的向量,它给出了输出:

  MA
  [1] -8.333333e-02 -6.000000e-02 -1.566667e-01 -2.300000e-01 -3.000000e-01
  [6] -3.100000e-01 -3.100000e-01 -2.133333e-01 -1.366667e-01 -2.433333e-01
 [11] -3.900000e-01 -4.466667e-01 -3.700000e-01 -3.433333e-01 -3.033333e-01
 [16] -2.466667e-01 -2.100000e-01 -2.033333e-01 -1.700000e-01 -1.166667e-01
 [21] -1.333333e-01 -2.333333e-01 -3.566667e-01 -3.400000e-01 -2.733333e-01
 [26] -2.700000e-01 -2.866667e-01 -3.400000e-01 -2.733333e-01 -2.566667e-01
 [31] -2.633333e-01 -2.866667e-01 -2.100000e-01 -8.000000e-02 -5.333333e-02
 [36] -2.000000e-01 -3.300000e-01 -2.933333e-01 -1.900000e-01 -9.333333e-02
 [41] -9.333333e-02 -9.666667e-02 -1.200000e-01 -1.400000e-01 -7.333333e-02
 [46] -5.333333e-02 -5.782412e-19 -8.666667e-02 -8.000000e-02 -7.333333e-02
 [51]  1.333333e-02 -1.333333e-02 -6.666667e-03 -4.666667e-02 -6.666667e-03
 [56]  1.666667e-02  9.333333e-02  8.333333e-02  9.000000e-02  7.666667e-02
 [61]  1.166667e-01  9.000000e-02  8.666667e-02  5.000000e-02  3.333333e-02
 [66]  4.000000e-02  3.333333e-02  0.000000e+00 -9.666667e-02 -9.333333e-02
 [71] -5.333333e-02  4.333333e-02  2.000000e-02 -2.000000e-02 -1.200000e-01
 [76] -6.333333e-02  4.625929e-18  7.666667e-02  4.333333e-02  4.333333e-02
 [81]  4.333333e-02  6.000000e-02 -5.666667e-02 -1.233333e-01 -1.566667e-01
 [86] -8.000000e-02 -6.000000e-02 -3.666667e-02 -1.666667e-02 -2.000000e-02
 [91] -3.666667e-02  1.000000e-02  2.333333e-02  3.333333e-02 -9.666667e-02
 [96] -2.333333e-02  6.666667e-03  1.200000e-01  1.566667e-01  2.666667e-01
 [101]  2.566667e-01  2.800000e-01  2.000000e-01  2.100000e-01  1.600000e-01
 [106]  2.233333e-01  3.200000e-01  3.500000e-01  3.966667e-01  4.033333e-01
 [111]  3.633333e-01  2.600000e-01  2.166667e-01  3.200000e-01  3.800000e-01
 [116]  4.100000e-01  4.900000e-01  5.133333e-01  5.166667e-01  4.666667e-01
 [121]  5.566667e-01  6.400000e-01  6.533333e-01




  summary(MA)
     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
  -0.44670 -0.16330 -0.03667 -0.01051  0.08500  0.65330 

 summary(anomaly)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
-0.55000 -0.18000 -0.02000 -0.00632  0.12000  0.71000 

还有一个向量year从1880年到2004年取值。

我的目的是在同一个数字中绘制anomalyMA。所以我写下了代码:

 plot(anomaly,col="red",xlim=c(1880,2004),ylim=c(-.6,.75),xlab="year",ylab="anomaly",main="Anomaly , 5-year Moving Average")

lines(anomaly,col="red")
lines(MA,col="green")

text(1978,.60,"Original data",col="red")
text(1978,.50,"5-year Moving Average",col="green")

但图表并没有产生线条。

enter image description here

为什么线条不可见?

1 个答案:

答案 0 :(得分:1)

我认为您已经有了在yearplot来电中加入lines的答案。为了使数据保持相同的长度,我会尝试几种方法。我首选的方法是使用data.frame,因为看起来您的数据肯定与year相关联:

dat <- data.frame(year, anomaly, MA)

下面绘制它的代码依赖于此,但如果你想继续使用单个向量,那么你可以使用这样的代码继续你自己的代码(如果你的数据长度会发生变化,那么你可以继续使用想要检查anomaly的长度。

if (length(MA) != length(year))
    MA <- c(MA, rep(NA, length(year) - length(MA)))

要绘制的代码。我根据我的编程OCD做了一些改动: - )

  • 避免在我的代码中使用“魔术常量”;想象一下,如果你想在某个时刻改变异常的颜色,你需要在两个位置将它改为相同的颜色。这是次要的,但当你的情节变得更复杂时,这可以节省生命。
  • 使用plotlines的公式(利用data.frame)。这并不一定能提高代码的可读性(在哪个轴上仍然很明显),但我认为阅读(MA ~ year, data=dat, ...)(dat$year, dat$MA, ...)稍微容易一些。很轻微。

代码:

colAnomaly <- 'red'
colMovAvg <- 'green'
plot(anomaly ~ year, data=dat, type='l', col=colAnomaly,
     xlim=c(1880,2004), ylim=c(-.6,.75),
     xlab="year", ylab="anomaly", main="Anomaly , 5-year Moving Average")
lines(MA ~ year, data=dat, col=colMovAvg)
text(1978, 0.60, "Original data", col=colAnomaly)
text(1978, 0.50, "5-year Moving Average", col=colMovAvg)