Reputation: 190
I am simply plotting some forecast errors, however one of my lines is plotting way outside of the chart area when I am using R's plot
function
The data is as follows:
> Errors
1.week 2.weeks 4.weeks 8.weeks
1 -0.8315842 -2.0770613 -1.5939279 -1.0221132
2 2.3557692 1.6248861 3.7038422 5.1803585
3 4.5628527 3.7731578 6.3575075 9.1854482
4 -13.9020725 -13.8751169 -9.9692492 -5.7205310
5 -26.2018878 -25.6789948 -19.6533859 -14.5865603
6 -15.4871932 -15.2419860 -6.8434238 -0.8066797
7 -14.1347094 -14.2751777 -4.5532670 2.5371869
8 7.6573971 7.0516291 20.0454082 29.5035655
9 14.2423208 11.7158540 26.8344705 38.9592733
10 12.9404280 10.4012970 23.8946655 35.4711499
11 13.5389572 11.8270242 22.4562988 32.0706150
12 -16.0386215 -17.8927906 -9.0232955 -0.9692534
13 -8.5479951 -11.4090786 -3.6729858 3.3122453
14 -17.4529120 -21.1515732 -13.8185905 -7.4398603
15 -26.7000634 -32.3277592 -23.1025683 -15.1729123
16 -4.0032531 -12.2613379 -1.8825783 6.7213671
17 8.8957503 -0.9585969 9.9365482 19.0748503
18 32.0322364 19.9301513 34.8202923 46.4041208
19 28.0687543 13.6083628 31.6832461 45.0969812
20 -3.0728661 -16.5312407 -0.9657741 10.3036339
21 11.7878788 -3.0478064 13.8424842 25.9771667
22 9.9892550 -4.9871205 11.5740132 23.2690438
23 22.8998538 6.0118306 22.7025023 35.3195641
24 5.7808321 -10.5933649 4.4892100 17.1939799
25 -6.0924242 -21.5923200 -9.2917299 2.1220339
26 -11.7374904 -28.1082658 -15.9798464 -4.1112579
27 -15.0147630 -29.3107975 -18.0836088 -6.8478348
28 -24.9128044 -38.4231537 -27.1156275 -15.6860608
29 -37.9817750 -51.6920999 -39.2222444 -25.5189189
30 -13.6706635 -27.6301887 -12.9572997 4.1337481
31 11.1107518 -1.6798887 12.6577970 30.1665708
32 31.8661224 19.8344487 34.2043033 52.8466940
33 -10.3451693 -20.2538434 -7.4683061 9.4857313
34 -13.6655462 -25.3717809 -11.4776440 7.5315424
35 11.2209802 -4.4268833 11.5400743 34.4884934
36 17.5595027 -4.1725697 12.6989773 37.6937120
37 72.3065693 41.3124760 61.7028494 93.0466741
38 89.8718428 55.3121569 74.5758621 107.7117030
39 63.0495238 35.4953908 50.1743506 77.7465922
40 4.5273250 -13.0854353 -2.6323285 18.1108514
41 -35.0096805 -48.1653640 -40.5522281 -23.7343486
42 -38.8347996 -52.1927711 -44.3939981 -26.6579849
43 -22.7564612 -40.4697479 -30.9755202 -8.8881556
44 -13.3679435 -33.7536109 -23.0032210 2.4724771
45 -21.5969388 -39.8752151 -29.9660974 -5.3971963
46 -22.2257732 -40.7611160 -30.0873218 -2.5576712
47 41.9854318 17.0388007 32.5581635 72.6495345
48 -4.7979058 -22.0000000 -9.4891390 22.1506720
49 0.8379237 -16.0771325 -2.3059105 31.0642555
50 -48.0432432 -64.6712707 -52.6205779 -23.2243465
51 -31.9405286 -50.1658080 -38.1287671 -6.7165479
52 13.3228700 -6.7078972 5.6320178 39.4271923
53 -9.9223744 -28.5279383 -18.3334270 10.8911537
54 -53.0358796 -69.7852611 -60.7242951 -33.7608786
55 -89.4042056 -106.8017795 -97.2221262 -66.1765325
56 -103.2912736 -117.4078156 -109.6670551 -81.5685855
57 -72.8317422 -89.7210127 -80.3448276 -46.6401129
58 -10.6460606 -33.5872690 -21.9263346 23.1584507
59 44.2416769 18.7887617 30.1124849 78.4170944
60 44.5294118 20.8873684 30.3804447 73.9218079
61 10.4536213 -11.1676424 -2.9950724 35.7541848
62 -13.4498715 -33.8472297 -26.4354588 10.9618605
63 -63.7424837 -83.6746464 -77.0424528 -42.0920245
64 -48.7203166 -74.8127054 -65.6121866 -20.2087413
65 -54.8806366 -77.7627958 -69.0863171 -28.6426332
66 -23.3034759 -46.2179700 -37.3916828 3.1970803
67 -0.4412955 -23.6060606 -14.4324853 28.0687361
68 -39.1934605 -58.2712826 -50.0247934 -13.7366842
69 -6.1793103 -26.9323006 -17.4225871 21.8382838
70 -67.7006993 -86.3950260 -77.7722806 -44.0970399
71 -95.0128023 -112.5663925 -104.3164426 -73.0256679
72 -58.8480463 -81.8627685 -71.4795383 -31.1377660
73 -31.9423077 -54.7657492 -45.3222698 -3.9940266
74 19.1490964 -3.2852665 5.1545455 50.2359800
75 11.1428571 -7.3954082 0.8880652 43.2303546
76 36.2714724 16.8534538 27.1051243 74.8324168
77 -18.1261682 -37.0353403 -27.1089450 18.6923529
78 -4.6456693 -25.0848244 -14.1691176 37.1199284
79 -55.9234450 -70.8848485 -61.7751479 -22.0358990
80 -80.6779935 -92.1077763 -84.1910700 -54.3332299
81 -63.1845140 -74.0436590 -66.2472325 -38.5645570
82 -27.2136895 -45.7618041 -30.1320755 20.0032362
83 -45.6570458 -61.3812950 -45.2886109 0.1458056
84 59.9193825 35.7708029 65.4523810 142.0542107
85 77.8921739 53.8424963 87.0364701 170.8487600
86 191.6063269 163.8654573 209.0661157 321.5901464
87 62.0849558 46.1521906 84.5931883 167.5243004
88 92.6666667 81.5545171 126.6058394 210.9166035
89 3.2517986 -2.0382166 30.7330657 87.3248731
90 -19.4141049 -20.6461039 7.3760903 55.6041667
Below is the code I have used to plot these.
x1 = rep(1:90)
z1 = Errors[[1]]
z2 = Errors[[2]]
z3 = Errors[[3]]
Z4 = Errors[[4]]
plot(x1,z1,type="l",col="red", bty="L", xlab="Forecast Days",ylab="Error") #Plot first line
points(x1,z2,type="l",col="blue") #Add second
points(x1,z3,type="l",col="darkgoldenrod2") #Add third
points(x1,z4,type="l", lwd=3, col="green") #Add fourth
When the fourth line is plotted, it is way above the others, off the chart area and partially outside of the viewing window. When I have plotted them in the reverse order (4th to 1st) the 4th line plots fine and the 3rd to 1st plot way below the chart area in a similar fashion. I'm getting no sort of error message and I can't find any other examples of this to understand why this is happening.
I have used the same code and method to plot the original forecast data (the data the errors have been calculated for) and had no such issues.
I know I can use ggplot2 also for charting, however I do still use this method sometimes. Any help is appreciated.
Using some suggested changes to the code to include the ylim
still produces a similar result:
plot(x1,z1,type="l",col="red", bty="L", xlab="Forecast Days",ylab="Error", ylim = c(min(Errors),max(Errors))) #Plot first destination
points(x1,z2,type="l",col="blue") #Addition of second destination
points(x1,z3,type="l",col="darkgoldenrod2") #Addition of a third destination
points(x1,z4,type="l", lwd=3, col="green") #Addition of a third destination
The fourth line has come down but is still way off from where it should be as the data is all similar.
Upvotes: 0
Views: 1830
Reputation:
The reason why one of lines falls outside the plotting area is probably because the coordinates of the plotting area are calculated from the data that you use in your initial plot()
command.
This can be easily rectified by setting the y-axis range by hand. You can do this by using the argument ylim()
in the plotting command. Here I just calculated the minimum and the maximum from your data, and used those as y-axis limits.
This should do the trick:
plot(x1,z1,type="l",col="red", bty="L", xlab="Forecast Days",ylab="Error",ylim=c(min(Errors),max(Errors))) #Plot first line
points(x1,z2,type="l",col="blue") #Add second
points(x1,z3,type="l",col="darkgoldenrod2") #Add third
points(x1,Z4,type="l", lwd=3, col="green") #Add fourth
Another option is to use the matplot()
function:
matplot(Errors, type="l")
Upvotes: 1