time
time

Reputation: 949

R : lines are not visible in plot

I have a vector named anomaly which gives the output :

 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

Also have a vector named MA which gives the output :

  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 

There is also a vector year which takes value from 1880 to 2004 .

My intention is to plot anomaly and MA in the same figure . So i wrote down the code :

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

But the graph doesn't produce the lines .

enter image description here

Why are the lines invisible ?

Upvotes: 0

Views: 478

Answers (1)

r2evans
r2evans

Reputation: 160942

I think you already have answers for including year in your calls to plot and lines. To keep the data all the same length, there are a couple of ways that I would try. My preferred method is to use a data.frame, since it appears your data are certainly tied to year:

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

The code to plot it below relies on this, but if you want to continue using individual vectors then you can continue with your own code by using code like this (generalized in case your data lengths will change, in which case you might want to check the length of anomaly as well).

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

The code to plot. I made a few changes, heavily based on my programming OCD :-)

  • Avoiding the use of "magic constants" in my code; imagine if you wanted to change the color of your anomalies at some point, you'd need to change it to the same in two locations. This is minor but when your plots get more complex, this can be a life-saver.
  • Using formulas for plot and lines (taking advantage of the data.frame). This doesn't necessarily improve the readability of the code that much (it's still obvious which is on which axis), but I think it's slightly easier to read (MA ~ year, data=dat, ...) vice (dat$year, dat$MA, ...). Very slightly.

Code:

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)

Upvotes: 1

Related Questions