Related
I'm trying to perform a rolling linear regression, whit overlapping windows and for different window sizes. I want to save the result in two tables, one with window size and slopes and one wiht window size a and intercepts.
My data set is now saved as a xts object. I'm trying to make a for-loop to generate the different window-sizes and then do a rolling regression between the two variables, with a overlapping window using rollapply. Then I would like that the coefficients for every regression get saved as a row in a matrix, that can be built on, so that every window size corresponds to one row. I have looked at a lot of previous questions, but I can not get it right.
I have hourly data for 2 variables, this is the data for the x-variable:
> dput(Xq$x)
structure(c(339.76625, 176.7196875, 142.8063125, 118.5785625,
102.0514375, 86.01156251, 86.99050001, 85.0089375, 380.0010626,
2114.279375, 2157.76875, 2442.575, 2562.6375, 2537.7125, 2581.888125,
1947.7575, 834.7918751, 765.1525001, 989.698125, 1433.2, 429.9081251,
388.333125, 1500.995, 1523.816625, 1529.090126, 439.746875, 1029.03625,
1470.07825, 1454.844813, 1448.8455, 413.7550001, 334.03125, 566.7362501,
1609.184375, 1509.266876, 1403.1425, 1142.695625, 795.138125,
1562.881875, 933.190625, 549.085625, 500.135, 535.83, 444.3493751,
356.386875, 268.1875001, 141.1878125, 119.658875, 162.0425625,
153.6944375, 161.0025, 151.37025, 155.9567501, 123.64825, 145.664125,
141.4480625, 232.2009375, 913.6480001, 2227.08125, 1870.618751,
2190.606251, 1357.81625, 824.555625, 699.99, 703.695, 620.74625,
581.2375001, 393.391875, 349.96625, 188.5445625, 154.03125, 142.0825,
339.7915625, 139.8941875, 154.6279376, 171.6016875, 151.1387501,
159.7665, 168.62025, 180.7596875, 447.2746875, 1550.11625, 1887.275,
1446.883125, 699.590625, 983.9925, 1319.62875, 769.505625, 812.713125,
638.1437501, 585.8312501, 626.7875, 1924.293125, 629.855625,
349.88375, 1038.54875, 1588.82225, 548.3475, 1053.368125, 964.825625,
990.324375, 1327.160625, 395.5862501, 421.8154376, 1308.551875,
1334.045, 1633.408125, 886.2662501, 1618.7975, 1325.59625, 847.0693751,
667.501875, 594.0531251, 249.920625, 210.8869376, 224.3725001,
209.6325, 213.64875, 210.3175001, 194.54, 90.1085625, 71.12575001,
70.69412501, 69.6075, 68.404625, 67.88837501, 67.317125, 67.220375,
67.696625, 273.2732501, 606.475625, 1277.3425, 913.24875, 612.5462501,
571.42125, 503.27375, 316.68375, 208.7436251, 127.283, 126.12375,
102.504625, 78.7938125, 70.0183125, 69.97900001, 77.7586875,
76.60543751, 94.5235625, 103.0095625, 105.1938125, 103.8090625,
104.005625, 104.544875, 105.2036875, 104.6611875, 375.7004375,
793.7725001, 782.544375, 257.065625, 387.1487501, 571.1912501,
356.6406251, 243.404375, 220.698125, 182.5680626, 161.060625,
166.5178125, 137.9805, 129.6253125, 99.4291875, 75.88075001,
65.769875, 62.62937501, 59.697875, 57.68675, 56.67275, 56.69425001,
57.296, 638.95125, 1210.92875, 990.09, 907.895625, 1122.704375,
569.139375, 295.6725, 263.21, 236.3885625, 153.935, 148.0899375,
161.72675, 102.1535, 115.8895, 114.2579375, 95.0023125, 90.72425,
84.2715, 71.06631251, 63.39812501, 75.2416875, 73.9420625, 72.26237501,
402.8870625, 1148.9775, 1058.86875, 896.47875, 890.9687501, 833.645625,
670.310625, 298.576875, 306.538125, 386.403125, 196.8343751,
174.0815, 156.6158126, 117.9345625, 105.095, 90.24362501, 87.08800001,
86.1628125, 105.3179375, 107.312625, 114.089, 104.85825, 95.9281875,
126.2874375, 529.7325626, 1533.605625, 1089.700625, 760.47, 1905.04125,
1744.460625, 1346.120625, 847.90625, 459.4625, 472.11, 474.5068751,
481.83875, 937.3618751, 267.1339376, 137.7895625, 124.0425625,
188.9461875, 108.8915, 111.67825, 84.319125, 127.1240625, 117.519125,
104.16225, 105.2490625, 158.5540625, 1215.278063, 1275.396875,
1301.14375, 1108.9225, 1280.390625, 1434.5225, 1038.926875, 599.3218751,
665.22875, 646.935, 487.604375, 236.8148126, 300.6626876, 830.2973126,
1286.612313, 403.0063751, 405.9081875, 156.5465625, 101.7661875,
100.2301875, 144.424625, 126.781125, 143.940625, 365.5354376,
849.313125, 1729.3175, 1762.6625, 1733.968751, 1275.4375, 1332.930625,
620.5625, 504.115625, 538.4556251, 433.5725, 337.094375, 225.183125,
184.7196251, 156.7545, 130.23325, 134.0796875, 145.196125, 153.243,
141.096375, 414.8042501, 304.7252501, 204.1889375, 129.7909375,
555.0276251, 1734.235625, 1780.209375, 1462.7825, 967.4743751,
1248.791875, 1001.655625, 662.92875, 546.801875, 393.72375, 308.85125,
259.985625, 280.439375, 274.1312501, 310.0375001, 270.3, 201.9288125,
219.6061875, 208.372625, 156.2023125, 162.5355626, 167.823875,
151.3928126, 151.42875, 293.7148125, 434.9101875, 943.9243751,
995.955, 614.055, 1387.864375, 1752.330626, 1238.1725, 1260.93,
565.383125, 253.179375, 275.66625, 280.4912501, 222.684375, 338.079875,
201.9365625, 136.7436875, 146.5479375, 142.794625, 150.3969375,
134.6478125, 88.22068751, 70.6445, 71.73737501, 243.176, 1559.0925,
1527.626876, 1260.648125, 1251.564375, 714.1075001, 1124.58,
901.6018751, 551.9075, 303.1025, 355.435625, 372.32375, 235.62125,
185.39475, 161.7701876, 155.4823751), index = structure(c(1438380000,
1438383600, 1438387200, 1438390800, 1438394400, 1438398000, 1438401600,
1438405200, 1438408800, 1438412400, 1438416000, 1438419600, 1438423200,
1438426800, 1438430400, 1438434000, 1438437600, 1438441200, 1438444800,
1438448400, 1438452000, 1438455600, 1438459200, 1438462800, 1438466400,
1438470000, 1438473600, 1438477200, 1438480800, 1438484400, 1438488000,
1438491600, 1438495200, 1438498800, 1438502400, 1438506000, 1438509600,
1438513200, 1438516800, 1438520400, 1438524000, 1438527600, 1438531200,
1438534800, 1438538400, 1438542000, 1438545600, 1438549200, 1438552800,
1438556400, 1438560000, 1438563600, 1438567200, 1438570800, 1438574400,
1438578000, 1438581600, 1438585200, 1438588800, 1438592400, 1438596000,
1438599600, 1438603200, 1438606800, 1438610400, 1438614000, 1438617600,
1438621200, 1438624800, 1438628400, 1438632000, 1438635600, 1438639200,
1438642800, 1438646400, 1438650000, 1438653600, 1438657200, 1438660800,
1438664400, 1438668000, 1438671600, 1438675200, 1438678800, 1438682400,
1438686000, 1438689600, 1438693200, 1438696800, 1438700400, 1438704000,
1438707600, 1438711200, 1438714800, 1438718400, 1438722000, 1438725600,
1438729200, 1438732800, 1438736400, 1438740000, 1438743600, 1438747200,
1438750800, 1438754400, 1438758000, 1438761600, 1438765200, 1438768800,
1438772400, 1438776000, 1438779600, 1438783200, 1438786800, 1438790400,
1438794000, 1438797600, 1438801200, 1438804800, 1438808400, 1438812000,
1438815600, 1438819200, 1438822800, 1438826400, 1438830000, 1438833600,
1438837200, 1438840800, 1438844400, 1438848000, 1438851600, 1438855200,
1438858800, 1438862400, 1438866000, 1438869600, 1438873200, 1438876800,
1438880400, 1438884000, 1438887600, 1438891200, 1438894800, 1438898400,
1438902000, 1438905600, 1438909200, 1438912800, 1438916400, 1438920000,
1438923600, 1438927200, 1438930800, 1438934400, 1438938000, 1438941600,
1438945200, 1438948800, 1438952400, 1438956000, 1438959600, 1438963200,
1438966800, 1438970400, 1438974000, 1438977600, 1438981200, 1438984800,
1438988400, 1438992000, 1438995600, 1438999200, 1439002800, 1439006400,
1439010000, 1439013600, 1439017200, 1439020800, 1439024400, 1439028000,
1439031600, 1439035200, 1439038800, 1439042400, 1439046000, 1439049600,
1439053200, 1439056800, 1439060400, 1439064000, 1439067600, 1439071200,
1439074800, 1439078400, 1439082000, 1439085600, 1439089200, 1439092800,
1439096400, 1439100000, 1439103600, 1439107200, 1439110800, 1439114400,
1439118000, 1439121600, 1439125200, 1439128800, 1439132400, 1439136000,
1439139600, 1439143200, 1439146800, 1439150400, 1439154000, 1439157600,
1439161200, 1439164800, 1439168400, 1439172000, 1439175600, 1439179200,
1439182800, 1439186400, 1439190000, 1439193600, 1439197200, 1439200800,
1439204400, 1439208000, 1439211600, 1439215200, 1439218800, 1439222400,
1439226000, 1439229600, 1439233200, 1439236800, 1439240400, 1439244000,
1439247600, 1439251200, 1439254800, 1439258400, 1439262000, 1439265600,
1439269200, 1439272800, 1439276400, 1439280000, 1439283600, 1439287200,
1439290800, 1439294400, 1439298000, 1439301600, 1439305200, 1439308800,
1439312400, 1439316000, 1439319600, 1439323200, 1439326800, 1439330400,
1439334000, 1439337600, 1439341200, 1439344800, 1439348400, 1439352000,
1439355600, 1439359200, 1439362800, 1439366400, 1439370000, 1439373600,
1439377200, 1439380800, 1439384400, 1439388000, 1439391600, 1439395200,
1439398800, 1439402400, 1439406000, 1439409600, 1439413200, 1439416800,
1439420400, 1439424000, 1439427600, 1439431200, 1439434800, 1439438400,
1439442000, 1439445600, 1439449200, 1439452800, 1439456400, 1439460000,
1439463600, 1439467200, 1439470800, 1439474400, 1439478000, 1439481600,
1439485200, 1439488800, 1439492400, 1439496000, 1439499600, 1439503200,
1439506800, 1439510400, 1439514000, 1439517600, 1439521200, 1439524800,
1439528400, 1439532000, 1439535600, 1439539200, 1439542800, 1439546400,
1439550000, 1439553600, 1439557200, 1439560800, 1439564400, 1439568000,
1439571600, 1439575200, 1439578800, 1439582400, 1439586000, 1439589600,
1439593200, 1439596800, 1439600400, 1439604000, 1439607600, 1439611200,
1439614800, 1439618400, 1439622000, 1439625600, 1439629200, 1439632800,
1439636400, 1439640000, 1439643600, 1439647200, 1439650800, 1439654400,
1439658000, 1439661600, 1439665200, 1439668800, 1439672400), tzone = "UTC", tclass = c("chron",
"dates", "times")), class = c("xts", "zoo"), .indexCLASS = c("chron",
"dates", "times"), tclass = c("chron", "dates", "times"), .indexTZ = "UTC", tzone = "UTC", .Dim = c(360L,
1L), .Dimnames = list(NULL, "x"))
And my y-variable:
> dput(Xq$y)
structure(c(-0.274050833, -0.236638333, -0.1994325, -0.174091667,
-0.153273333, -0.136978333, -0.124748333, -0.117348333, -0.147061667,
-0.346170833, -0.517939167, -0.575585833, -0.595914167, -0.563639167,
-0.53403, -0.5500525, -0.505650833, -0.471713333, -0.472485833,
-0.547393333, -0.390174167, -0.321545, -0.29781, -0.26912, -0.345084167,
-0.367618333, -0.279933333, -0.256805, -0.2514675, -0.314349167,
-0.3594375, -0.33482, -0.369094167, -0.4801075, -0.554780833,
-0.600498333, -0.604796667, -0.544491667, -0.636653333, -0.568401667,
-0.485494167, -0.453199167, -0.417475, -0.38417, -0.341585833,
-0.2821625, -0.248325, -0.2230575, -0.2449075, -0.2385375, -0.231885,
-0.214125, -0.194190833, -0.178575833, -0.1677675, -0.1615725,
-0.1739675, -0.2432125, -0.402414167, -0.448185833, -0.563599167,
-0.5855025, -0.55586, -0.516350833, -0.47892, -0.4603325, -0.434146667,
-0.37602, -0.320976667, -0.267863333, -0.250915, -0.241764167,
-0.271475, -0.2170225, -0.206605, -0.2088, -0.214511667, -0.2086825,
-0.203060833, -0.192895, -0.212629167, -0.319143333, -0.42647,
-0.4623275, -0.467844167, -0.506395833, -0.507088333, -0.496953333,
-0.511156667, -0.487846667, -0.4455525, -0.398383333, -0.565926667,
-0.429720833, -0.314555, -0.418586667, -0.578256667, -0.414858333,
-0.345411667, -0.3088925, -0.304373333, -0.334221667, -0.305029167,
-0.273269167, -0.315901667, -0.409731667, -0.500245833, -0.505959167,
-0.54742, -0.574725, -0.548458333, -0.5560675, -0.50246, -0.411618333,
-0.35965, -0.331884167, -0.312573333, -0.298478333, -0.289144167,
-0.274429167, -0.2218225, -0.198025833, -0.191955833, -0.1780825,
-0.157910833, -0.135935, -0.116965833, -0.099886667, -0.0864975,
-0.188904167, -0.325656667, -0.444044167, -0.5050425, -0.54236,
-0.547500833, -0.5399775, -0.521490833, -0.444911667, -0.388053333,
-0.327650833, -0.2478, -0.2026775, -0.1693425, -0.140848333,
-0.130440833, -0.103501667, -0.088843333, -0.09344, -0.126231667,
-0.158463333, -0.181145, -0.185095, -0.11657, -0.0349225, -0.223260833,
-0.431800833, -0.516, -0.487566667, -0.49941, -0.541773333, -0.511953333,
-0.4404425, -0.4021125, -0.380635, -0.3301275, -0.302468333,
-0.311290833, -0.2774125, -0.223738333, -0.192925, -0.176034167,
-0.158151667, -0.136234167, -0.117050833, -0.101201667, -0.08872,
-0.0855675, -0.257761667, -0.424794167, -0.528980833, -0.56834,
-0.591263333, -0.56574, -0.53178, -0.5410825, -0.552130833, -0.501294167,
-0.44576, -0.402855833, -0.316163333, -0.2515275, -0.213383333,
-0.186723333, -0.174315833, -0.170100833, -0.154446667, -0.147075833,
-0.169245, -0.188474167, -0.187344167, -0.2586975, -0.440815,
-0.534344167, -0.598391667, -0.613878333, -0.624658333, -0.583091667,
-0.508740833, -0.518310833, -0.51102, -0.4203825, -0.364895833,
-0.31302, -0.27689, -0.254130833, -0.232273333, -0.218198333,
-0.22501, -0.235655833, -0.242728333, -0.260448333, -0.263113333,
-0.243530833, -0.222301667, -0.274098333, -0.3974225, -0.484443333,
-0.506165, -0.6136375, -0.631805, -0.596274167, -0.539795, -0.446769167,
-0.398489167, -0.3986925, -0.4098625, -0.484515833, -0.375961667,
-0.291685, -0.273963333, -0.2621025, -0.2054525, -0.177625833,
-0.1564625, -0.141883333, -0.130685833, -0.121943333, -0.112834167,
-0.136975, -0.3204725, -0.458004167, -0.5262175, -0.527530833,
-0.520574167, -0.5800325, -0.561841667, -0.4909225, -0.4641625,
-0.443126667, -0.405705833, -0.3098625, -0.286379167, -0.30477,
-0.395341667, -0.38505, -0.356219167, -0.263050833, -0.219625833,
-0.197383333, -0.2019775, -0.226838333, -0.2333075, -0.2890475,
-0.3341175, -0.43458, -0.520441667, -0.577875, -0.568123333,
-0.551936667, -0.463691667, -0.468790833, -0.4747725, -0.4125925,
-0.394731667, -0.380213333, -0.308688333, -0.279549167, -0.249766667,
-0.233964167, -0.229904167, -0.244835833, -0.232436667, -0.215466667,
-0.198559167, -0.184533333, -0.185376667, -0.246823333, -0.395918333,
-0.4956775, -0.540474167, -0.5402375, -0.577863333, -0.561466667,
-0.503130833, -0.455221667, -0.4401875, -0.4187675, -0.389215,
-0.345275, -0.3378175, -0.348759167, -0.325149167, -0.29995,
-0.289409167, -0.291635, -0.301183333, -0.274799167, -0.2443375,
-0.2254225, -0.21272, -0.245265, -0.295081667, -0.356848333,
-0.4258325, -0.4329175, -0.487074167, -0.595525, -0.59333, -0.564645,
-0.464548333, -0.4294775, -0.425609167, -0.403769167, -0.353311667,
-0.282888333, -0.2464575, -0.231771667, -0.2275425, -0.224230833,
-0.218419167, -0.1902275, -0.161319167, -0.143495833, -0.133691667,
-0.181291667, -0.36056, -0.46681, -0.5194575, -0.532989167, -0.4899375,
-0.533224167, -0.4976575, -0.428966667, -0.412929167, -0.416463333,
-0.366666667, -0.316356667, -0.3023825, -0.282655, -0.267275833
), index = structure(c(1438380000, 1438383600, 1438387200, 1438390800,
1438394400, 1438398000, 1438401600, 1438405200, 1438408800, 1438412400,
1438416000, 1438419600, 1438423200, 1438426800, 1438430400, 1438434000,
1438437600, 1438441200, 1438444800, 1438448400, 1438452000, 1438455600,
1438459200, 1438462800, 1438466400, 1438470000, 1438473600, 1438477200,
1438480800, 1438484400, 1438488000, 1438491600, 1438495200, 1438498800,
1438502400, 1438506000, 1438509600, 1438513200, 1438516800, 1438520400,
1438524000, 1438527600, 1438531200, 1438534800, 1438538400, 1438542000,
1438545600, 1438549200, 1438552800, 1438556400, 1438560000, 1438563600,
1438567200, 1438570800, 1438574400, 1438578000, 1438581600, 1438585200,
1438588800, 1438592400, 1438596000, 1438599600, 1438603200, 1438606800,
1438610400, 1438614000, 1438617600, 1438621200, 1438624800, 1438628400,
1438632000, 1438635600, 1438639200, 1438642800, 1438646400, 1438650000,
1438653600, 1438657200, 1438660800, 1438664400, 1438668000, 1438671600,
1438675200, 1438678800, 1438682400, 1438686000, 1438689600, 1438693200,
1438696800, 1438700400, 1438704000, 1438707600, 1438711200, 1438714800,
1438718400, 1438722000, 1438725600, 1438729200, 1438732800, 1438736400,
1438740000, 1438743600, 1438747200, 1438750800, 1438754400, 1438758000,
1438761600, 1438765200, 1438768800, 1438772400, 1438776000, 1438779600,
1438783200, 1438786800, 1438790400, 1438794000, 1438797600, 1438801200,
1438804800, 1438808400, 1438812000, 1438815600, 1438819200, 1438822800,
1438826400, 1438830000, 1438833600, 1438837200, 1438840800, 1438844400,
1438848000, 1438851600, 1438855200, 1438858800, 1438862400, 1438866000,
1438869600, 1438873200, 1438876800, 1438880400, 1438884000, 1438887600,
1438891200, 1438894800, 1438898400, 1438902000, 1438905600, 1438909200,
1438912800, 1438916400, 1438920000, 1438923600, 1438927200, 1438930800,
1438934400, 1438938000, 1438941600, 1438945200, 1438948800, 1438952400,
1438956000, 1438959600, 1438963200, 1438966800, 1438970400, 1438974000,
1438977600, 1438981200, 1438984800, 1438988400, 1438992000, 1438995600,
1438999200, 1439002800, 1439006400, 1439010000, 1439013600, 1439017200,
1439020800, 1439024400, 1439028000, 1439031600, 1439035200, 1439038800,
1439042400, 1439046000, 1439049600, 1439053200, 1439056800, 1439060400,
1439064000, 1439067600, 1439071200, 1439074800, 1439078400, 1439082000,
1439085600, 1439089200, 1439092800, 1439096400, 1439100000, 1439103600,
1439107200, 1439110800, 1439114400, 1439118000, 1439121600, 1439125200,
1439128800, 1439132400, 1439136000, 1439139600, 1439143200, 1439146800,
1439150400, 1439154000, 1439157600, 1439161200, 1439164800, 1439168400,
1439172000, 1439175600, 1439179200, 1439182800, 1439186400, 1439190000,
1439193600, 1439197200, 1439200800, 1439204400, 1439208000, 1439211600,
1439215200, 1439218800, 1439222400, 1439226000, 1439229600, 1439233200,
1439236800, 1439240400, 1439244000, 1439247600, 1439251200, 1439254800,
1439258400, 1439262000, 1439265600, 1439269200, 1439272800, 1439276400,
1439280000, 1439283600, 1439287200, 1439290800, 1439294400, 1439298000,
1439301600, 1439305200, 1439308800, 1439312400, 1439316000, 1439319600,
1439323200, 1439326800, 1439330400, 1439334000, 1439337600, 1439341200,
1439344800, 1439348400, 1439352000, 1439355600, 1439359200, 1439362800,
1439366400, 1439370000, 1439373600, 1439377200, 1439380800, 1439384400,
1439388000, 1439391600, 1439395200, 1439398800, 1439402400, 1439406000,
1439409600, 1439413200, 1439416800, 1439420400, 1439424000, 1439427600,
1439431200, 1439434800, 1439438400, 1439442000, 1439445600, 1439449200,
1439452800, 1439456400, 1439460000, 1439463600, 1439467200, 1439470800,
1439474400, 1439478000, 1439481600, 1439485200, 1439488800, 1439492400,
1439496000, 1439499600, 1439503200, 1439506800, 1439510400, 1439514000,
1439517600, 1439521200, 1439524800, 1439528400, 1439532000, 1439535600,
1439539200, 1439542800, 1439546400, 1439550000, 1439553600, 1439557200,
1439560800, 1439564400, 1439568000, 1439571600, 1439575200, 1439578800,
1439582400, 1439586000, 1439589600, 1439593200, 1439596800, 1439600400,
1439604000, 1439607600, 1439611200, 1439614800, 1439618400, 1439622000,
1439625600, 1439629200, 1439632800, 1439636400, 1439640000, 1439643600,
1439647200, 1439650800, 1439654400, 1439658000, 1439661600, 1439665200,
1439668800, 1439672400), tzone = "UTC", tclass = c("chron", "dates",
"times")), class = c("xts", "zoo"), .indexCLASS = c("chron",
"dates", "times"), tclass = c("chron", "dates", "times"), .indexTZ = "UTC", tzone = "UTC", .Dim = c(360L,
1L), .Dimnames = list(NULL, "y"))
My code looks like this:
library(zoo)
mat_slope=matrix(nrow=i)
mat_intercept=matrix(nrow=i)
for(i in seq(from=24, to=240, by=24)){
mov.reg<- rollapplyr(Xq,
width= i,
by=24,
FUN = function(y,x)
{
coefficients(lm(formula=y~x, data = Xq))
},
by.column=FALSE)
mat_slope[i] <- coefficients(mov.reg)[2]
mat_intercept[i] <-coefficients(mov.reg)[1]
mat_intercept=rbind(i,mat_intercept)
mat_slope[n,i]=rbind(i, mat_slope)
}
I either get an empty matrix as result, or the following error message:
Error in merge.xts(res, xts(, idx, attr(data, "frequency"))) :
(list) object cannot be coerced to type 'double'
If anyone have some inputs or ideas it would be very appreciated! Thank's a lot!
mov.reg is not an lm objct, so it does not make sense to take coefficients of it. Also there is a reference to chron in the dput output so we need to load the chron package.
Xq is not specified but rather two separate xts objects are shown so we have assumed that those two are called x and y. Then create a single zoo object z from them. Now define a Coef function which takes a matrix with x and y columns and performs the indicated regression returning the coefficients. Also define roll which takes the width as input and runs rollapplyr with the desired arguments. Now use lapply to run roll with each width producing a list of zoo objects. This list has one component for each element in widths consisting of a zoo object with intercept and slope columns. Finally extract the intercepts from each component in the list and form a zoo object and do the same for the slopes. Note that if L is a list of zoo objects then do.call("merge", L) will produce a single zoo object from it.
library(xts) # this also loads zoo
library(chron)
# inputs are xts objects x and y
z <- cbind(x = as.zoo(x)[, 1], y = as.zoo(y)[, 1])
Coef <- function(m) coef(lm(y ~ x, as.data.frame(m)))
roll <- function(w) rollapplyr(z, w, Coef, by = 24, by.column = FALSE)
widths <- seq(24, 240, 24)
names(widths) <- widths # lapply will use these names for its output
L <- lapply(widths, roll)
intercepts <- do.call("merge", lapply(L, "[", TRUE, 1)) # extract 1st columns
slopes <- do.call("merge", lapply(L, "[", TRUE, 2)) # 2nd columns
Alternately do the rollapplyr twice replacing the last 3 lines with:
intercepts <- do.call("merge", lapply(widths, function(w) roll(w)[, 1]))
slopes <- do.call("merge", lapply(widths, function(w) roll(w)[, 2]))
I was wondering if anyone knows of a way to get the dates to appear on the x axis of an stl plot.
res<- (stl(ts(data[,variable],frequency=52,start=as.Date(data[1,date]) ),s.window="per",robust=TRUE) )
plot(res)
However this is just producing some unicode labels at the bottom of the chart, I have also tried chaning as.Date to as.character however this didn't work.
dput of some data:
structure(c("2007-01-01", "2007-01-08", "2007-01-15", "2007-01-22",
"2007-01-29", "2007-02-05", "2007-02-12", "2007-02-19", "2007-02-26",
"2007-03-05", "2007-03-12", "2007-03-19", "2007-03-26", "2007-04-02",
"2007-04-09", "2007-04-16", "2007-04-23", "2007-04-30", "2007-05-07",
"2007-05-14", "2007-05-21", "2007-05-28", "2007-06-04", "2007-06-11",
"2007-06-18", "2007-06-25", "2007-07-02", "2007-07-09", "2007-07-16",
"2007-07-23", "2007-07-30", "2007-08-06", "2007-08-13", "2007-08-20",
"2007-08-27", "2007-09-03", "2007-09-10", "2007-09-17", "2007-09-24",
"2007-10-01", "2007-10-08", "2007-10-15", "2007-10-22", "2007-10-29",
"2007-11-05", "2007-11-12", "2007-11-19", "2007-11-26", "2007-12-03",
"2007-12-10", "2007-12-17", "2007-12-24", "2007-12-31", "2008-01-07",
"2008-01-14", "2008-01-21", "2008-01-28", "2008-02-04", "2008-02-11",
"2008-02-18", "2008-02-25", "2008-03-03", "2008-03-10", "2008-03-17",
"2008-03-24", "2008-03-31", "2008-04-07", "2008-04-14", "2008-04-21",
"2008-04-28", "2008-05-05", "2008-05-12", "2008-05-19", "2008-05-26",
"2008-06-02", "2008-06-09", "2008-06-16", "2008-06-23", "2008-06-30",
"2008-07-07", "2008-07-14", "2008-07-21", "2008-07-28", "2008-08-04",
"2008-08-11", "2008-08-18", "2008-08-25", "2008-09-01", "2008-09-08",
"2008-09-15", "2008-09-22", "2008-09-29", "2008-10-06", "2008-10-13",
"2008-10-20", "2008-10-27", "2008-11-03", "2008-11-10", "2008-11-17",
"2008-11-24", "2008-12-01", "2008-12-08", "2008-12-15", "2008-12-22",
"2008-12-29", "2009-01-05", "2009-01-12", "2009-01-19", "2009-01-26",
"2009-02-02", "2009-02-09", "2009-02-16", "2009-02-23", "2009-03-02",
"2009-03-09", "2009-03-16", "2009-03-23", "2009-03-30", "2009-04-06",
"2009-04-13", "2009-04-20", "2009-04-27", "2009-05-04", "2009-05-11",
"2009-05-18", "2009-05-25", "2009-06-01", "2009-06-08", "2009-06-15",
"2009-06-22", "2009-06-29", "2009-07-06", "2009-07-13", "2009-07-20",
"2009-07-27", "2009-08-03", "2009-08-10", "2009-08-17", "2009-08-24",
"2009-08-31", "2009-09-07", "2009-09-14", "2009-09-21", "2009-09-28",
"2009-10-05", "2009-10-12", "2009-10-19", "2009-10-26", "2009-11-02",
"2009-11-09", "2009-11-16", "2009-11-23", "2009-11-30", "2009-12-07",
"2009-12-14", "2009-12-21", "2009-12-28", "2010-01-04", "2010-01-11",
"2010-01-18", "2010-01-25", "2010-02-01", "2010-02-08", "2010-02-15",
"2010-02-22", "2010-03-01", "2010-03-08", "2010-03-15", "2010-03-22",
"2010-03-29", "2010-04-05", "2010-04-12", "2010-04-19", "2010-04-26",
"2010-05-03", "2010-05-10", "2010-05-17", "2010-05-24", "2010-05-31",
"2010-06-07", "2010-06-14", "2010-06-21", "2010-06-28", "2010-07-05",
"2010-07-12", "2010-07-19", "2010-07-26", "2010-08-02", "2010-08-09",
"2010-08-16", "2010-08-23", "2010-08-30", "2010-09-06", "2010-09-13",
"2010-09-20", "2010-09-27", "2010-10-04", "2010-10-11", "2010-10-18",
"2010-10-25", "2010-11-01", "2010-11-08", "2010-11-15", "2010-11-22",
"2010-11-29", "2010-12-06", "2010-12-13", "2010-12-20", "2010-12-27",
"2011-01-03", "2011-01-10", "2011-01-17", "2011-01-24", "2011-01-31",
"2011-02-07", "2011-02-14", "2011-02-21", "2011-02-28", "2011-03-07",
"2011-03-14", "2011-03-21", "2011-03-28", "2011-04-04", "2011-04-11",
"2011-04-18", "2011-04-25", "2011-05-02", "2011-05-09", "2011-05-16",
"2011-05-23", "2011-05-30", "2011-06-06", "2011-06-13", "2011-06-20",
"2011-06-27", "2011-07-04", "2011-07-11", "2011-07-18", "2011-07-25",
"2011-08-01", "2011-08-08", "2011-08-15", "2011-08-22", "2011-08-29",
"2011-09-05", "2011-09-12", "2011-09-19", "2011-09-26", "2011-10-03",
"2011-10-10", "2011-10-17", "2011-10-24", "2011-10-31", "2011-11-07",
"2011-11-14", "2011-11-21", "2011-11-28", "2011-12-05", "2011-12-12",
"2011-12-19", "2011-12-26", "442573", "452832", "452785", "459228",
"479509", "477631", "465619", "462001", "485567", "462381", "456059",
"457401", "474094", "468766", "456945", "539126", "545640", "511801",
"486619", "484430", "481428", "470622", "479677", "486755", "477617",
"483656", "497479", "493436", "480080", "481527", "516029", "532349",
"503939", "472171", "461550", "504532", "489715", "480032", "488376",
"470772", "467395", "488155", "455712", "474456", "471237", "482943",
"459320", "456956", "465056", "461930", "441201", "451255", "464508",
"449199", "455411", "476323", "521761", "513416", "521070", "497596",
"485461", "485593", "461148", "429938", "441207", "459484", "462099",
"469285", "454395", "456729", "469251", "517727", "526719", "477985",
"484538", "469766", "472399", "481162", "479039", "481071", "485068",
"462108", "459079", "452410", "488574", "502166", "504323", "514529",
"524206", "504315", "462935", "461899", "461551", "455491", "456857",
"443314", "454586", "458943", "450555", "454311", "442808", "440126",
"414876", "415787", "413352", "423864", "448319", "415440", "431948",
"433313", "448509", "436400", "454154", "454183", "447735", "452220",
"451433", "472808", "446767", "426595", "463693", "435673", "452704",
"456828", "465069", "448685", "457353", "443859", "463972", "480139",
"493342", "500982", "529602", "527365", "512005", "484585", "455007",
"470006", "477522", "443956", "459038", "488877", "466669", "476242",
"470862", "457298", "466438", "454062", "460216", "466645", "445113",
"457255", "451553", "451504", "447991", "435100", "416140", "481390",
"489041", "496984", "486245", "478191", "455201", "448085", "459097",
"480859", "490248", "462523", "489755", "468391", "460229", "481276",
"472845", "481099", "476435", "487314", "475043", "476847", "461928",
"483488", "479379", "456732", "461538", "480773", "471101", "459898",
"482129", "464356", "459420", "457850", "456764", "438152", "467928",
"464732", "458767", "470256", "449488", "433263", "428548", "435099",
"429163", "424251", "434723", "425841", "399498", "418791", "405051",
"385037", "425144", "418296", "397644", "414283", "431907", "429117",
"424862", "439664", "432791", "443588", "434985", "442418", "445760",
"449290", "451412", "456247", "444372", "441390", "458192", "456435",
"450670", "447609", "439083", "464513", "462784", "439423", "450857",
"442374", "447753", "440207", "435254", "430841", "437233", "426523",
"430127", "431305", "470244", "508878", "511064", "504182", "462076",
"452218", "426535", "436892", "459008", "441449", "438783", "427497",
"432275", "436745", "423068", "429574", "416074"), .Dim = c(261L,
2L))
The problem is with how you have created the time series object.
?ts will give you
start : the time of the first observation. Either a single number or a
vector of two integers, which specify a natural time unit and a
(1-based) number of samples into the time unit.
Since your data starts at 2007-01-01 and is a weekly data, you have to specify c(2007,1), Note that the "1" here denotes that data start from the 1st week of 2007, as the data is weekly. Here the frequency should be set to 52 because, after 52 cycles/weeks a year will change from 2007 to 2008.
This should work
res<- (stl(ts(data[,2],frequency=52,start=c(2007,1 )),s.window="per",robust=TRUE) )
I'm using the som function from kohonen package and I'm using this tutorial as a guide.
som_model <- som(t(data_train_matrix),
grid=som_grid,
rlen=1,
alpha=c(0.05,0.01),
keep.data = TRUE,
n.hood= "circular")
plot(som_model, type="count")
I have a data of 31×1400 but when I'm trying to read the counts plot, it doesn't match the same quantity of samples (31). The plot I got gives me around 100 samples.
this is an example of my data
structure(c(-8691, 96233, 94318, 19206, 139453, 135652, 47782,
56190, -63139, 161452, 83268, 194879, -4112, 193153, -55742,
168244, -26043, 171304, 67181, -52147, -45487, 77098, 79648,
123380, -52991, 113980, -35812, 161776, -26874, 125046, 89662,
94537, 68053, 59601, 87972, 8844, 82108, 85343, 145252, 130751,
66224, 32792, 101576, 174156, 37090, 102740, 101521, 154264,
28431, 81740, 113085, 104153, 6385, 95860, 67247, 58344, 106147,
225375, -714, 212145, 51164, 24108, 33896, 128937, 182192, 5715,
94954, 198818, 127890, 272457, -45888, 96562, 15617, 22385, 102185,
154694, 93797, 55638, 118959, 184010, 6310, 54728, 141369, 209807,
140799, 100809, 93704, 198260, 264015, 271577, 90354, 125713,
112490, 103888, 186512, 178664, 30646, 100788, 194517, 197307,
-3709, 111675, 175022, 186898, 242656, 107932, 269935, 110129,
38492, 88179, 35392, 81003, 118482, 124713, 124830, 78896, 79962,
154348, 181855, 8146, 90599, 115869, 58740, 119922, 12046, 216180,
24770, 329719, 96091, 239658, 119638, 96020, 120758, 144941,
170427, 26691, 112377, 246679, 170131, 314849, 97399, 194975,
60680, 287463, 85241, 145845, 31902, 93297, 305020, 261229, 176675,
146127, 306958, 62111, 101310, 113621, 208697, 73762, 354256,
104883, 202739, 108740, 287507, 106152, 532421, 115689, 643646,
143442, 1026203, 130904, 1951723, 83570, 5818682, 289423, 5864851,
49296, 2369016, 184566, 1184562, 188584, 635480, 88563, 577216,
144760, 454805, 139133, 510350, 40305, 366688, 55486, 258099,
74080, 281519, 162968, 218759, 334500, 353090, 263008, 210560,
445832, 378725, 497266, 298151, 469138, 248365, 381608, 219821,
393548, 116469, 396907, 320685, 429351, 164311, 397822, 270935,
565860, 245619, 435443, 214331, 417268, 268967, 459714, 419833,
348551, 314194, 480522, 183062, 274876, 295398, 470877, 350616,
425871, 642579, 670075, 395643, 679445, 333423, 524843, 207011,
488471, 426073, 263541, 328925, 485258, 419273, 493798, 325854,
505041, 317356, 394418, 254990, 432610, 254212, 459642, 311613,
429672, 340076, 478631, 393919, 415449, 335445, 499644, 310914,
479802, 229937, 317058, 321625, 349939, 410056, 450019, 389055,
544372, 382828, 465619, 441888, 522416, 321660, 562040, 390768,
427916, 356081, 477152, 481436, 609419, 589189, 710624, 1167121,
1884282, 687543, 949401, 613100, 834546, 425695, 613319, 444637,
624192, 684646, 771698, 726366, 813694, 616384, 756803, 483736,
707427, 517319, 761253, 617534, 785160, 765434, 892135, 1057392,
1144724, 902210, 1114782, 1027470, 1488406, 1794285, 3202191,
1680875, 2378078, 1384652, 1942915, 1940959, 2753094, 8178326,
9296747, 17334234, 18341903, 4077766, 4757832, 12437987, 13770176,
5388983, 6043089, 3557519, 4344363, 3462297, 4193800, 3002130,
3751664, 2889390, 3596128, 2990671, 4052441, 4551441, 7645868,
6055792, 8720274, 11204851, 14342229, 13297134, 17421005, 16032135,
22046214, 33034409, 41895354, 134659124, 309713419, 54318759,
138429326, 21521670, 32507615, 31364480, 51975213, 41972208,
70888295, 96423383, 193962950, 127239777, 183865572, 184485432,
362581277, 266284997, 372142920, 254797938, 379524966, 168531540,
217593817, 109955151, 132909067, 45946711, 52766776, 39059161,
46851181, 36854510, 44138904, 42208748, 51775529, 30842738, 38044295,
34739365, 62307643, 35222394, 263589982, 19288663, 53574604,
22473059, 32180156, 15377680, 39229960, 14883020, 37024731, 26775128,
38438456, 31174328, 37159679, 20374102, 24638170, 18843009, 23709900,
16290335, 22019997, 13866224, 21410739, 13409806, 19807146, 12580394,
19423477, 12605523, 20175243, 11286325, 15895629, 12624149, 20150895,
15564663, 23173356, 13344234, 19144941, 16147475, 25059434, 16165655,
22543911, 17719126, 26056352, 25241362, 33870246, 20346030, 25373366,
27599769, 34874263, 19750731, 25488861, 17456089, 26803417, 21655991,
29032569, 18525780, 28589676, 28986493, 54330313, 30702160, 62741684,
36217696, 66746224, 37179427, 70384105, 32845869, 54067819, 40466477,
60334993, 47169915, 71194010, 64983410, 94329526, 107713258,
163970090, 205975485, 422860074, 727019292, 1894177881, 1536778649,
2595541011, 1325315247, 1724342395, 623772789, 933707256, 520787992,
763760597, 484959955, 762351348, 544836917, 814960223, 557751071,
825802985, 484106551, 727516034, 320419472, 473245904, 215986703,
319460038, 176355114, 258972369, 147044357, 203777597, 93349619,
132506190, 89329707, 120091964, 56028958, 73913976, 39160835,
57805430, 31597040, 45502763, 24996502, 38369765, 23164619, 34974560,
23446564, 36260730, 24240365, 37462383, 24380634, 38464790, 23825674,
38232189, 24261678, 40822263, 22545025, 37662136, 25969748, 41828554,
25342961, 41373278, 28190579, 51622837, 40721443, 89715709, 33899480,
63713905, 33879100, 82571628, 33194622, 63495601, 40899580, 83115054,
52391035, 119897472, 43331860, 85516401, 40972806, 111409364,
45229140, 86017237, 60769699, 114219430, 76387165, 153804973,
100612861, 166584932, 119025540, 209322137, 125258469, 215257202,
126370047, 181489720, 114200659, 190602759, 98391103, 165094932,
82887335, 170246121, 62767360, 159226407, 53772778, 144576214,
56504793, 178803162, 54720435, 176147058, 43853977, 160656487,
40490358, 127145028, 46175432, 125716299, 47970998, 142990986,
32686143, 96639106, 28256535, 113010719, 32245717, 109662077,
49315728, 158277164, 39866932, 133740008, 29835677, 121148901,
24484676, 101192015, 20524807, 79615303, 19472158, 67957180,
17802439, 57347318, 17393829, 46409063, 21637835, 57425134, 24685542,
64142491, 22664706, 52782681, 18106352, 45773357, 17952323, 35943518,
22782703, 48608934, 29545983, 54107623, 20658150, 35173101, 19308973,
30420232, 21648523, 33358523, 34925054, 57821290, 23749536, 52300713,
22246131, 51411073, 29970764, 49549669, 28882978, 45160688, 23305717,
42669741, 22496477, 42017707, 24123603, 37571652, 27087406, 46212322,
31957832, 67751755, 31735687, 44207281, 31518232, 39656499, 44331797,
66592574, 305051402, 236738335, 61016620, 351951034, 47694378,
54296987, 57684704, 62552739, 72834978, 76647987, 77661681, 90054910,
94027736, 105247183, 94705645, 118623817, 127635699, 168214213,
125541816, 183982552, 158891836, 202074988, 154380584, 202356268,
156180324, 227847605, 151802781, 210993781, 103866998, 140327139,
91144468, 146060827, 78798545, 172226188, 94375043, 184702914,
59593981, 111951306, 48128719, 67660907, 58181026, 63873729,
58155047, 64229300, 59114829, 66547475, 51011422, 59659321, 40733341,
48214428, 48326348, 55047638, 37896199, 42305785, 31219727, 37677791,
36206563, 45170766, 30796703, 39213659, 29426202, 38564819, 31122601,
41869397, 30150284, 39687478, 30251942, 41245534, 36266084, 50365634,
42078213, 72001066, 38470907, 60095827, 45098881, 67795094, 32427023,
43091370, 44211158, 69249130, 46198628, 72272292, 42080483, 59575435,
67142635, 78404256, 91590826, 91367080, 81279600, 97189859, 159178282,
145226665, 64489072, 69171662, 107042650, 117525720, 71536249,
91016903, 55757027, 135642542, 41353460, 72293186, 68166846,
222105000, 29394948, 73100483, 40777925, 101079557, 29585737,
75349492, 25111702, 36658070, 20004139, 30107908, 17352754, 24086411,
16352644, 23340098, 15867984, 21918125, 15447299, 22444804, 15101802,
21772688, 28749939, 41991543, 18181681, 25302101, 15872482, 22007830,
18623139, 27740895, 27122497, 38924843, 15959970, 23381320, 19605220,
25139255, 20045670, 24677277, 17806558, 25316488, 17454866, 24929439,
21039898, 27983508, 26828223, 37740624, 24913259, 41158419, 37691892,
82078166, 39765574, 105437749, 39806713, 92607859, 36546145,
79750166, 31341108, 76242765, 22884050, 66475659, 17116842, 41957883,
12610875, 20866546, 11422645, 18295880, 11426134, 16997562, 10421891,
16780423, 9952551, 15942561, 9597599, 16154386, 9345908, 15026048,
8971600, 15287752, 8630352, 14028149, 8972152, 13734295, 9210456,
15206966, 8586592, 14676163, 9113002, 15707156, 8724114, 14789908,
9653042, 14574939, 10636733, 15123006, 10884812, 15665015, 16096044,
31557043, 50665144, 69162457, 40125003, 67033492, 88178064, 117506119,
48467438, 62147370, 48550396, 64970507, 28885550, 33953693, 14804984,
21215748, 13263821, 19255188, 11543123, 16023682, 11155710, 13600438,
13419212, 15819226, 11648329, 14025008, 15381219, 15881212, 18821434,
23967947, 11387648, 12943318, 13650064, 17002021, 9341178, 11791878,
7537499, 9877936, 7424723, 9225182, 7303770, 9539660, 7533521,
9356114, 7445487, 9183369, 7723960, 10046696, 7069531, 8696718,
6883109, 8408871, 6576015, 8175139, 6292912, 7827066, 6193985,
7618591, 6092437, 7463851, 8007993, 9628431, 8214867, 9592375,
5643731, 7108804, 7796140, 9073715, 7786279, 9218450, 5852014,
7220848, 5531071, 6707757, 5325259, 7057039, 5144619, 6276074,
4919355, 6112982, 4642040, 5895631, 4766331, 5898431, 4372028,
5764096, 4425480, 5700040, 4791509, 5677859, 4351727, 6068910,
4356586, 7292655, 6665298, 7799927, 6460052, 8712420, 8969410,
13822499, 38495765, 43424278, 39621419, 44732021, 37841957, 45142206,
34591715, 41608581, 36228112, 45347955, 35407558, 39870269, 25625081,
28832164, 28159583, 30823422, 36269127, 36764400, 33333975, 33608379,
33903467, 37356045, 49753141, 51095775, 36042304, 39506987, 40017888,
40858778, 33928722, 31665538, 35496066, 34854597, 36648303, 34853318,
40974424, 37693597, 37355103, 37254712, 40888929, 39220835, 46749102,
45365054, 38692171, 38994526, 37071537, 38250689, 30771455, 30199810,
30860932, 32031393, 26897069, 27840729, 33115398, 30062000, 37198082,
34015339, 31362599, 34999206, 30190111, 29543583, 37835112, 33625379,
36564345, 32805001, 33423060, 27516038, 27951644, 25433845, 28747171,
29462452, 37615549, 32462563, 36596022, 35115114, 32025505, 33817468,
28993647, 27627406, 32829876, 29419615, 28088898, 28000767, 25990459,
25452615, 28452616, 22908176, 80282537, 44883045, 48061766, 31887840,
26450990, 22957308, 21324193, 21018404, 21640103, 21695803, 20979613,
20553114, 22796444, 21646388, 28102885, 25637043, 46302294, 30639089,
78909133, 63895259, 924877222, 574298446, 61682541, 69381555,
36784559, 44112668, 35770403, 39446356, 40450488, 46573911, 36409794,
36249387, 32861168, 33681605, 27772410, 28078711, 22718664, 21390343,
20514355, 20727964, 18684570, 18747393, 17864735, 18478176, 17042677,
18099092, 21912916, 23904073, 23854075, 25437678, 28012160, 27610597,
28921222, 27998643, 38371840, 38347990, 29883020, 31254336, 28944013,
28106503, 22333477, 25509381, 37523067, 44624151, 51398284, 51326151,
45237494, 48676641, 48884986, 36137123, 48455452, 50305940, 34113506,
30096213, 47848045, 50180241, 29558817, 27433066, 23156895, 30452971,
47913798, 81310397, 21173499, 36507223, 16705783, 44526670, 16915890,
27976765, 39421477, 97994802, 18089480, 46748560, 19213895, 68914899,
18327982, 25978185, 22752008, 26705144, 23232643, 37060652, 49410882,
115207125, 35668038, 100482133, 32216745, 37102054, 26936190,
37952923, 29880600, 59994902, 17154364, 36879989, 16187604, 18481243,
36345076, 36791199, 34979093, 33833615, 35171628, 30291521, 18008813,
18242520, 35924017, 34838177, 31498662, 30227506, 20756740, 18249859,
15655526, 16252234, 13366295, 13884202, 12628160, 12998964, 36134899,
31894894, 38682499, 42236948, 174659159, 161098060, 19201111,
23133845, 15768300, 23803944, 15563058, 21015488, 12400951, 15170894,
14463083, 15372174, 14145159, 13432976, 12508976, 14533641, 12603315,
14752532, 29165535, 34701841, 34613004, 38382305, 12701108, 15299177,
10542043, 11814855, 12720299, 14503320, 13262320, 16774322, 10999137,
13968885, 12477166, 14675299, 11295054, 14260493, 8920837, 10540879,
10821060, 12680480, 7308803, 8518389, 8708401, 9690657, 7360906,
8333873, 7872593, 9331100, 14983661, 20052686, 7071602, 8167268,
6752306, 7607500, 7300664, 8251348, 10283979, 10793896, 8633848,
9200107, 7047555, 7782679, 7026706, 8085627, 7037919, 8680326,
10692618, 11052656, 11815421, 14029510, 11321528, 11017049, 11148373,
12237342, 10273471, 10712920, 10930483, 9380573, 10664164, 9876970,
13067421, 9311465, 10683709, 8063691, 7772941, 5590061, 6222425,
4846499, 5578634, 4242595, 5323003, 3907435, 4659877, 3810184,
4686749, 3530004, 4106660, 3572372, 4192119, 3723871, 3911411,
3725753, 3844458, 3938543, 4354640, 4345201, 5141553, 4971951,
6759012, 6429379, 9578618, 8903225, 15996240, 14592373, 33416266,
30327576, 87368361, 86730343, 116182636, 147896821, 50987865,
66104255, 21879816, 25754860, 11882765, 13454804, 7726892, 8477395,
5837996, 6149962, 4994890, 5079621, 4383296, 4387098, 4258400,
4099896, 3964729, 3877924, 3337955, 3353870, 3603645, 3658213,
3117322, 3254205, 3400425, 3487749, 3909460, 3903053, 4335854,
4256376, 3558737, 3873069, 4237518, 4184615, 3879568, 4033895,
4166573, 4595958, 4272930, 4539149, 4873247, 4683438, 6452743,
7393386, 7265479, 12181806, 6763889, 7661375, 6971519, 6047856,
6438627, 5386490, 5686910, 4848516, 5979046, 4854495, 5753200,
4628045, 4952361, 4710863, 4565250, 4561258, 5486697, 5536561,
4411443, 4537075, 4755574, 4706289, 5164482, 5076590, 5073376,
7646303, 5311173, 10917625, 4101964, 5068857, 4337606, 5115597,
4564387, 4668824, 4631901, 5467250, 4723013, 5798968, 5869221,
7572800, 7765647, 10084591, 9597208, 11138673, 15000598, 15929891,
15296224, 14624702, 18227523, 16894466, 16357620, 18928804, 23857201,
26463772, 15077118, 19689309, 28562776, 38402739, 31931912, 44236189,
37961974, 47425417, 47120253, 82298168, 117287497, 147415427,
105762156, 132902413, 118794694, 148980103, 66277231, 66976258,
47955638, 59812110, 42280110, 47196037, 37788708, 38700275, 24178459,
22981247, 32673089, 32218495, 18744666, 16737281, 11989656, 10980265,
8226540, 7846527, 5285698, 4718877, 3719617, 3816493, 3296615,
3337248, 3349739, 3234481, 2725805, 3040272, 2784791, 2834818,
2905428, 3418362, 3323803, 3547666, 3424843, 2993907, 2991925,
3190753, 3698424, 3207622, 3041379, 3391014, 3548188, 3830123,
2794684, 3171099, 2976888, 3409152, 2609357, 3076370, 3448181,
3477913, 2395924, 3008964, 2935119, 2503567, 2747312, 3003776,
2345180, 2887543, 2875523, 4047590, 3544112, 3356890, 2872637,
2801455, 3385894, 2895361, 3228398, 2974132, 3224779, 2755368,
2956570, 2778855, 2732627, 2535518, 2924457, 2712460, 2918495,
2988522, 2940121, 2910417, 2799566, 2634529, 2843051, 2638672,
2841214, 2506761, 2569948, 2651799, 2272136, 2412711, 2200321,
2512456, 1782598, 2444849, 1586675, 2115228, 1662123, 2141720,
1365500, 2291167, 1541416, 2250852, 1614926, 2097689, 1651982,
2224546, 1476782, 2110319, 1613876, 2129865, 1684429, 2368080,
1842319, 2484362, 1823977, 2267909, 1636541, 2482010, 1506425,
2201026, 1704055, 2406874, 1754001, 2693560, 1958744, 2487501,
1786765, 2647321, 1762151, 2506832, 2146423, 2569078, 1847711,
2571620, 1971076, 2857411, 1825205, 2860939, 1694570, 2900597,
1658465, 3454193, 2452289, 24374736, 2562823, 27360760, 1530087,
4774473, 2406990, 18455112, 3438796, 43025599, 1892972, 5540505,
1987491, 3729814, 1960989, 3339258, 1826160, 3126293, 1841758,
3202876, 1975889, 3140266, 1784067, 2711450, 1759820, 2772621,
1890299, 2798137, 1713928, 2681236, 1810850, 2695869, 1982948,
2696095, 2046829, 2834138, 2032780, 2776086, 1883856, 2792566,
2038178, 2667824, 2516633, 2671938, 2434678, 2777007, 2121305,
3218425, 2393865, 4534850, 3026273, 3532661, 2508045, 3782342,
2733001, 3722022, 2053139, 2918697, 2583343, 3507402, 3628020,
3558825, 4760942, 4080084, 3503204, 3744204, 2564623, 3456410,
3033733, 4347196, 4794716, 9519405, 39264539, 129542795, 31952500,
90506566, 5440410, 10940205, 6986735, 42102411, 6807691, 18738039,
5727356, 6646276, 6334260, 7106678, 27528625, 25385670, 12503497,
10349194, 16741591, 15880339, 22913289, 23861758, 16395721, 20257989,
27227267, 24764297, 10987688, 9991612, 14836286, 14178754, 15889351,
15068307, 19056688, 19893617, 13550093, 74537094, 17046564, 64156917,
16928452, 18709071, 7970680, 8679154, 21885172, 16677500, 11086138,
10347864, 5777681, 6511858, 10702080, 8263870, 6414862, 5177301,
7392801, 5522991, 7632363, 5521891, 4429442, 5141246, 4437962,
4713124, 4682656, 5201962, 3357663, 4033730, 6347157, 4536040,
5162888, 5858457, 4530111, 5514569, 3705917, 4395069, 4005062,
4322744, 4137568, 4509432, 4568026, 4917769, 4246884, 4752821,
4101506, 4794941, 4716765, 5100793, 5251493, 5487365, 5151850,
5539515, 5329484, 5979635, 5693495, 6431739, 5551617, 6137586,
6717681, 7579966, 8287942, 9944413, 7973593, 9539309, 6260445,
6944790, 6639264, 7017903, 8661446, 8890185, 7452330, 7076329,
8065504, 7758935, 12986285, 12510351, 14687851, 16016589, 10811421,
10505975, 16936125, 16048723, 14235022, 14692536, 11515410, 10185673,
12366240, 10845984, 11628100, 11152221, 12104536, 12209379, 11897270,
11660802, 13841182, 10864520, 15370625, 10895724, 14362926, 9628075,
16481124, 10886762, 21453723, 11848232, 27491072, 15788705, 43149552,
33719668, 30596535, 27402730, 55715731, 36420718, 42660840, 40643469,
26369805, 20993226, 17688010, 15372860, 13853568, 10276320, 14987013,
10451967, 11915948, 8664789, 7948947, 6767177, 6562650, 5540239,
6098262, 4965553, 8351472, 5203208, 7635639, 5066968, 9317502,
5508798, 9179550, 5908849, 8243336, 5041237, 7386454, 5545817,
8637649, 5760011, 8217680, 6006850, 21881677, 12726606, 45508424,
36954717, 19157445, 23446346, 36113612, 23161366, 32250763, 30897388,
21136706, 16206069, 30222602, 19291662, 16714054, 14372993, 15650123,
10796328, 17412825, 15672118, 18807737, 17917709, 15503508, 10081708,
11417608, 7923077, 10699799, 7749773, 14386948, 9261257, 56467667,
33417304, 52890449, 48032293, 29062872, 23242018, 36501486, 25065587,
23412523, 20826889, 19066199, 13908736, 10945865, 8582446, 12192383,
6659470, 15113489, 6820906, 7424753, 5668608, 6933959, 5670777,
6195668, 5768035, 10875709, 8193418, 7811554, 11609960, 7010878,
6137633, 12463110, 12057172, 6545448, 7732166, 7126985, 6395963,
14712971, 7478643, 16245271, 11206575, 8742793, 7826046, 10455331,
6923893, 7973865, 7164603, 8153317, 7577606, 9840250, 9746034,
14425877, 26246565, 54367548, 122538954, 26109680, 40166089,
11137275, 12810374, 8641624, 9622083, 6658394, 6728764, 10523876,
6634422, 6649192, 5785925, 6190257, 5878080, 10275940, 5990167,
6047874, 19281908, 7720276, 89801987, 4375955, 12655641, 4618788,
6776033, 6682859, 6004507, 4902971, 4857628, 5543398, 4571978,
4709011, 4301051, 6197798, 7274294, 6679755, 7558111, 4087772,
4809747, 3439387, 4404121, 3981186, 4155907, 4893781, 6083104,
4977023, 6429334, 4893516, 5824296, 14311515, 20325943, 12896349,
14917987, 5651188, 5331369, 4977180, 5787633, 4094555, 4877837,
5385169, 4938339, 5704021, 5788051, 6524867, 5104170, 5125587,
4480038, 4361553, 3857693, 5522122, 4442797, 6018572, 4736688,
8359507, 5533513, 26298661, 16836405, 9494794, 12965025, 15970044,
9261835, 12597059, 12515509, 11531908, 7522031, 25978385, 15344807,
17410087, 16188748, 23840369, 15386360, 26054572, 18904748, 14266842,
12893861, 6265884, 6181915, 10717623, 6973572, 11438989, 11864686,
15902082, 10367689, 12820533, 10140250, 6767399, 5794777, 8072540,
4987108, 4407985, 4579671, 3894191, 4071378, 3300006, 3446009,
4659237, 3550968, 5111119, 3934388, 4529638, 3633981, 4401070,
3305972, 3788658, 2815157, 4379994, 2563102, 3526197, 2799465,
3371079, 2612031, 3395443, 2879536, 3167921, 3445117, 2728494,
3387837, 2777852, 4002138, 3191039, 4896911, 3064223, 3744857,
2882676, 2902579, 3192018, 3020582, 3389213, 3445934, 3991864,
3692447, 7492313, 5699982, 19672453, 10839706, 265562556, 143875100,
37315909, 46713627, 10699275, 8801800, 7451853, 7942848, 7310364,
7842462, 6046960, 6140278, 6196309, 6506569, 5342634, 5545833,
4889793, 5176057, 4779892, 5297344, 3807013, 4127983, 4305286,
4807368, 3347352, 3570671, 2987449, 3389652, 2997169, 3208945,
2894681, 3166850, 3108300, 3487693, 5614301, 6082539, 6426602,
7359362, 6252190, 7040382, 3762828, 3877266, 3362161, 3465711,
3401599, 3465280, 7227293, 6262747, 13237754, 12960688, 10409922,
8986292, 22737209, 20786940, 10695245, 11105227, 20328453, 18559200,
15286621, 16834838, 10300064, 10545877, 6270455, 6778050, 5363202,
5383261, 5010046, 5296460, 3995311, 4354900, 2590026, 3069099,
2618467, 2975881, 2437062, 2793579, 2326570, 2529690, 2170797,
2789567, 2404439, 2458120, 2523752, 2489057, 2821219, 2815891,
2334835, 2120994, 2075297, 2148385, 2051080, 2071283, 2018314,
2112460, 2251169, 1845515, 1970813, 2018929, 1746810, 1834591,
1841811, 1851426, 1970003, 1779241, 1941810, 1777824, 2109186,
1774234, 2030660, 1765702, 1858116, 1778271, 1917918, 1785178,
1828023, 1608377, 2865573, 2450871, 2389475, 1880194, 2051307,
1727171, 2902959, 2174825, 2415768, 1962911, 2516672, 2145713,
2006572, 1896725, 1994148, 1866227, 2054433, 1652913, 1645923,
1772884, 2738784, 2259827, 1719877, 2384452, 1766685, 8205262,
2198165, 69853478, 3131797, 8033983, 3482429, 3243942, 41344297,
2362982, 11974176, 2233809, 2933079, 1896241, 1910444, 1533542,
1626105, 1584269, 1696223, 1336963, 2446058, 1672874, 1844127,
1491734, 1350359, 1308638, 1239937, 1088673, 1174309, 1058759,
1568335, 1360706, 2408198, 2071746, 3011886, 2385974, 2004130,
1624216, 2493848, 1818031, 2713902, 2849376, 1720433, 1543522,
4498393, 3350395, 1777060, 2614621, 2080426, 1562895, 1902742,
2330395, 1106693, 1284215, 1076838, 859232, 1088411, 809099,
1660226, 1387403, 962019, 892591, 699096, 820620, 672778, 978114,
762233, 588511, 685934, 703018, 1317503, 835060, 740301, 683674,
596679, 840375, 790465, 868156, 654120, 577653, 701434, 896742,
645360, 703156, 600585, 673209, 628230, 701949, 555575, 650786,
554766, 507645, 581107, 639615, 562741, 698161, 666114, 749345,
395458, 598941, 569225, 495801, 453205, 510450, 536268, 486821,
341256, 577687, 512912, 594426, 785059, 845732, 4600127, 3115079,
24186388, 21174502, 11207431, 11143093, 23510245, 21492747, 4962912,
6832719, 1254627, 1343449, 6037434, 4468585, 32044645, 28029382,
10078353, 10943453, 21016001, 18035347, 19715190, 20921866, 1665130,
1850215, 685908, 666779, 500121, 529365, 543014, 453104, 723921,
728066, 541418, 573654, 251205, 429521, 228239, 483925, 210967,
549936, 403591, 426226, 321299, 274720, 403122, 442367, 366499,
408785, 327629, 526714, 272867, 442841, 207223, 488989, 100714,
442116, 311411, 338482, 634560, 231571, 864007, 663464, 414986,
693304, 215624, 348521, 214632, 247313, 284767, 380655, 335655,
302782, 237952, 234741, 119118, 290922, 56938, 228415, 145593,
229541, 79279, 219851, 114883, 229190, 253440, 231830, 298345,
343361, 659553, 583089, 5661473, 4763641, 42501347, 37465706,
19046469, 19515752, 1678340, 1700477, 666387, 699187, 570783,
576521, 418374, 314371, 330738, 240459, 280430, 291783, 270997,
164393, 213340, 257488, 204183, 251663, 143228, 231753, 314947,
284795, 462774, 463102, 655267, 557190, 992314, 626150, 281850,
270902, 218983, 228705, 205245, 127646, 177902, 265351, 531852,
483171, 504113, 533657, 178571, 399556, 429216, 341907, 339516,
547767, 302428, 524955, 259506, 303736, 298121, 279209, 705637,
839540, 1892941, 2688194, 832135, 1224368, 499247, 689864, 561686,
411460, 401568, 376855, 389534, 381450, 324276, 270848, 253953,
360355, 153729, 142692, 182576, 215206, 26731, 131634, 121065,
279575, 8378, 354559, 3350890, 6042431, 731897, 1303240, 299307,
269394, 188308, 372369, 93559, 394179, 165682, 569932, 282164,
333632, 150821, 308888, 1788277, 3701803, 216357, 334917, 133974,
148063, -19306, 217085, -40842, 186624, 99943, 272404, 161389,
356429, 203508, 193395, 36872, 232185, 95510, 134201, 52049,
190583, 180666, 270383, 351192, 559072, 99377, 176959, 80716,
287869, 94236, 158161, 271578, 79586, 12655, 160936, 168816,
284199, 173465, 189205, -46341, 50435, 129964, 191754, 83231,
56092, 25193, 162338, 107541, -22639, 55217, 35787, 47909, 63362,
53623, 98341, 84343, 83148, 68525, 281156, 200815, 325498, 7023,
211971, 147348, 173311, 131772, 82241, 57698, 55677, 125820,
75611, 141190, 206538, 121937, 132452, 109561, 285020, 288524,
161788, 218752, 339620, 376737, 461421, 788267, 629020, 1200393,
1242008, 2462325, 2464638, 3344975, 3831304, 2510363, 3074276,
1173759, 1533410, 627920, 818287, 440831, 576747, 280345, 389708,
339139, 383207, 146265, 193118, 199649, 245270, 192506, 139053,
200213, 102594, 191376, 166123, 146673, 525440, 160612, 141108,
214046, 163549, 81501, 89169, 229145, 149189, 127803, 224104,
43046, 62199, 100512, 64995, 182381, 223371, 227468, 152893,
188902, 161563, 271719, 180982, 195312, 132571, 185243, 204,
190690, 42928, 260847, 86701, 266180, 101212, 432107, 84562,
912283, 58893, 2052785, 149812, 2087275, 109099, 1221562, 212146,
505035, 155971, 366796, 208734, 274636, 157009, 220756, 39843,
123336, 140177, 51948, 85997, 186316, 159521, 73779, 142105,
233432, 163863, 103808, 213311, 175274, 122588, 223116, -70881,
51806, 95016, 199224, 129689, 16215, 14954, 83282, -58656, 64609,
-37894, 75890, 81202, 25216, -57565, 4882, 223007, -104123, -6556,
111613, 1972, 29788, 78473, 38631, 41930, 7173, -87043, 16431,
42437, 40524, 75041, -47853, -24668, -83193, 97270, 93566, -64184,
-53851, 72846, 81179, -88798, -85487, 93245, 99034, -53903, 48958,
-11602, 61371, 76051, 161268, 73033, -120141, 17482, 71106, 871,
55534, 7786, -36060, 49042, -9883, 40971, -3269, 12172, -17860,
174429, 66996, 16702, 105242, -110576, 106180, -7294, -158111,
55265, 29837, 57985, -38040, 60157, -15124, -94016, 17030, -61063,
-92350, -56016, -31862, -2691, -34232, -151965, -80909, -85343,
112232, 2900, -62024, -44420, 82929, 80607, 26812, -851, -116838,
-100528, 60697, 112403, -144841, -22094, 1512, -21703, -62315,
-173280, -5621, -5223, -138151, -18163, 107855, -46407, -831,
140759, -5251, -101338, 15117, 22505, -159721, 38653, 75320,
-18872, 49405, -50979, 37787, -84577, 91876, -56425, 82390, 46025,
-27861, -35438, -10174, -26542, -95622, -115634, 83964, -128186,
47685, -53810, 10099, 97387, 3935, 7213, -200517, 22792), .Dim = c(2L,
1406L))
The reason you have fewer items (samples) in the plot is that kohonen clusters items that are sufficiently similar. Consider the data set wines from the package. It has 13 variables for 177 wines, but the count plot shows 25 clusters. It also shows how many wines are in each cluster by the color gradient.
library(kohonen)
data(wines)
kohmap <- xyf(scale(wines), classvec2classmat(wine.classes),
grid = somgrid(5, 5, "hexagonal"), rlen=100)
plot(kohmap, type="counts", labels = wine.classes, col = wine.classes + 1, main = "count plot")
I have been fiddling around with a R project and have hit a roadblock. Ill explain what the goal is, It is to make a set of plots that show points plotted on a map based on their measurement time (water_level_date) and plot accordingly over a pre-selected time period.
The roadblock is that I have hit is that the loop creates other plots but doesn't apply the date restriction. so it produces the same plot 12 times. the loop I am currently using is
for (i in c(1:length(date)))
filename=paste(sprintf('%05d', i), ".png", sep="")
I just was a bit confused about what I should be using for the loop as I am using for() for dates and I think that its wrong, I think I have to use seq_along but I'm unsure how to get it to loop for a sequence say 01/01/1985 to 01/01/1986.
The dates are retrieved from a data frame but the issue is that it ignores the dates completely. Any help would be well appreciated. Yes, and I know I don't have a repeatable example just wondering how to properly use seq_along for a list of dates which loops between a start and end date.
DATA
structure(list(neg_decimal_lat = c(-35.9580624, -36.0270162,
-35.9505656, -35.9492215, -35.97803, -35.9766841, -35.9855039,
-35.98203, -35.984858, -36.0272577, -36.0048864, -35.9731485,
-35.9701733, -36.0034897, -36.0036909), decimal_long = c(140.3021648,
140.360413, 140.2858737, 140.2650201, 140.287947, 140.2547256,
140.2625761, 140.268055, 140.277901, 140.2702671, 140.2628329,
140.2857832, 140.2663454, 140.2683559, 140.2898351), max_drill_depth = c(67.5,
27.43, 38.4, 21.3, 18, 22, 67.1, 34, 34, 10.67, 40, 20, 16, 24,
40), water_level_date = structure(c(120L, 120L, 982L, 1320L,
1294L, 762L, 1320L, 592L, 729L, 817L, 90L, 1320L, 1320L, 202L,
631L), .Label = c("", "1/02/1972", "1/02/1985", "1/02/1996",
"1/02/2006", "1/03/2007", "1/03/2013", "1/04/1976", "1/04/1978",
"1/04/1985", "1/04/1992", "1/04/1998", "1/04/1999", "1/04/2011",
"1/05/2013", "1/06/1979", "1/06/1982", "1/06/1983", "1/06/1995",
"1/06/2000", "1/06/2006", "1/07/1985", "1/07/1986", "1/07/2003",
"1/07/2004", "1/07/2008", "1/07/2013", "1/08/1972", "1/08/1980",
"1/08/1983", "1/08/1993", "1/09/1978", "1/09/1998", "1/10/1974",
"1/10/1975", "1/10/1992", "1/10/1995", "1/10/2008", "1/10/2010",
"1/11/1974", "1/11/1991", "1/11/1999", "1/12/1971", "1/12/1976",
"1/12/1983", "1/12/2000", "1/12/2003", "1/12/2009", "10/01/1980",
"10/01/1997", "10/01/2012", "10/02/1970", "10/02/1980", "10/02/1982",
"10/03/2011", "10/04/1986", "10/04/1989", "10/04/1991", "10/04/1996",
"10/04/2002", "10/04/2013", "10/05/1969", "10/05/1988", "10/05/1989",
"10/06/1998", "10/07/1979", "10/07/1987", "10/07/2002", "10/07/2013",
"10/08/1976", "10/08/2012", "10/09/1975", "10/09/1979", "10/09/1981",
"10/09/1988", "10/09/2012", "10/10/1985", "10/10/2005", "10/10/2012",
"10/11/1969", "10/11/1970", "10/11/1981", "10/11/1988", "10/11/2000",
"10/11/2004", "10/12/1998", "10/12/2003", "10/12/2004", "10/12/2009",
"11/01/2008", "11/01/2012", "11/02/1974", "11/03/1970", "11/03/1977",
"11/03/1983", "11/03/1985", "11/03/1991", "11/03/2003", "11/04/1973",
"11/04/1975", "11/04/1991", "11/04/1994", "11/04/2013", "11/05/1973",
"11/05/1982", "11/05/1988", "11/05/2004", "11/05/2011", "11/06/1979",
"11/06/1981", "11/06/1999", "11/06/2010", "11/06/2013", "11/07/2005",
"11/07/2013", "11/08/1978", "11/08/1982", "11/08/2004", "11/09/1974",
"11/09/1978", "11/09/1981", "11/09/1996", "11/09/1997", "11/09/2006",
"11/10/1978", "11/10/1984", "11/10/1985", "11/10/1989", "11/10/2006",
"11/10/2008", "11/10/2011", "11/10/2012", "11/11/1981", "11/11/1991",
"11/12/1967", "11/12/1971", "11/12/1985", "11/12/2001", "11/12/2009",
"12/01/1979", "12/02/1975", "12/02/1979", "12/03/1971", "12/03/1981",
"12/03/1997", "12/03/1998", "12/03/2001", "12/03/2003", "12/04/1973",
"12/04/1977", "12/04/1983", "12/04/2013", "12/05/1982", "12/06/1977",
"12/06/1987", "12/06/2001", "12/06/2007", "12/06/2013", "12/07/1974",
"12/07/1983", "12/07/2010", "12/07/2013", "12/08/1991", "12/08/2002",
"12/08/2013", "12/09/1975", "12/09/1978", "12/09/1980", "12/09/1988",
"12/09/1997", "12/10/1982", "12/10/1999", "12/10/2000", "12/10/2004",
"12/10/2006", "12/11/1969", "12/11/1979", "12/11/1997", "12/12/1979",
"12/12/1988", "12/12/2003", "13/01/1989", "13/01/2013", "13/02/1981",
"13/02/1985", "13/02/1990", "13/03/1974", "13/03/1998", "13/03/2001",
"13/03/2003", "13/03/2013", "13/04/1988", "13/04/2004", "13/05/1981",
"13/05/1983", "13/06/2013", "13/07/1971", "13/07/1987", "13/07/1999",
"13/07/2010", "13/08/1974", "13/08/1998", "13/08/2013", "13/09/1974",
"13/09/1978", "13/09/1979", "13/09/1983", "13/09/1984", "13/09/1989",
"13/09/1990", "13/09/1993", "13/09/1994", "13/09/2000", "13/09/2010",
"13/10/1998", "13/10/2004", "13/10/2006", "13/10/2008", "13/10/2011",
"13/11/1996", "13/12/1996", "13/12/1999", "13/12/2001", "13/12/2004",
"14/01/1977", "14/02/1984", "14/02/1990", "14/03/1994", "14/03/2000",
"14/03/2001", "14/03/2002", "14/03/2008", "14/03/2012", "14/03/2013",
"14/04/1980", "14/04/1989", "14/05/1982", "14/05/1983", "14/05/1985",
"14/05/2002", "14/05/2010", "14/06/1967", "14/06/1968", "14/06/1989",
"14/06/1996", "14/06/2002", "14/06/2013", "14/07/1983", "14/08/1973",
"14/08/2013", "14/09/1973", "14/09/1978", "14/09/1993", "14/09/1994",
"14/09/1998", "14/09/2000", "14/09/2009", "14/10/1986", "14/10/1988",
"14/10/1997", "14/11/1962", "14/11/1984", "14/11/1986", "14/11/1996",
"14/11/2000", "14/12/1987", "14/12/1998", "14/12/1999", "14/12/2000",
"14/12/2001", "15/02/1968", "15/03/1977", "15/03/1979", "15/03/1989",
"15/03/1991", "15/03/1994", "15/03/1995", "15/03/1996", "15/03/1999",
"15/03/2000", "15/03/2001", "15/03/2003", "15/03/2010", "15/04/1999",
"15/04/2009", "15/05/1991", "15/07/1980", "15/07/1982", "15/07/2010",
"15/07/2013", "15/08/2013", "15/09/1971", "15/09/1975", "15/09/1978",
"15/09/1989", "15/09/1992", "15/09/1994", "15/09/1995", "15/09/1997",
"15/09/2000", "15/09/2003", "15/09/2009", "15/10/1993", "15/10/1996",
"15/10/2002", "15/10/2006", "15/11/1994", "15/11/1996", "15/12/1977",
"15/12/1983", "15/12/1989", "15/12/1994", "15/12/2000", "15/12/2008",
"16/01/1979", "16/01/2012", "16/02/1971", "16/02/1978", "16/02/2008",
"16/03/1977", "16/03/1979", "16/03/1989", "16/03/1991", "16/03/1993",
"16/03/1998", "16/03/2000", "16/03/2003", "16/03/2007", "16/03/2010",
"16/04/2008", "16/04/2013", "16/05/1979", "16/05/1996", "16/06/1989",
"16/06/1992", "16/06/1997", "16/06/2008", "16/07/1974", "16/07/1977",
"16/07/1982", "16/07/1996", "16/07/2008", "16/07/2013", "16/08/1988",
"16/08/1995", "16/08/2013", "16/09/1969", "16/09/1976", "16/09/1979",
"16/09/1993", "16/09/2005", "16/10/1970", "16/10/1986", "16/10/1995",
"16/10/2001", "16/10/2006", "16/10/2008", "16/11/1981", "16/12/1971",
"16/12/1979", "16/12/1980", "16/12/2003", "17/01/1974", "17/01/1980",
"17/01/1985", "17/01/2008", "17/02/1979", "17/03/1972", "17/03/1977",
"17/03/1998", "17/03/1999", "17/03/2002", "17/03/2010", "17/04/1998",
"17/04/2007", "17/04/2013", "17/05/1977", "17/05/2002", "17/05/2012",
"17/06/1977", "17/06/1986", "17/06/2002", "17/06/2013", "17/07/1996",
"17/07/2008", "17/07/2013", "17/08/1989", "17/08/1990", "17/08/1994",
"17/09/1969", "17/09/1974", "17/09/1975", "17/09/1984", "17/09/1992",
"17/09/1993", "17/09/1996", "17/09/1998", "17/09/2008", "17/10/1991",
"17/10/1994", "17/11/1983", "17/11/1997", "17/12/1985", "17/12/1986",
"17/12/1991", "17/12/1997", "17/12/1999", "17/12/2009", "17/12/2012",
"18/01/1972", "18/01/1977", "18/01/1984", "18/01/1995", "18/01/2005",
"18/01/2007", "18/02/1975", "18/02/2008", "18/03/1973", "18/03/1977",
"18/03/1996", "18/03/1998", "18/03/2009", "18/03/2010", "18/03/2013",
"18/04/1996", "18/04/2000", "18/04/2013", "18/05/1990", "18/06/2002",
"18/06/2008", "18/06/2010", "18/06/2013", "18/07/1973", "18/07/2000",
"18/08/1979", "18/08/1983", "18/09/1978", "18/09/1991", "18/09/1992",
"18/09/1996", "18/09/2000", "18/09/2012", "18/10/1991", "18/10/2001",
"18/10/2005", "18/10/2012", "18/12/1981", "18/12/1992", "18/12/2008",
"19/01/1957", "19/01/1971", "19/01/1974", "19/01/1984", "19/02/1975",
"19/02/1985", "19/03/1972", "19/03/1984", "19/03/1999", "19/03/2010",
"19/03/2013", "19/04/1971", "19/04/1996", "19/04/2000", "19/04/2006",
"19/04/2013", "19/05/1999", "19/05/2009", "19/06/1985", "19/06/1989",
"19/06/2002", "19/06/2006", "19/06/2008", "19/06/2013", "19/07/1980",
"19/07/1982", "19/07/1985", "19/07/2013", "19/08/1969", "19/08/2013",
"19/09/1973", "19/09/1975", "19/09/1977", "19/09/1978", "19/09/1979",
"19/09/1988", "19/09/2001", "19/09/2002", "19/09/2003", "19/11/1996",
"2/01/1975", "2/01/1986", "2/01/1996", "2/01/2007", "2/02/1973",
"2/02/1977", "2/02/1989", "2/03/1982", "2/03/1995", "2/04/1974",
"2/04/1976", "2/04/1980", "2/04/1984", "2/04/1985", "2/04/1996",
"2/04/2003", "2/04/2004", "2/04/2013", "2/05/1981", "2/05/2008",
"2/05/2011", "2/06/1982", "2/06/1992", "2/06/1997", "2/06/2009",
"2/06/2010", "2/07/1974", "2/07/1985", "2/07/1986", "2/07/1987",
"2/07/1999", "2/07/2008", "2/08/1971", "2/08/1993", "2/08/1996",
"2/08/2004", "2/09/1977", "2/09/1998", "2/09/2013", "2/10/1986",
"2/10/1987", "2/10/2008", "2/11/1971", "2/11/1981", "2/12/1939",
"2/12/1959", "2/12/1974", "2/12/1982", "2/12/1986", "2/12/1992",
"2/12/1998", "2/12/2011", "20/01/1983", "20/01/1984", "20/03/1974",
"20/03/1998", "20/03/1999", "20/03/2001", "20/03/2003", "20/03/2006",
"20/03/2009", "20/03/2013", "20/04/1993", "20/04/2012", "20/05/1982",
"20/05/1989", "20/06/1988", "20/06/1991", "20/06/2001", "20/06/2002",
"20/06/2005", "20/06/2007", "20/06/2008", "20/06/2013", "20/07/1973",
"20/07/1989", "20/08/1980", "20/08/1991", "20/09/1971", "20/09/1974",
"20/09/1978", "20/09/1988", "20/09/1993", "20/09/1994", "20/09/1996",
"20/09/2000", "20/09/2005", "20/09/2012", "20/10/1981", "20/10/1999",
"20/10/2000", "20/11/1995", "20/11/1996", "20/12/1983", "20/12/1991",
"20/12/2012", "21/02/2001", "21/03/1969", "21/03/1975", "21/03/1977",
"21/03/1979", "21/03/1984", "21/03/1987", "21/03/1988", "21/03/1995",
"21/03/2000", "21/03/2001", "21/03/2003", "21/03/2007", "21/03/2013",
"21/04/1980", "21/04/1981", "21/04/1990", "21/04/1999", "21/05/2012",
"21/06/2007", "21/06/2010", "21/06/2013", "21/07/1977", "21/08/1985",
"21/08/1992", "21/09/1973", "21/09/1978", "21/09/1989", "21/09/1992",
"21/09/2009", "21/10/1993", "21/11/1981", "21/11/1985", "21/11/1990",
"21/12/1953", "21/12/1981", "21/12/1990", "21/12/1995", "21/12/2000",
"21/12/2009", "21/12/2012", "22/02/1973", "22/02/1974", "22/02/1979",
"22/02/1983", "22/02/1999", "22/02/2000", "22/03/1977", "22/03/1983",
"22/03/1985", "22/03/1987", "22/03/1993", "22/03/2000", "22/03/2001",
"22/03/2004", "22/03/2011", "22/03/2012", "22/03/2013", "22/04/1969",
"22/04/1985", "22/04/1998", "22/04/2013", "22/05/1968", "22/05/1972",
"22/05/1978", "22/05/2002", "22/05/2012", "22/06/1972", "22/06/1978",
"22/06/1982", "22/06/2004", "22/06/2010", "22/07/1981", "22/07/1988",
"22/07/2010", "22/07/2013", "22/08/1973", "22/08/2001", "22/08/2013",
"22/09/1983", "22/09/1992", "22/09/1996", "22/09/1998", "22/09/1999",
"22/09/2004", "22/10/1974", "22/10/1976", "22/10/1985", "22/10/1986",
"22/10/1991", "22/10/2012", "22/11/1967", "22/11/1983", "22/11/1984",
"22/11/1995", "22/11/2012", "22/12/1982", "22/12/1983", "22/12/2011",
"23/01/1976", "23/02/1978", "23/02/1982", "23/02/2000", "23/03/1977",
"23/03/1984", "23/03/1994", "23/03/1998", "23/03/1999", "23/03/2000",
"23/03/2001", "23/03/2006", "23/03/2009", "23/03/2011", "23/04/1981",
"23/04/2007", "23/04/2013", "23/06/1998", "23/06/2005", "23/07/1979",
"23/07/2002", "23/07/2013", "23/08/1974", "23/08/1982", "23/08/1985",
"23/08/2005", "23/09/1974", "23/09/1975", "23/09/1985", "23/09/1986",
"23/09/1992", "23/09/1994", "23/09/1996", "23/09/2003", "23/09/2009",
"23/09/2010", "23/10/1979", "23/10/1986", "23/10/1989", "23/10/1991",
"23/11/1973", "23/12/1977", "23/12/1982", "24/01/1967", "24/02/2000",
"24/03/1988", "24/03/1993", "24/03/1997", "24/03/2000", "24/03/2005",
"24/04/1978", "24/04/2012", "24/04/2013", "24/05/1977", "24/05/1982",
"24/05/2002", "24/05/2006", "24/06/1974", "24/06/1981", "24/06/1982",
"24/06/1983", "24/06/1992", "24/06/2009", "24/06/2013", "24/07/1972",
"24/07/1992", "24/07/2008", "24/07/2013", "24/08/1978", "24/08/1983",
"24/09/1984", "24/09/1986", "24/09/1992", "24/09/1996", "24/09/1997",
"24/09/2008", "24/09/2009", "24/09/2011", "24/10/1973", "24/10/1979",
"24/10/1984", "24/10/1985", "24/11/1986", "24/11/1994", "25/01/1977",
"25/02/1994", "25/02/2013", "25/03/1969", "25/03/1975", "25/03/1977",
"25/03/1986", "25/03/1993", "25/03/2010", "25/03/2013", "25/04/2013",
"25/05/2002", "25/05/2007", "25/06/1984", "25/06/1985", "25/06/1999",
"25/06/2002", "25/06/2008", "25/06/2013", "25/07/1972", "25/07/1977",
"25/07/1980", "25/07/1987", "25/08/1969", "25/08/1980", "25/09/1974",
"25/09/1980", "25/09/1985", "25/09/1992", "25/09/1995", "25/09/1996",
"25/10/1976", "25/10/1980", "25/10/1984", "25/10/1991", "25/10/2005",
"25/10/2007", "25/10/2010", "25/11/1971", "25/11/1985", "26/02/1994",
"26/02/2013", "26/03/1977", "26/03/1978", "26/03/1980", "26/03/1985",
"26/03/1992", "26/03/2001", "26/04/2011", "26/05/2010", "26/06/1969",
"26/06/1985", "26/06/2001", "26/06/2002", "26/06/2003", "26/06/2013",
"26/07/1990", "26/07/1999", "26/07/2002", "26/07/2013", "26/08/1977",
"26/08/1980", "26/08/1996", "26/08/2013", "26/09/1974", "26/09/1978",
"26/09/1989", "26/09/1995", "26/09/2001", "26/09/2005", "26/09/2011",
"26/10/1978", "26/10/1985", "26/10/1993", "26/10/2005", "26/10/2010",
"26/11/1971", "26/11/1984", "27/01/1988", "27/02/1984", "27/02/2003",
"27/02/2013", "27/03/1984", "27/03/1992", "27/03/1995", "27/03/1998",
"27/03/2001", "27/03/2003", "27/03/2012", "27/03/2013", "27/04/1971",
"27/05/1976", "27/06/1978", "27/06/1982", "27/06/1985", "27/06/1986",
"27/06/2006", "27/06/2013", "27/07/1971", "27/07/1983", "27/07/1995",
"27/08/2012", "27/09/1973", "27/09/1985", "27/09/1988", "27/09/1993",
"27/09/1995", "27/09/1999", "27/09/2002", "27/09/2005", "27/09/2011",
"27/10/2006", "27/11/1997", "28/01/1976", "28/01/1983", "28/02/2002",
"28/02/2013", "28/03/1977", "28/03/1982", "28/03/1983", "28/03/1990",
"28/03/2000", "28/03/2003", "28/03/2006", "28/03/2012", "28/04/1976",
"28/04/1977", "28/04/1978", "28/04/1983", "28/04/1987", "28/04/2002",
"28/05/1980", "28/06/1977", "28/06/1985", "28/06/2006", "28/06/2013",
"28/07/1982", "28/08/1978", "28/08/1991", "28/08/2012", "28/09/1992",
"28/09/2000", "28/09/2006", "28/09/2011", "28/10/1985", "28/10/1986",
"28/10/1991", "28/10/1993", "28/11/1976", "28/11/1979", "28/11/2001",
"29/01/1953", "29/01/1969", "29/01/1998", "29/03/1974", "29/03/1977",
"29/03/1979", "29/03/1993", "29/03/1999", "29/03/2011", "29/03/2012",
"29/04/1977", "29/04/1978", "29/04/2002", "29/04/2009", "29/05/1978",
"29/05/2013", "29/06/1984", "29/06/2005", "29/07/1975", "29/07/1980",
"29/07/1996", "29/07/2013", "29/08/1978", "29/08/1985", "29/09/1978",
"29/09/1983", "29/09/1992", "29/09/1999", "29/09/2009", "29/10/1976",
"29/10/1985", "29/10/1986", "29/10/1987", "29/10/1995", "29/10/2002",
"29/10/2004", "29/11/1995", "3/01/1975", "3/01/2007", "3/02/1976",
"3/02/1989", "3/02/1992", "3/02/1997", "3/02/1999", "3/03/1969",
"3/03/1982", "3/03/2003", "3/03/2004", "3/03/2008", "3/03/2010",
"3/04/1992", "3/04/1996", "3/04/1997", "3/04/2002", "3/04/2003",
"3/04/2012", "3/04/2013", "3/05/1973", "3/05/1990", "3/06/1976",
"3/06/1982", "3/06/1993", "3/07/1986", "3/07/1996", "3/07/2002",
"3/08/1975", "3/09/1975", "3/09/1985", "3/09/1992", "3/09/1996",
"3/09/2007", "3/10/1973", "3/10/1975", "3/10/1979", "3/10/1980",
"3/10/1988", "3/10/1989", "3/10/1990", "3/10/2006", "3/11/1968",
"3/11/1970", "3/11/1986", "3/12/1981", "3/12/1982", "3/12/1993",
"3/12/1997", "3/12/2002", "30/01/1999", "30/01/2006", "30/01/2007",
"30/03/1977", "30/03/1992", "30/03/2001", "30/03/2011", "30/03/2012",
"30/04/1956", "30/04/1980", "30/04/1998", "30/04/2001", "30/04/2004",
"30/04/2010", "30/04/2011", "30/05/1995", "30/05/2000", "30/06/1976",
"30/06/2004", "30/06/2009", "30/07/1980", "30/07/1982", "30/07/1984",
"30/07/2001", "30/07/2013", "30/08/1976", "30/08/1978", "30/08/1982",
"30/08/1985", "30/08/1989", "30/08/2002", "30/09/1971", "30/09/1974",
"30/09/1980", "30/09/1992", "30/09/1998", "30/09/2011", "30/10/1978",
"30/10/1986", "30/10/1991", "30/10/1995", "30/10/2001", "30/10/2003",
"30/10/2006", "30/11/1978", "30/11/1995", "30/11/2010", "30/11/2012",
"31/01/1980", "31/01/1991", "31/01/1997", "31/01/2006", "31/01/2013",
"31/03/1977", "31/03/1992", "31/03/1995", "31/03/1998", "31/07/1986",
"31/07/2013", "31/08/1978", "31/08/1979", "31/08/1982", "31/08/1983",
"31/08/1993", "31/08/1999", "31/08/2012", "31/10/1984", "31/10/1986",
"31/10/2006", "31/12/1982", "4/01/1982", "4/01/1990", "4/02/1970",
"4/02/1980", "4/03/1971", "4/03/1994", "4/03/1996", "4/03/1998",
"4/03/1999", "4/03/2013", "4/04/1974", "4/04/1991", "4/04/2003",
"4/04/2005", "4/05/1981", "4/05/1982", "4/05/1985", "4/05/2004",
"4/05/2010", "4/06/1993", "4/06/1997", "4/06/1998", "4/06/1999",
"4/06/2002", "4/06/2003", "4/06/2013", "4/07/1985", "4/07/1986",
"4/07/2002", "4/07/2005", "4/08/1977", "4/08/1982", "4/08/1983",
"4/08/1986", "4/09/1978", "4/10/1968", "4/10/1971", "4/10/1974",
"4/10/1977", "4/10/1982", "4/10/1996", "4/10/2001", "4/10/2012",
"4/11/1977", "4/12/1976", "4/12/2000", "4/12/2006", "4/12/2007",
"4/12/2008", "5/01/1983", "5/02/1965", "5/02/2009", "5/03/1974",
"5/03/1981", "5/03/1987", "5/03/2002", "5/03/2010", "5/03/2013",
"5/04/2001", "5/05/1981", "5/05/2010", "5/06/1969", "5/06/1985",
"5/06/2013", "5/07/1975", "5/07/2007", "5/07/2012", "5/07/2013",
"5/08/1986", "5/08/2002", "5/08/2004", "5/09/1977", "5/09/1978",
"5/09/1991", "5/10/1993", "5/10/2005", "5/10/2011", "5/11/1969",
"5/11/1979", "5/11/1981", "5/12/1972", "5/12/1973", "5/12/2006",
"5/12/2012", "6/01/1994", "6/01/2012", "6/02/1970", "6/02/2006",
"6/03/1989", "6/03/2000", "6/03/2001", "6/03/2003", "6/03/2012",
"6/04/1978", "6/04/1994", "6/04/1998", "6/04/2000", "6/04/2009",
"6/05/1969", "6/05/2003", "6/06/1984", "6/06/2013", "6/07/1977",
"6/07/2007", "6/08/1967", "6/08/1973", "6/08/1982", "6/08/2013",
"6/09/1973", "6/09/1977", "6/09/1978", "6/09/1988", "6/09/1999",
"6/09/2005", "6/09/2011", "6/10/1983", "6/10/1985", "6/10/1986",
"6/10/1987", "6/10/1988", "6/10/1993", "6/10/2004", "6/10/2011",
"6/11/1976", "6/11/1981", "6/12/1977", "7/01/1986", "7/02/1995",
"7/02/2006", "7/03/1974", "7/03/1978", "7/03/1979", "7/03/1983",
"7/03/1985", "7/03/1989", "7/03/1991", "7/03/1997", "7/03/2000",
"7/03/2005", "7/03/2011", "7/04/1976", "7/04/1979", "7/04/1986",
"7/04/1987", "7/04/1998", "7/04/1999", "7/04/2005", "7/04/2006",
"7/04/2008", "7/05/1975", "7/05/2010", "7/05/2012", "7/06/1971",
"7/06/1995", "7/07/1977", "7/07/1982", "7/07/1986", "7/07/2010",
"7/08/1980", "7/09/1978", "7/09/1989", "7/09/1999", "7/10/1971",
"7/10/1977", "7/10/1987", "7/10/1993", "7/10/2003", "7/10/2008",
"7/11/1970", "7/11/1980", "7/11/1984", "7/11/2000", "7/11/2001",
"7/11/2002", "7/11/2006", "7/12/1976", "7/12/1978", "7/12/2000",
"7/12/2005", "7/12/2012", "8/02/1968", "8/02/1990", "8/03/1974",
"8/03/1978", "8/03/1979", "8/03/1988", "8/03/1989", "8/03/1993",
"8/03/1994", "8/03/1995", "8/03/2001", "8/03/2003", "8/03/2013",
"8/04/1981", "8/04/1982", "8/04/2008", "8/04/2009", "8/04/2010",
"8/04/2013", "8/05/1981", "8/05/1986", "8/06/1979", "8/06/2001",
"8/07/1975", "8/07/1982", "8/07/2010", "8/07/2013", "8/08/1990",
"8/08/2009", "8/08/2013", "8/09/1978", "8/09/1988", "8/09/1997",
"8/09/2000", "8/09/2006", "8/10/1985", "8/10/1986", "8/10/2002",
"8/10/2003", "8/10/2009", "8/10/2012", "8/11/1970", "8/11/1996",
"8/11/2000", "8/11/2002", "8/12/1977", "8/12/1987", "8/12/1988",
"8/12/1995", "9/01/1971", "9/01/1985", "9/01/2012", "9/02/1970",
"9/02/1982", "9/02/1990", "9/02/1996", "9/03/1982", "9/03/1988",
"9/03/2009", "9/03/2010", "9/04/1986", "9/04/1991", "9/04/1996",
"9/04/2013", "9/05/2012", "9/07/2008", "9/07/2013", "9/08/1991",
"9/08/2002", "9/08/2013", "9/09/1975", "9/09/1982", "9/09/1993",
"9/09/1997", "9/09/1998", "9/09/2008", "9/10/1991", "9/10/1996",
"9/10/2003", "9/10/2012", "9/11/1976", "9/11/1992", "9/11/1999",
"9/12/1988", "9/12/1997"), class = "factor")), .Names = c("neg_decimal_lat",
"decimal_long", "max_drill_depth", "water_level_date"), row.names = c(NA,
15L), class = "data.frame")
The code is as follows
require(ggplot2)
require('ggmap')
plotdata <- plotdata[1:15, c("neg_decimal_lat", "decimal_long",
"max_drill_depth", "water_level_date")]
date <- factor(plotdata$water_level_date,
levels=plotdata$water_level_date[!duplicated(plotdata$water_level_date)])
colormap <- c("darkblue","blue","lightblue", "green",
"yellow", "orange","darkorange", "red", "darkred")
depth <- c(1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50,60,70,80,90,100,110, #legendrange
120,130,140,150,160,170,180,190, 200, 300, 400,500,600, 700,
800,900, 1000,1100,1200,1300,1400, 1500, 2400)
# static map drawn based on min max of latlong
map.plot<- get_map(location = c(min(plotdata$decimal_long),
min(plotdata$neg_decimal_lat),
max(plotdata$decimal_long),
max(plotdata$neg_decimal_lat)),
source = "osm", zoom=6)
theme_set(theme_bw(base_size = 8))
static.map <- ggmap(map.plot) %+% plotdata +
aes(x = decimal_long,
y = neg_decimal_lat,
z = max_drill_depth) +
stat_summary2d(fun = median, binwidth = c(.01, .01),alpha = 1) +
scale_fill_gradientn(name = "Drill Depth",colours = colormap, breaks=depth,
labels = format(depth), space = "Lab") +
labs(x = "Longitude",y = "Latitude" )+ coord_map()
Dates <- seq(as.Date("1985/1/1"), as.Date("1995/1/1"), "years")
#current loop for date
for (i in seq_along(Dates)){
filename=paste(sprintf('%05d', i), ".png", sep="")
print(static.map)
ggsave(filename=filename,
plot = static.map,
scale = 4,
width = 5, height = 3,
dpi = 300)
}
The overall aim is to get it for example to draw all points that were recorded in 1985-1986 and exclude all others, but also showing each month and the points are drawn at their specific month. These would be made into 12 images. Also watch out as the loop is bugged and keeps printing more of the same map so don't forget to stop it.
A reproducible example would help. It is not clear what the problem is.
This seems to work:
Dates <- seq(as.Date("1985/1/1"), as.Date("1995/1/1"), "years")
for (i in seq_along(Dates)) {
filename=paste(sprintf('%05d', i), ".png", sep="")
print(filename)
}
Is there a way to force
align.plots()
(ggExtra) to produce a plot where the "individual" y-axis are of the same height/length?
Plot1:
kurth.plot <- qplot(x=trade.date, y=kurth, data=finalregstack, geom='line')+scale_y_continuous(name='')+scale_x_date(name='', expand=c(0,0))+opts(panel.background=theme_rect(),panel.grid.major=theme_blank(), panel.grid.minor=theme_blank(), plot.margin = unit(c(1,0,0,0), "lines"))
Plot2:
skewh.plot <- qplot(x=trade.date, y=skewh, data=finalregstack, geom='line')+scale_y_continuous(name='')+scale_x_date(name='', expand=c(0,0))+opts(panel.background=theme_rect(),panel.grid.major=theme_blank(), panel.grid.minor=theme_blank(),plot.margin = unit(c(-1,0,0,0), "lines"))
Plot2:
logpremh.plot <- qplot(x=trade.date, y=logpremh, data=finalregstack, geom='line')+scale_y_continuous(name='')+scale_x_date(name='', expand=c(0,0))+opts(panel.background=theme_rect(),panel.grid.major=theme_blank(), panel.grid.minor=theme_blank(), plot.margin = unit(c(-1,0,0,0), "lines"))
All from the same data frame (finalregstack), with trade.date one the x-axis.
align.plots(kurth.plot, skewh.plot, logpremh.plot)
returns at plot where the y-axis are of different height. Would like to have three plots with the same size, and ticks and axis.text on all the x-axis...
finalregstack:
structure(list(trade.date = structure(c(11869, 11870, 11871,
11876, 11877, 11878, 11879, 11880, 11883, 11884, 11885, 11886,
11887, 11890, 11891, 11892, 11893, 11894, 11897, 11898, 11899,
11900, 11901, 11904, 11905, 11906, 11907, 11908, 11911, 11912,
11913, 11914, 11915, 11918, 11919, 11920, 11921, 11922, 11926,
11927, 11928, 11929, 11933, 11934, 11935, 11936, 11939, 11940,
11941, 11942, 11943, 11946, 11947, 11948, 11949, 11950, 11953,
11954, 11955, 11956, 11957, 11960, 11961, 11962, 11963, 11964,
11967, 11968, 11969, 11970, 11971, 11974, 11975, 11976, 11977,
11978, 11981, 11982, 11983, 11984, 11985, 11988, 11989, 11990,
11991, 11992, 11995, 11996, 11997, 11998, 11999, 12002, 12003,
12004, 12005, 12006, 12009, 12010, 12011, 12012), class = "Date"),
kurth = c(-0.538313927970605, -0.489599831389002, -0.549781828821448,
-0.471480517120967, -0.77047442140922, -0.764965969697188,
-0.797709358466937, -0.832263316808549, -0.739745115199957,
-0.737998397925412, -0.67595581513572, -0.765405764054348,
-0.901490974527926, -0.89225716170503, -0.939998163588295,
-0.758003571622454, -0.947725995002572, -1.00684152882085,
-1.00573304351807, -0.934273517386361, -0.859399051665602,
-0.942893064943436, 2.47305763864541, 2.06181470542550, 1.56582316347337,
1.19831125493319, 1.22311352859957, 1.15155464086347, 8.89610455063215,
8.86956391322178, 8.68580530405464, 8.61123904123055, 8.7400669250269,
8.79737625016586, 8.78629168919513, 8.29623941128428, 8.05754059674136,
8.02077532530273, 8.23011985866437, 8.24401447203093, 7.92294576051403,
7.4281887928507, 7.3426056371313, 7.30433477318178, 7.32670525202146,
7.17139705328142, 7.25310778701676, 7.1313581631237, 6.90092496220942,
6.81977869235276, 8.7728390134954, 9.0300473239695, 9.86979682886692,
10.0467584323277, 9.18063174440517, 9.60705497699159, -0.834707723641238,
-0.831476742135092, -0.683003538196646, -0.128689206047591,
-0.0486840773604658, -0.193928790670311, -0.372070480951705,
1.24156083387007, 2.16386195607461, 1.94481670179387, 1.87512691616447,
1.60466257254115, 1.58162121662634, 1.82406671267336, 2.14439006082267,
2.11326738777539, 1.84632151012893, 1.81703000912427, 1.70683982987174,
1.52156804636807, 1.37669564606729, 1.34356392851010, 1.38192685184208,
0.979515684470376, 0.888891137016543, 0.675602543012428,
0.661040087943164, 0.806562602010497, 1.03315305152296, 0.78156186596084,
0.8053103693573, 0.826531073748131, 0.825522225795727, 0.898444393507555,
1.10994069195078, 1.10994069195078, 1.10994069195078, 1.1364451896438,
1.09982566285141, 1.85866580040354, -0.351810796009023, -0.096669160154403,
-0.492681631027265, -0.288429330247108), skewh = c(0.558228477951341,
0.58308901843335, 0.609602375964546, 0.477880650885013, 0.387805408324395,
0.387839989209099, 0.286456452315072, 0.193331879936383,
0.206591453689309, 0.0971220953017504, 0.221147814384501,
0.187595125737881, 0.0524187756954932, 0.0961716860713602,
0.182338727065898, 0.264757353490630, 0.163830324947476,
0.143991871613884, 0.144463923069265, 0.0730550579677163,
0.183757658876955, 0.0656791648713786, 1.18950139112842,
1.09434539636253, 0.802846925891693, 0.63602979484928, 0.672813293573144,
0.736614149819276, -2.33563391004889, -2.31143640895283,
-2.31727166871707, -2.32876897713447, -2.38781198320570,
-2.37160833347724, -2.33074692187816, -2.24219936843415,
-2.21001773133659, -2.17823730104347, -2.2105667535483, -2.22156866044437,
-2.19942975461446, -2.07253305889856, -2.0419313498378, -2.02266448996358,
-1.97786014003463, -1.95408902307469, -1.98284250481692,
-1.93088774946092, -1.92119697583021, -1.88750434547032,
-2.45890556022003, -2.53154407812213, -2.67753652485642,
-2.70481025800469, -2.54591006187052, -2.62989769761383,
0.247330549690984, 0.228815399584095, 0.227991391662379,
0.571885460253403, 0.689877958381067, 0.533755075074511,
0.400111197818491, -0.535379813307516, -1.12355112329518,
-1.11402405119459, -1.06419661476207, -1.01542648507456,
-0.98798275182458, -1.01968536538631, -1.13136880338172,
-1.11658585003117, -1.08702903755025, -1.07310996361807,
-1.04443252316148, -0.98146201617556, -0.989718695652915,
-0.978648824220157, -0.93936499984734, -0.842000316650154,
-0.856125603005716, -0.752318760987758, -0.743850925871073,
-0.847638348832287, -0.918625055313669, -0.846416731547665,
-0.864256826872963, -0.890095785952014, -0.892616437941412,
-0.867123449112882, -0.936966060328, -0.936966060328, -0.936966060328,
-0.758598109998537, -0.682579399329156, -0.619814484766827,
0.620814547041089, 0.725657661522822, 0.592805261790067,
0.721071190789906), logpremh = c(-0.995610217466355, -0.70000069009059,
-0.678144103868504, -0.678144103868504, -0.494505232880775,
-0.484213835616396, -0.495268057973604, -0.372900957639147,
-0.450480847761974, -0.437795043597507, -0.517585169818772,
-0.430739184642212, -0.415173125239592, -0.313292951895139,
-0.552202213743963, -0.646341796497822, -0.715263921701793,
-0.660538105883145, -0.64253792833408, -0.491943429630031,
-0.605111281016567, -0.687871516304753, -0.819398672581242,
-0.720843146615785, -0.554697314678323, -0.604358822116492,
-0.635963319554632, -0.58688762990502, -0.533766579673723,
-0.549540818233004, -0.509744651370597, -0.497574488575739,
-0.50295497641327, -0.465150684081682, -0.471107502191422,
-0.484308973579098, -0.519413594601966, -0.409658720851543,
-0.409658720851543, -0.312434559177703, -0.240594651315486,
-0.187647447055703, -0.187647447055703, -0.431835237434803,
-0.492239335398449, -0.601873889653004, -0.847778497243267,
-1.28468760005568, -1.34064083106371, -1.32738794981778,
-1.43946408093213, -1.66269758662786, -1.56874565640878,
-1.68389571629343, -1.57211119569467, -1.49267822488204,
-1.42922611404924, -1.74337835169184, -1.75526498666775,
-1.65718139709817, -1.49798628913073, -1.3162509752857, -1.25568570523457,
-1.32158185197453, -1.32751736653430, -1.79028679759309,
-1.68133286507375, -1.59210428304230, -0.358448286780251,
-0.282320721989257, -0.222935326576976, -0.140064751669158,
-0.0652636984938439, -0.0843461362304372, -0.065141838860399,
-0.0424518787941153, 0.0550573562398911, -0.190765741146921,
-0.269701195073448, -0.173665980472658, -0.0334693122151012,
-0.539434660595658, -0.376468992885012, -0.446138926996241,
-0.368295712776361, -0.203379346763717, -0.123717788393908,
-0.0460878584714188, -0.463198358966021, -0.726046498313927,
-1.00170780286485, -0.831902448806796, -0.804806611101685,
-0.787069116472282, -0.97805437572716, -0.908526799872866,
-0.735307225195469, -0.696862274769597, -0.635797410804945,
-0.514329954731469)), .Names = c("trade.date", "kurth", "skewh",
"logpremh"), row.names = c("2002-07-01", "2002-07-02", "2002-07-03",
"2002-07-08", "2002-07-09", "2002-07-10", "2002-07-11", "2002-07-12",
"2002-07-15", "2002-07-16", "2002-07-17", "2002-07-18", "2002-07-19",
"2002-07-22", "2002-07-23", "2002-07-24", "2002-07-25", "2002-07-26",
"2002-07-29", "2002-07-30", "2002-07-31", "2002-08-01", "2002-08-02",
"2002-08-05", "2002-08-06", "2002-08-07", "2002-08-08", "2002-08-09",
"2002-08-12", "2002-08-13", "2002-08-14", "2002-08-15", "2002-08-16",
"2002-08-19", "2002-08-20", "2002-08-21", "2002-08-22", "2002-08-23",
"2002-08-27", "2002-08-28", "2002-08-29", "2002-08-30", "2002-09-03",
"2002-09-04", "2002-09-05", "2002-09-06", "2002-09-09", "2002-09-10",
"2002-09-11", "2002-09-12", "2002-09-13", "2002-09-16", "2002-09-17",
"2002-09-18", "2002-09-19", "2002-09-20", "2002-09-23", "2002-09-24",
"2002-09-25", "2002-09-26", "2002-09-27", "2002-09-30", "2002-10-01",
"2002-10-02", "2002-10-03", "2002-10-04", "2002-10-07", "2002-10-08",
"2002-10-09", "2002-10-10", "2002-10-11", "2002-10-14", "2002-10-15",
"2002-10-16", "2002-10-17", "2002-10-18", "2002-10-21", "2002-10-22",
"2002-10-23", "2002-10-24", "2002-10-25", "2002-10-28", "2002-10-29",
"2002-10-30", "2002-10-31", "2002-11-01", "2002-11-04", "2002-11-05",
"2002-11-06", "2002-11-07", "2002-11-08", "2002-11-11", "2002-11-12",
"2002-11-13", "2002-11-14", "2002-11-15", "2002-11-18", "2002-11-19",
"2002-11-20", "2002-11-21"), class = "data.frame")
Personally, I would use facetting to solve this, not aligning plots. This means you can let ggplot decide what the y-axis values should be. It also makes for much more readable code
library(reshape2)
finalregstack$trade.date <- 1:100
mregstack <- melt(finalregstack, id.vars="trade.date")
ggplot(mregstack, aes(x=trade.date, y=value, colour=variable)) + geom_line() +
facet_grid(variable~.) + theme_bw()
How about
library(reshape2)
finalregstack$trade.date <- as.Date(rownames(finalregstack))
fm <- melt(finalregstack,id.var="trade.date")
library(ggplot2)
qplot(trade.date,value,data=fm,geom="line")+
facet_grid(variable~.,scale="free_y")+
labs(x="Trading date",y="")+
theme_bw() ## cosmetic
updated using reshape2 and scale="free_y"
The only real problem with this solution (which is fairly difficult to fix) is that the individual y-axis labels come out as strips on the right, not as traditional axis labels on the left. If you can convince your readers to look on the left ...
If you really need to do this via align.plot (because you need the ticks etc.), the most recent version of align.plots (which I grabbed from the googlecode repository) has a heights argument. I started to mess around with this but haven't got it working to my liking yet ...
library(ggExtra)
theme_set(theme_bw())
theme_update(panel.background=theme_rect(),
panel.grid.major=theme_blank(),
panel.grid.minor=theme_blank())
kurth.plot <- qplot(x=trade.date, y=kurth,
data=finalregstack, geom='line')+
scale_y_continuous(name='kurth')+
scale_x_date(name='',expand=c(0,0))+
opts(plot.margin = unit(c(1,0,0,0), "lines"))
skewh.plot <- qplot(x=trade.date, y=skewh,
data=finalregstack, geom='line')+
scale_y_continuous(name='skewh')+
scale_x_date(name='', expand=c(0,0))+
opts(plot.margin = unit(c(-1,0,0,0), "lines"))
logpremh.plot <- qplot(x=trade.date, y=logpremh,
data=finalregstack, geom='line')+
scale_y_continuous(name='logpremh')+
scale_x_date(name='Trading date', expand=c(0,0))+
opts(plot.margin = unit(c(-1,0,0,0), "lines"))
source(url("http://ggextra.googlecode.com/svn/trunk/R/align.r"))
grid.newpage()
align.plots(kurth.plot, skewh.plot, logpremh.plot,
heights=unit(c(0.33,0.33,0.33),"null"))
By the way: since all your plots have the same x axis, is there a possibility that grid.arrange would work for you? align.plots is designed for fussier situations (e.g. some plots have strips/legends/etc. and others don't).