Related
Yesterday I asked this question about drawing a line that connects two given points using add_lines() from plotly. That makes me think about some other visualization aspects I want to give to my graph: to connect two given points and extend the line across the whole x-axis.
This is my current database:
dput(sma)
structure(list(time = structure(c(1640808000, 1640822400, 1640836800,
1640851200, 1640865600, 1640880000, 1640894400, 1640908800, 1640923200,
1640937600, 1640952000, 1640966400, 1640980800, 1640995200, 1641009600,
1641024000, 1641038400, 1641052800, 1641067200, 1641081600, 1641096000,
1641110400, 1641124800, 1641139200, 1641153600, 1641168000, 1641182400,
1641196800, 1641211200, 1641225600, 1641240000, 1641254400, 1641268800,
1641283200, 1641297600, 1641312000, 1641326400, 1641340800, 1641355200,
1641369600, 1641384000, 1641398400, 1641412800, 1641427200, 1641441600,
1641456000, 1641470400, 1641484800, 1641499200, 1641513600, 1641528000,
1641542400, 1641556800, 1641571200, 1641585600, 1641600000, 1641614400,
1641628800, 1641643200, 1641657600, 1641672000, 1641686400, 1641700800,
1641715200, 1641729600, 1641744000, 1641758400, 1641772800, 1641787200,
1641801600, 1641816000, 1641830400, 1641844800, 1641859200, 1641873600,
1641888000, 1641902400, 1641916800, 1641931200, 1641945600, 1641960000,
1641974400, 1641988800, 1642003200, 1642017600, 1642032000, 1642046400,
1642060800, 1642075200, 1642089600, 1642104000, 1642118400, 1642132800,
1642147200, 1642161600, 1642176000, 1642190400, 1642204800, 1642219200,
1642233600, 1642248000, 1642262400, 1642276800, 1642291200, 1642305600,
1642320000, 1642334400, 1642348800, 1642363200, 1642377600, 1642392000,
1642406400, 1642420800, 1642435200, 1642449600, 1642464000, 1642478400,
1642492800, 1642507200, 1642521600, 1642536000, 1642550400, 1642564800,
1642579200, 1642593600, 1642608000, 1642622400, 1642636800, 1642651200,
1642665600, 1642680000, 1642694400, 1642708800, 1642723200, 1642737600,
1642752000, 1642766400, 1642780800, 1642795200, 1642809600, 1642824000,
1642838400, 1642852800, 1642867200, 1642881600, 1642896000, 1642910400,
1642924800, 1642939200, 1642953600, 1642968000, 1642982400, 1642996800,
1643011200, 1643025600, 1643040000, 1643054400, 1643068800, 1643083200,
1643097600, 1643112000, 1643126400, 1643140800, 1643155200, 1643169600,
1643184000, 1643198400, 1643212800, 1643227200, 1643241600, 1643256000,
1643270400, 1643284800, 1643299200, 1643313600, 1643328000, 1643342400,
1643356800, 1643371200, 1643385600, 1643400000, 1643414400, 1643428800,
1643443200, 1643457600, 1643472000, 1643486400, 1643500800, 1643515200,
1643529600, 1643544000, 1643558400, 1643572800, 1643587200, 1643601600,
1643616000, 1643630400, 1643644800, 1643659200, 1643673600, 1643688000,
1643702400, 1643716800, 1643731200, 1643745600, 1643760000, 1643774400,
1643788800, 1643803200, 1643817600, 1643832000, 1643846400, 1643860800,
1643875200, 1643889600, 1643904000, 1643918400, 1643932800, 1643947200,
1643961600, 1643976000, 1643990400, 1644004800, 1644019200, 1644033600
), class = c("POSIXct", "POSIXt"), tzone = ""), open = c(0.12428,
0.12167, 0.11791, 0.12133, 0.124, 0.12245, 0.12375, 0.12392,
0.12379, 0.12166, 0.1239, 0.12416, 0.11915, 0.12107, 0.12243,
0.12321, 0.12216, 0.12488, 0.12839, 0.12815, 0.12759, 0.12704,
0.12746, 0.12669, 0.12634, 0.12571, 0.12455, 0.12455, 0.12861,
0.12556, 0.12244, 0.12281, 0.12175, 0.12213, 0.12338, 0.12431,
0.12216, 0.1217, 0.12407, 0.12479, 0.12338, 0.12365, 0.11492,
0.11013, 0.10664, 0.10831, 0.10642, 0.10901, 0.10986, 0.10979,
0.10398, 0.1033, 0.104, 0.10127, 0.10275, 0.10207, 0.10357, 0.10443,
0.10514, 0.10271, 0.09677, 0.10175, 0.10111, 0.10103, 0.10221,
0.10152, 0.10517, 0.10355, 0.10461, 0.10576, 0.10419, 0.09702,
0.09658, 0.09847, 0.10165, 0.10064, 0.0999, 0.10119, 0.10199,
0.10152, 0.10233, 0.10215, 0.10273, 0.1059, 0.10571, 0.10564,
0.10405, 0.10608, 0.10475, 0.10286, 0.10122, 0.10018, 0.10022,
0.10127, 0.09859, 0.10011, 0.10054, 0.10066, 0.10147, 0.10333,
0.10762, 0.11317, 0.11409, 0.11525, 0.11243, 0.11022, 0.10932,
0.11007, 0.10968, 0.11175, 0.11387, 0.11404, 0.11295, 0.10842,
0.10746, 0.10779, 0.11022, 0.11553, 0.11481, 0.11341, 0.1146,
0.11565, 0.11529, 0.11192, 0.11229, 0.11194, 0.11081, 0.10661,
0.10554, 0.10666, 0.10636, 0.10864, 0.10741, 0.09956, 0.09362,
0.09521, 0.09495, 0.0936, 0.09223, 0.08409, 0.0828, 0.07273,
0.07066, 0.07008, 0.0701, 0.072, 0.0711, 0.07424, 0.07467, 0.07324,
0.07124, 0.07559, 0.07096, 0.06866, 0.06469, 0.06602, 0.06853,
0.07014, 0.06903, 0.06895, 0.07152, 0.0715, 0.07204, 0.07213,
0.07263, 0.07346, 0.07471, 0.07629, 0.0757, 0.07368, 0.07013,
0.07135, 0.07329, 0.07601, 0.07396, 0.07423, 0.07524, 0.074,
0.0731, 0.0754, 0.07527, 0.07648, 0.07666, 0.07782, 0.07804,
0.07598, 0.07785, 0.07924, 0.07924, 0.0803, 0.07938, 0.07773,
0.07475, 0.07529, 0.07172, 0.07102, 0.07118, 0.07292, 0.07386,
0.07367, 0.07504, 0.07506, 0.07524, 0.0764, 0.07571, 0.07642,
0.07676, 0.07632, 0.0777, 0.07355, 0.07462, 0.07218, 0.07176,
0.07262, 0.07203, 0.07313, 0.0723, 0.07433, 0.075, 0.07626, 0.07713,
0.07766, 0.07823, 0.07916, 0.08149), high = c(0.1249, 0.1219,
0.1221, 0.12449, 0.12468, 0.12469, 0.125, 0.12459, 0.12419, 0.12509,
0.1247, 0.12435, 0.12219, 0.12314, 0.12337, 0.12379, 0.1257,
0.13202, 0.12864, 0.12884, 0.12778, 0.12824, 0.1275, 0.12846,
0.12643, 0.12586, 0.12611, 0.1293, 0.1291, 0.12598, 0.12539,
0.12323, 0.12241, 0.1235, 0.1258, 0.12597, 0.1243, 0.12522, 0.12494,
0.12527, 0.1252, 0.12372, 0.11609, 0.11294, 0.10881, 0.10838,
0.11043, 0.11073, 0.1107, 0.11156, 0.1046, 0.10593, 0.10532,
0.10439, 0.10434, 0.10447, 0.10498, 0.10556, 0.10518, 0.1035,
0.10294, 0.10226, 0.10237, 0.10302, 0.10435, 0.10577, 0.10697,
0.10637, 0.10794, 0.10592, 0.10451, 0.10002, 0.09904, 0.10209,
0.10208, 0.10162, 0.10222, 0.10529, 0.10228, 0.10552, 0.10301,
0.10383, 0.10751, 0.10666, 0.10712, 0.10641, 0.10727, 0.10798,
0.10733, 0.10371, 0.10179, 0.10146, 0.10257, 0.10128, 0.10085,
0.10155, 0.10168, 0.10219, 0.1042, 0.1084, 0.11577, 0.115, 0.11817,
0.116, 0.1129, 0.11084, 0.11083, 0.1113, 0.11353, 0.11784, 0.11776,
0.11845, 0.11295, 0.10911, 0.10845, 0.11478, 0.11579, 0.1175,
0.11665, 0.11785, 0.11698, 0.1194, 0.11767, 0.11323, 0.11335,
0.11267, 0.11098, 0.10751, 0.10698, 0.10684, 0.10928, 0.1089,
0.10764, 0.10056, 0.09559, 0.09648, 0.09531, 0.0943, 0.09277,
0.08542, 0.08315, 0.07432, 0.07523, 0.07335, 0.07358, 0.07323,
0.07433, 0.07611, 0.07517, 0.07387, 0.07573, 0.07559, 0.0712,
0.0701, 0.0662, 0.06954, 0.07125, 0.07021, 0.06932, 0.07309,
0.073, 0.0732, 0.07273, 0.07317, 0.074, 0.07531, 0.07681, 0.07934,
0.0757, 0.07448, 0.07179, 0.07405, 0.07721, 0.0763, 0.07491,
0.07546, 0.07561, 0.07502, 0.07573, 0.07638, 0.0769, 0.07732,
0.078, 0.07849, 0.07858, 0.07817, 0.07933, 0.08047, 0.08092,
0.0805, 0.07974, 0.07813, 0.0763, 0.07558, 0.07249, 0.07182,
0.07319, 0.0752, 0.07437, 0.07518, 0.07607, 0.07724, 0.07668,
0.07748, 0.07744, 0.07743, 0.07725, 0.07776, 0.07836, 0.07492,
0.07462, 0.07259, 0.07278, 0.07301, 0.07326, 0.0739, 0.07444,
0.07535, 0.07669, 0.07729, 0.07824, 0.07928, 0.07934, 0.08162,
0.08316), low = c(0.11978, 0.1178, 0.1172, 0.1201, 0.12178, 0.12205,
0.12212, 0.12193, 0.12048, 0.12058, 0.12257, 0.11896, 0.11811,
0.12107, 0.12117, 0.12129, 0.122, 0.12429, 0.1266, 0.12721, 0.1266,
0.12661, 0.12412, 0.12504, 0.12548, 0.12404, 0.1243, 0.12431,
0.12533, 0.12138, 0.12, 0.12036, 0.12059, 0.12162, 0.12222, 0.1203,
0.12148, 0.12103, 0.12313, 0.12225, 0.12175, 0.11411, 0.10467,
0.10653, 0.10525, 0.10507, 0.10529, 0.10833, 0.10878, 0.10316,
0.10021, 0.10294, 0.09925, 0.10017, 0.1003, 0.10126, 0.10326,
0.10373, 0.10133, 0.09563, 0.09632, 0.09935, 0.10089, 0.09973,
0.10072, 0.10144, 0.10297, 0.10316, 0.10421, 0.10231, 0.09465,
0.09572, 0.09618, 0.09773, 0.09974, 0.09863, 0.09931, 0.10089,
0.10076, 0.10128, 0.10143, 0.10196, 0.10253, 0.10498, 0.1053,
0.10303, 0.10376, 0.10452, 0.10154, 0.10024, 0.0996, 0.09903,
0.1002, 0.09738, 0.09778, 0.09941, 0.1003, 0.10007, 0.10095,
0.1027, 0.10753, 0.11202, 0.11312, 0.1108, 0.1097, 0.10787, 0.10805,
0.10945, 0.10915, 0.10955, 0.11154, 0.11255, 0.10815, 0.10594,
0.10556, 0.10715, 0.10988, 0.11185, 0.11061, 0.11198, 0.11243,
0.11368, 0.11125, 0.10968, 0.11047, 0.11069, 0.10627, 0.10495,
0.10497, 0.10559, 0.10482, 0.10695, 0.099, 0.09241, 0.09346,
0.09452, 0.08974, 0.09206, 0.08209, 0.08185, 0.07179, 0.06451,
0.06982, 0.06912, 0.06969, 0.06978, 0.07074, 0.07367, 0.07129,
0.07085, 0.071, 0.07051, 0.06847, 0.06405, 0.06267, 0.0644, 0.06819,
0.06828, 0.06762, 0.06881, 0.07029, 0.07149, 0.07066, 0.07069,
0.07232, 0.07344, 0.07382, 0.07435, 0.07163, 0.0697, 0.0698,
0.07053, 0.07294, 0.07338, 0.07073, 0.07214, 0.07394, 0.07241,
0.07208, 0.07436, 0.0751, 0.0757, 0.07623, 0.07703, 0.07535,
0.07524, 0.0776, 0.07782, 0.07924, 0.0789, 0.07693, 0.07389,
0.07411, 0.07132, 0.07025, 0.07026, 0.07008, 0.07264, 0.07324,
0.07341, 0.07425, 0.07435, 0.07507, 0.07561, 0.07569, 0.07585,
0.07574, 0.07585, 0.07335, 0.0722, 0.07157, 0.071, 0.0716, 0.07029,
0.07111, 0.07195, 0.07201, 0.07392, 0.07465, 0.07623, 0.07472,
0.0775, 0.07763, 0.07878, 0.08125), close = c(0.12167, 0.11791,
0.12137, 0.124, 0.12236, 0.12377, 0.12392, 0.12381, 0.12164,
0.12389, 0.12415, 0.11915, 0.12109, 0.1224, 0.1232, 0.12214,
0.12488, 0.12839, 0.1281, 0.12755, 0.12701, 0.12745, 0.12667,
0.12633, 0.12575, 0.12454, 0.12458, 0.12859, 0.12555, 0.1224,
0.12279, 0.12176, 0.12208, 0.12335, 0.12427, 0.12214, 0.1217,
0.12403, 0.12473, 0.12335, 0.1237, 0.11486, 0.11016, 0.10653,
0.10829, 0.10642, 0.10902, 0.10982, 0.10981, 0.10402, 0.10336,
0.104, 0.10134, 0.10268, 0.10199, 0.10358, 0.10443, 0.10513,
0.10274, 0.09678, 0.10175, 0.10115, 0.10107, 0.1022, 0.10152,
0.10517, 0.10362, 0.10465, 0.10576, 0.10422, 0.09695, 0.09657,
0.09841, 0.10165, 0.10065, 0.09993, 0.10115, 0.10199, 0.10155,
0.10233, 0.10212, 0.10274, 0.10594, 0.10571, 0.10572, 0.10405,
0.10609, 0.10478, 0.10286, 0.10132, 0.10018, 0.10025, 0.10122,
0.09857, 0.10011, 0.10051, 0.10072, 0.10155, 0.10333, 0.10762,
0.11307, 0.11414, 0.11527, 0.11241, 0.11021, 0.1093, 0.11005,
0.10972, 0.11176, 0.11385, 0.11405, 0.1129, 0.10851, 0.10746,
0.10777, 0.11022, 0.11558, 0.11485, 0.11349, 0.11465, 0.11569,
0.11527, 0.1119, 0.11233, 0.11197, 0.11084, 0.10661, 0.10556,
0.10664, 0.1064, 0.10863, 0.10743, 0.09959, 0.09358, 0.09525,
0.09484, 0.09361, 0.09215, 0.08424, 0.08281, 0.0727, 0.0706,
0.07001, 0.07005, 0.07199, 0.07117, 0.07421, 0.07469, 0.07327,
0.07132, 0.07559, 0.07097, 0.06862, 0.0647, 0.066, 0.06857, 0.07012,
0.06901, 0.06896, 0.0715, 0.07156, 0.07209, 0.07207, 0.07261,
0.07345, 0.07469, 0.07625, 0.0758, 0.07376, 0.07006, 0.07137,
0.07336, 0.07602, 0.07389, 0.07429, 0.0752, 0.07399, 0.07305,
0.07533, 0.07525, 0.0765, 0.07666, 0.07782, 0.07804, 0.07596,
0.07783, 0.07925, 0.07921, 0.08028, 0.07938, 0.0779, 0.07477,
0.07534, 0.07174, 0.07103, 0.07122, 0.07289, 0.07387, 0.07369,
0.07503, 0.07509, 0.07524, 0.07636, 0.07573, 0.07639, 0.07679,
0.07628, 0.0777, 0.07359, 0.07462, 0.07216, 0.07173, 0.07265,
0.07201, 0.07315, 0.07227, 0.07429, 0.07499, 0.07626, 0.07713,
0.07766, 0.07819, 0.07915, 0.08148, 0.08275), volume = c(17894295,
25956559, 19646834, 21073444, 18817421, 12316289, 11348380, 14945318,
20611382, 29389056, 15639183, 23990903, 10230550, 11791876, 8849962,
8712513, 17411436, 52294930, 13741243, 12159456, 10156693, 7392933,
24556017, 12498171, 6585602, 9053311, 10389994, 32763165, 18691599,
20920341, 17194082, 13384844, 10762647, 11238837, 20643490, 17826487,
8605881, 12725146, 8033532, 14576577, 18203767, 33106274, 57475480,
35090353, 19236551, 22848525, 19670114, 12209910, 9189784, 28139942,
32849205, 23256020, 31210609, 23094329, 12240627, 12774399, 8347728,
10614794, 13157648, 47509634, 20708007, 13009872, 5347605, 10099313,
18566517, 24284140, 16257416, 13504669, 26353561, 13722621, 42173001,
23675661, 8144656, 17929342, 9387011, 13854189, 14603858, 31616737,
6704224, 24196867, 13695017, 11613987, 27856536, 13454585, 10497017,
16458068, 16297639, 12785798, 19360445, 12732714, 10444114, 16201247,
9645137, 19007416, 14556473, 9980469, 8106055, 13820499, 10194965,
64253247, 161980227, 55008265, 62794334, 47634737, 27375292,
28948205, 23303586, 15667935, 20378324, 42039932, 45234586, 55979026,
38458874, 19968558, 11843244, 33406862, 42373158, 54126294, 46806385,
61654732, 27197944, 59155494, 33707242, 23832047, 19967177, 10957640,
15637922, 19658790, 13028772, 9082828, 24976755, 10789730, 39076951,
49842677, 16384967, 15601195, 29659625, 14836814, 53131858, 26904846,
50215911, 80446251, 31711637, 27684325, 18295756, 30244578, 25000228,
31701319, 20297596, 17620148, 16712144, 24532847, 20024178, 61355138,
49383309, 34613954, 15938600, 14048986, 9165050, 33670464, 28771619,
24220858, 11889148, 24206767, 16696844, 19341205, 30682562, 48411541,
31624605, 19583695, 7736058, 14697691, 20371228, 21601985, 14667855,
11662673, 8565838, 8950674, 15198713, 18115403, 13873815, 9454750,
13116891, 11227183, 17723342, 11168569, 17650051, 16902150, 16042595,
12631966, 17581570, 22259046, 16421002, 23295568, 15918092, 18004724,
24079621, 23097753, 12800081, 12312440, 13131810, 19374725, 14036774,
15552921, 12017393, 6465640, 6071254, 8288237, 23909383, 15594514,
13318570, 9361610, 7166725, 9715764, 8906698, 8078021, 8742947,
5312715, 8228518, 7468992, 21681712, 15425063, 9088154, 16051149,
14306452), trades = c(11591L, 13904L, 8770L, 8976L, 8889L, 7218L,
7956L, 10289L, 8338L, 15267L, 7974L, 11350L, 6627L, 8199L, 5462L,
5711L, 9750L, 27559L, 7345L, 7275L, 4761L, 4086L, 11405L, 7343L,
3511L, 5410L, 4619L, 14885L, 10363L, 11337L, 10040L, 7900L, 6029L,
6823L, 9894L, 9795L, 5252L, 8359L, 4739L, 8325L, 10895L, 17994L,
26704L, 16954L, 11073L, 11451L, 10072L, 6270L, 4694L, 13397L,
14960L, 9414L, 14906L, 12126L, 6198L, 5929L, 4447L, 4968L, 6663L,
19502L, 8574L, 6111L, 2793L, 4580L, 7616L, 11026L, 7975L, 6380L,
10178L, 7006L, 18590L, 11151L, 4108L, 7542L, 4110L, 7336L, 7791L,
14672L, 3764L, 12346L, 6006L, 6644L, 14171L, 6804L, 5282L, 7091L,
6975L, 7109L, 9196L, 7046L, 5131L, 7664L, 5566L, 9995L, 8029L,
4665L, 3391L, 5387L, 5661L, 21898L, 63727L, 23335L, 25023L, 22152L,
11598L, 12217L, 10815L, 6903L, 10246L, 25837L, 21694L, 21720L,
12474L, 8370L, 5376L, 15747L, 18057L, 20906L, 17232L, 25948L,
11954L, 24800L, 12686L, 9503L, 8451L, 6271L, 8233L, 10329L, 6095L,
5637L, 9733L, 6092L, 13746L, 19684L, 7050L, 6934L, 13457L, 7401L,
17572L, 11348L, 18615L, 25756L, 10412L, 10349L, 6128L, 12691L,
9583L, 10143L, 7123L, 7471L, 6147L, 10867L, 6986L, 14811L, 15151L,
9144L, 4273L, 4538L, 3426L, 9159L, 9095L, 8463L, 4558L, 7123L,
6183L, 6101L, 8886L, 16735L, 8368L, 6808L, 3165L, 5547L, 8119L,
9066L, 6105L, 5640L, 4008L, 4116L, 4971L, 6974L, 4911L, 3472L,
4044L, 4096L, 5997L, 4972L, 7099L, 7793L, 6059L, 4567L, 5282L,
6897L, 6491L, 10157L, 6830L, 6899L, 8623L, 9719L, 4053L, 4742L,
4628L, 7551L, 5978L, 7164L, 4851L, 3556L, 3060L, 3858L, 8498L,
6731L, 5812L, 3765L, 2530L, 3805L, 3734L, 4060L, 3785L, 2778L,
4578L, 4151L, 8128L, 7304L, 3698L, 7376L, 5582L), SMA_5 = c(NA,
NA, NA, NA, 0.121462, 0.121882, 0.123084, 0.123572, 0.1231, 0.123406,
0.123482, 0.122528, 0.121984, 0.122136, 0.121998, 0.121596, 0.122742,
0.124202, 0.125342, 0.126212, 0.127186, 0.1277, 0.127356, 0.127002,
0.126642, 0.126148, 0.125574, 0.125958, 0.125802, 0.125132, 0.124782,
0.124218, 0.122916, 0.122476, 0.12285, 0.12272, 0.122708, 0.123098,
0.123374, 0.12319, 0.123502, 0.122134, 0.11936, 0.11572, 0.112708,
0.109252, 0.108084, 0.108016, 0.108672, 0.107818, 0.107206, 0.106202,
0.104506, 0.10308, 0.102674, 0.102718, 0.102804, 0.103562, 0.103574,
0.102532, 0.102166, 0.10151, 0.100698, 0.10059, 0.101538, 0.102222,
0.102716, 0.103432, 0.104144, 0.104684, 0.10304, 0.10163, 0.100382,
0.09956, 0.098846, 0.099442, 0.100358, 0.101074, 0.101054, 0.10139,
0.101828, 0.102146, 0.102936, 0.103768, 0.104446, 0.104832, 0.105502,
0.10527, 0.1047, 0.10382, 0.103046, 0.101878, 0.101166, 0.100308,
0.100066, 0.100132, 0.100226, 0.100292, 0.101244, 0.102746, 0.105258,
0.107942, 0.110686, 0.112502, 0.11302, 0.112266, 0.111448, 0.110338,
0.110208, 0.110936, 0.111886, 0.112456, 0.112214, 0.111354, 0.110138,
0.109372, 0.109908, 0.111176, 0.112382, 0.113758, 0.114852, 0.11479,
0.1142, 0.113968, 0.113432, 0.112462, 0.11073, 0.109462, 0.108324,
0.10721, 0.106768, 0.106932, 0.105738, 0.103126, 0.100896, 0.098138,
0.095374, 0.093886, 0.092018, 0.08953, 0.085102, 0.0805, 0.076072,
0.073234, 0.07107, 0.070764, 0.071486, 0.072422, 0.073066, 0.072932,
0.073816, 0.073168, 0.071954, 0.07024, 0.069176, 0.067772, 0.067602,
0.06768, 0.068532, 0.069632, 0.07023, 0.070624, 0.071236, 0.071966,
0.072356, 0.072982, 0.073814, 0.07456, 0.07479, 0.074112, 0.073448,
0.07287, 0.072914, 0.07294, 0.073786, 0.074552, 0.074678, 0.074084,
0.074372, 0.074564, 0.074824, 0.075358, 0.076312, 0.076854, 0.076996,
0.077262, 0.07778, 0.078058, 0.078506, 0.07919, 0.079204, 0.078308,
0.077534, 0.075826, 0.074156, 0.07282, 0.072444, 0.07215, 0.07254,
0.07334, 0.074114, 0.074584, 0.075082, 0.07549, 0.075762, 0.076102,
0.07631, 0.076578, 0.07615, 0.075796, 0.07487, 0.07396, 0.07295,
0.072634, 0.07234, 0.072362, 0.072874, 0.073342, 0.074192, 0.074988,
0.076066, 0.076846, 0.077678, 0.078722, 0.079846), SMA_10 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, 0.122434, 0.122682, 0.122806,
0.122778, 0.122618, 0.122702, 0.122539, 0.122635, 0.123093, 0.123739,
0.124105, 0.124391, 0.125221, 0.125779, 0.126172, 0.126427, 0.126667,
0.126637, 0.126657, 0.126402, 0.125887, 0.125465, 0.124896, 0.124437,
0.124139, 0.123991, 0.123751, 0.123463, 0.123007, 0.122925, 0.12302,
0.123111, 0.122421, 0.121229, 0.119547, 0.117949, 0.116377, 0.115109,
0.113688, 0.112196, 0.110263, 0.108229, 0.107143, 0.106261, 0.105876,
0.105246, 0.104962, 0.104503, 0.104034, 0.103327, 0.102603, 0.102442,
0.102157, 0.10213, 0.102082, 0.102035, 0.102194, 0.102113, 0.102065,
0.102367, 0.103111, 0.102631, 0.102173, 0.101907, 0.101852, 0.101765,
0.101241, 0.100994, 0.100728, 0.100307, 0.100118, 0.100635, 0.101252,
0.102005, 0.102411, 0.102918, 0.10333, 0.103824, 0.104103, 0.104234,
0.104133, 0.103939, 0.10369, 0.103218, 0.102504, 0.101943, 0.101589,
0.101052, 0.100729, 0.100776, 0.101406, 0.102695, 0.104084, 0.105489,
0.106873, 0.107883, 0.108762, 0.109695, 0.110512, 0.111355, 0.111978,
0.112076, 0.111952, 0.111276, 0.110781, 0.110537, 0.110629, 0.111182,
0.111695, 0.111868, 0.111948, 0.112112, 0.112349, 0.112688, 0.113175,
0.113595, 0.113657, 0.11276, 0.111831, 0.111146, 0.110321, 0.109615,
0.108831, 0.1076, 0.105725, 0.104053, 0.102453, 0.101153, 0.099812,
0.097572, 0.095213, 0.09162, 0.087937, 0.084979, 0.082626, 0.0803,
0.077933, 0.075993, 0.074247, 0.07315, 0.072001, 0.07229, 0.072327,
0.072188, 0.071653, 0.071054, 0.070794, 0.070385, 0.069817, 0.069386,
0.069404, 0.069001, 0.069113, 0.069458, 0.070249, 0.070994, 0.071606,
0.072219, 0.072898, 0.073378, 0.073234, 0.073215, 0.073342, 0.073737,
0.073865, 0.073949, 0.074, 0.073774, 0.073499, 0.073656, 0.074175,
0.074688, 0.075018, 0.075198, 0.075613, 0.07578, 0.076043, 0.076569,
0.077185, 0.07768, 0.078093, 0.078233, 0.078044, 0.077796, 0.077166,
0.076673, 0.076012, 0.075376, 0.074842, 0.074183, 0.073748, 0.073467,
0.073514, 0.073616, 0.074015, 0.074551, 0.075108, 0.075447, 0.07583,
0.07582, 0.075779, 0.075486, 0.075135, 0.074764, 0.074392, 0.074068,
0.073616, 0.073417, 0.073146, 0.073413, 0.073664, 0.074214, 0.07486,
0.07551, 0.076457, 0.077417), SMA_20 = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.1232695,
0.1235365, 0.1240135, 0.1242785, 0.124395, 0.1245645, 0.124603,
0.124636, 0.124875, 0.1250705, 0.124996, 0.124928, 0.1250585,
0.125108, 0.1251555, 0.125209, 0.125209, 0.12505, 0.124832, 0.1246635,
0.1244535, 0.124288, 0.1236585, 0.122833, 0.121843, 0.12097,
0.120064, 0.119286, 0.1183475, 0.1175605, 0.1166415, 0.11567,
0.114782, 0.113745, 0.1127115, 0.1115975, 0.1106695, 0.109806,
0.108861, 0.1077615, 0.106433, 0.1053355, 0.10465, 0.1041955,
0.103979, 0.1036405, 0.103578, 0.103308, 0.1030495, 0.102847,
0.102857, 0.1025365, 0.102165, 0.1020185, 0.101967, 0.1019, 0.1017175,
0.1015535, 0.1013965, 0.101337, 0.1016145, 0.101633, 0.1017125,
0.101956, 0.1021315, 0.1023415, 0.1022855, 0.102409, 0.1024155,
0.1022705, 0.1021255, 0.102287, 0.102471, 0.1026115, 0.1024575,
0.1024305, 0.1024595, 0.102438, 0.102416, 0.102505, 0.1027695,
0.103317, 0.103887, 0.1043535, 0.1046885, 0.104913, 0.1051755,
0.1053735, 0.1056205, 0.1060655, 0.106692, 0.1073855, 0.108018,
0.1083825, 0.108827, 0.10921, 0.1096955, 0.1104385, 0.1111035,
0.1116115, 0.111963, 0.112094, 0.1121505, 0.111982, 0.111978,
0.112066, 0.112143, 0.111971, 0.111763, 0.111507, 0.1111345,
0.1108635, 0.11059, 0.110144, 0.10945, 0.108824, 0.108055, 0.1069565,
0.1058215, 0.104359, 0.102767, 0.1006175, 0.0983839999999999,
0.0962894999999999, 0.0941754999999999, 0.0921764999999999, 0.0901929999999999,
0.0885729999999999, 0.0870294999999999, 0.0853609999999999, 0.0836069999999999,
0.0819549999999999, 0.0801319999999999, 0.0785834999999999, 0.0771394999999999,
0.0756769999999999, 0.0743634999999999, 0.0731889999999999, 0.0720319999999999,
0.0712679999999999, 0.0707024999999999, 0.0706454999999999, 0.0707199999999999,
0.0708229999999999, 0.0709509999999999, 0.0710239999999999, 0.0711999999999999,
0.0713019999999999, 0.0713574999999999, 0.0713819999999999, 0.0713189999999999,
0.0711079999999999, 0.0712274999999999, 0.0715974999999999, 0.0720569999999999,
0.0724714999999999, 0.0728029999999999, 0.0729964999999999, 0.0731984999999999,
0.0735169999999999, 0.0737044999999999, 0.0739514999999999, 0.0741799999999999,
0.0744674999999999, 0.0747389999999999, 0.0748644999999999, 0.0750214999999999,
0.0751714999999999, 0.0753419999999999, 0.0756679999999999, 0.0761339999999999,
0.0764604999999999, 0.0765309999999999, 0.0764969999999999, 0.0763894999999999,
0.0762264999999999, 0.0760274999999999, 0.0759724999999999, 0.0760134999999999,
0.0759314999999999, 0.0759204999999999, 0.0758499999999999, 0.0757789999999999,
0.0757059999999999, 0.0755904999999999, 0.0756119999999999, 0.0755599999999999,
0.0754114999999999, 0.0753359999999999, 0.0750014999999999, 0.0747634999999999,
0.0744764999999999, 0.0743244999999999, 0.0741899999999999, 0.0742034999999999,
0.0743094999999999, 0.0743619999999999, 0.0744319999999999, 0.0744879999999999,
0.0746164999999999, 0.0747214999999999, 0.0748499999999999, 0.0749974999999999,
0.0751369999999999, 0.0754244999999999, 0.0757424999999999)), row.names = c(NA,
-225L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00000000105f1ef0>)
What I want is to connect the two highest highs and expand the line across all the x-axis. Here's the code to get that data:
highs <- arrange(sma, desc(high))%>%
slice(1:2)
highs
time open high low close volume trades SMA_5 SMA_10 SMA_20
1: 2022-01-01 10:00:00 0.12488 0.13202 0.12429 0.12839 52294930 27559 0.124202 0.123093 NA
2: 2022-01-03 02:00:00 0.12455 0.12930 0.12431 0.12859 32763165 14885 0.125958 0.126657 0.124875
So basically I have 0.13202 from January 1st and 0.12930 from January 3rd as my highest highs. With some code such as:
add_lines(inherit = F, data = highs, x = ~time, y = ~high,
name = "Higher highs trendline", line = list(color = "seagreen", width = 2.5, dash = "dot"))
I can draw the line exactly for the two points in my highs object. But what I want is that this line goes across all my graph. In short, given two points I want to draw a trendline the same way we used to do on basic algebra. Main code for my graph is:
sma %>% plot_ly(x = ~time, type="candlestick",
open = ~open, close = ~close,
high = ~high, low = ~low) %>%
add_lines(x = ~time, y= ~SMA_5, line = list(color = "gold", width = 2), inherit = F,
name = "SMA 5", showlegend=T)%>%
add_lines(x = ~time, y= ~SMA_10, line = list(color = "deeppink", width = 2), inherit = F,
name = "SMA 10", showlegend=T)%>%
add_lines(x = ~time, y= ~SMA_20, line = list(color = "purple", width = 2), inherit = F,
name = "SMA 20", showlegend=T)%>%
plotly::layout(title = paste0(nombre, " Simple Moving Average, ", tiempo),
xaxis= list(title="Time", rangeslider = list(visible = F)), yaxis = list(title = "Price"),
sliders=list(visible=F)) -> sma_
Any help and orientation will be much appreciated.
You could fit a linear model on the two high points, and predict it's values on the remaining dataset:
fit <- lm(high ~ time, data = highs)
sma %>% plot_ly(x = ~time, type="candlestick",
open = ~open, close = ~close,
high = ~high, low = ~low) %>%
add_lines( x = ~time, y = ~predict(fit,sma))
I have a homework assignment where I need to take a CSV file based around population data around the United States and do some data analysis on the data inside. I need to find the data that exists for my state and for starters run a Linear Regression Analysis to predict the size of the population.
I've been studying R for a few weeks now, went through a LinkedIn Learning training, as well as 2 different trainings on pluralsight about R. I have also tried searching for how to do a Linear Regression Analysis in R and I find plenty of examples for how to do it when the data is perfectly laid out in a table in just the right way to Analyze.
The CSV file is laid out so that each state is defined on a single line/row so I used the filter function to grab just the data for my State and put it into a variable.
Within that dataset the population data is defined across several columns with the most important data being the Population Estimates for each year from 2010 to 2018.
library(tidyverse)
population.data <- read_csv("nst-est2018-alldata.csv")
mn.state.data <- filter(population.data, NAME == "Minnesota")
I'm looking for some help to get headed in the right direction my thought is that I will need to create to containers of data 1 having each year from 2010 to 2018 and one that contains the population data for each of those years. And then use the xyplot function with those two containers? If you have some experience in this area please help me think this through I'm not looking for anybody to do the assignment for me just want some help trying to think it through.
Edit: Here is the results of the
dput(head(population.data))
command:
structure(list(SUMLEV = c("010", "020", "020", "020", "020",
"040"), REGION = c("0", "1", "2", "3", "4", "3"), DIVISION = c("0",
"0", "0", "0", "0", "6"), STATE = c("00", "00", "00", "00", "00",
"01"), NAME = c("United States", "Northeast Region", "Midwest Region",
"South Region", "West Region", "Alabama"), CENSUS2010POP = c(308745538L,
55317240L, 66927001L, 114555744L, 71945553L, 4779736L), ESTIMATESBASE2010
= c(308758105L,
55318430L, 66929743L, 114563045L, 71946887L, 4780138L), POPESTIMATE2010 =
c(309326085L,
55380645L, 66974749L, 114867066L, 72103625L, 4785448L), POPESTIMATE2011 =
c(311580009L,
55600532L, 67152631L, 116039399L, 72787447L, 4798834L), POPESTIMATE2012 =
c(313874218L,
55776729L, 67336937L, 117271075L, 73489477L, 4815564L), POPESTIMATE2013 =
c(316057727L,
55907823L, 67564135L, 118393244L, 74192525L, 4830460L), POPESTIMATE2014 =
c(318386421L,
56015864L, 67752238L, 119657737L, 74960582L, 4842481L), POPESTIMATE2015 =
c(320742673L,
56047587L, 67869139L, 121037542L, 75788405L, 4853160L), POPESTIMATE2016 =
c(323071342L,
56058789L, 67996917L, 122401186L, 76614450L, 4864745L), POPESTIMATE2017 =
c(325147121L,
56072676L, 68156035L, 123598424L, 77319986L, 4875120L), POPESTIMATE2018 =
c(327167434L,
56111079L, 68308744L, 124753948L, 77993663L, 4887871L), NPOPCHG_2010 =
c(567980L,
62215L, 45006L, 304021L, 156738L, 5310L), NPOPCHG_2011 = c(2253924L,
219887L, 177882L, 1172333L, 683822L, 13386L), NPOPCHG_2012 = c(2294209L,
176197L, 184306L, 1231676L, 702030L, 16730L), NPOPCHG_2013 = c(2183509L,
131094L, 227198L, 1122169L, 703048L, 14896L), NPOPCHG_2014 = c(2328694L,
108041L, 188103L, 1264493L, 768057L, 12021L), NPOPCHG_2015 = c(2356252L,
31723L, 116901L, 1379805L, 827823L, 10679L), NPOPCHG_2016 = c(2328669L,
11202L, 127778L, 1363644L, 826045L, 11585L), NPOPCHG_2017 = c(2075779L,
13887L, 159118L, 1197238L, 705536L, 10375L), NPOPCHG_2018 = c(2020313L,
38403L, 152709L, 1155524L, 673677L, 12751L), BIRTHS2010 = c(987836L,
163454L, 212614L, 368752L, 243016L, 14227L), BIRTHS2011 = c(3973485L,
646265L, 834909L, 1509597L, 982714L, 59689L), BIRTHS2012 = c(3936976L,
637904L, 830701L, 1504936L, 963435L, 59070L), BIRTHS2013 = c(3940576L,
635741L, 830869L, 1504799L, 969167L, 57936L), BIRTHS2014 = c(3963195L,
632433L, 836505L, 1525280L, 968977L, 58907L), BIRTHS2015 = c(3992376L,
634515L, 837968L, 1545722L, 974171L, 59637L), BIRTHS2016 = c(3962654L,
628039L, 831667L, 1541342L, 961606L, 59388L), BIRTHS2017 = c(3901982L,
616552L, 816177L, 1519944L, 949309L, 58259L), BIRTHS2018 = c(3855500L,
609336L, 804431L, 1499838L, 941895L, 57216L), DEATHS2010 = c(598691L,
110848L, 140785L, 228706L, 118352L, 11073L), DEATHS2011 = c(2512442L,
470816L, 586840L, 962751L, 492035L, 48818L), DEATHS2012 = c(2501531L,
460985L, 584817L, 960575L, 495154L, 48364L), DEATHS2013 = c(2608019L,
480032L, 605188L, 1011093L, 511706L, 50847L), DEATHS2014 = c(2582448L,
470196L, 597078L, 1006057L, 509117L, 49692L), DEATHS2015 = c(2699826L,
488881L, 626494L, 1052360L, 532091L, 51820L), DEATHS2016 = c(2703215L,
480331L, 619471L, 1058173L, 545240L, 51662L), DEATHS2017 = c(2779436L,
501022L, 620556L, 1092949L, 564909L, 53033L), DEATHS2018 = c(2814013L,
506909L, 621030L, 1109152L, 576922L, 53425L), NATURALINC2010 = c(389145L,
52606L, 71829L, 140046L, 124664L, 3154L), NATURALINC2011 = c(1461043L,
175449L, 248069L, 546846L, 490679L, 10871L), NATURALINC2012 = c(1435445L,
176919L, 245884L, 544361L, 468281L, 10706L), NATURALINC2013 = c(1332557L,
155709L, 225681L, 493706L, 457461L, 7089L), NATURALINC2014 = c(1380747L,
162237L, 239427L, 519223L, 459860L, 9215L), NATURALINC2015 = c(1292550L,
145634L, 211474L, 493362L, 442080L, 7817L), NATURALINC2016 = c(1259439L,
147708L, 212196L, 483169L, 416366L, 7726L), NATURALINC2017 = c(1122546L,
115530L, 195621L, 426995L, 384400L, 5226L), NATURALINC2018 = c(1041487L,
102427L, 183401L, 390686L, 364973L, 3791L), INTERNATIONALMIG2010 =
c(178835L,
45723L, 25158L, 68742L, 39212L, 928L), INTERNATIONALMIG2011 = c(792881L,
206686L, 116948L, 285343L, 183904L, 4716L), INTERNATIONALMIG2012 =
c(858764L,
207584L, 120995L, 344198L, 185987L, 5874L), INTERNATIONALMIG2013 =
c(850952L,
194103L, 126681L, 329897L, 200271L, 5111L), INTERNATIONALMIG2014 =
c(947947L,
222685L, 134310L, 365281L, 225671L, 3753L), INTERNATIONALMIG2015 =
c(1063702L,
227275L, 142759L, 429088L, 264580L, 4685L), INTERNATIONALMIG2016 =
c(1069230L,
236718L, 144859L, 436795L, 250858L, 5950L), INTERNATIONALMIG2017 =
c(953233L,
215872L, 126013L, 404582L, 206766L, 3190L), INTERNATIONALMIG2018 =
c(978826L,
229700L, 127583L, 418418L, 203125L, 3344L), DOMESTICMIG2010 = c(0L,
-32918L, -50873L, 90679L, -6888L, 1238L), DOMESTICMIG2011 = c(0L,
-159789L, -186896L, 335757L, 10928L, -2239L), DOMESTICMIG2012 = c(0L,
-205314L, -181285L, 336615L, 49984L, 59L), DOMESTICMIG2013 = c(0L,
-216273L, -123814L, 293443L, 46644L, 2641L), DOMESTICMIG2014 = c(0L,
-274391L, -182730L, 373439L, 83682L, -755L), DOMESTICMIG2015 = c(0L,
-339996L, -234823L, 452879L, 121940L, -1553L), DOMESTICMIG2016 = c(0L,
-372953L, -228200L, 442633L, 158520L, -1977L), DOMESTICMIG2017 = c(0L,
-316879L, -161387L, 364465L, 113801L, 2065L), DOMESTICMIG2018 = c(0L,
-292928L, -157048L, 345132L, 104844L, 5718L), NETMIG2010 = c(178835L,
12805L, -25715L, 159421L, 32324L, 2166L), NETMIG2011 = c(792881L,
46897L, -69948L, 621100L, 194832L, 2477L), NETMIG2012 = c(858764L,
2270L, -60290L, 680813L, 235971L, 5933L), NETMIG2013 = c(850952L,
-22170L, 2867L, 623340L, 246915L, 7752L), NETMIG2014 = c(947947L,
-51706L, -48420L, 738720L, 309353L, 2998L), NETMIG2015 = c(1063702L,
-112721L, -92064L, 881967L, 386520L, 3132L), NETMIG2016 = c(1069230L,
-136235L, -83341L, 879428L, 409378L, 3973L), NETMIG2017 = c(953233L,
-101007L, -35374L, 769047L, 320567L, 5255L), NETMIG2018 = c(978826L,
-63228L, -29465L, 763550L, 307969L, 9062L), RESIDUAL2010 = c(0L,
-3196L, -1108L, 4554L, -250L, -10L), RESIDUAL2011 = c(0L, -2459L,
-239L, 4387L, -1689L, 38L), RESIDUAL2012 = c(0L, -2992L, -1288L,
6502L, -2222L, 91L), RESIDUAL2013 = c(0L, -2445L, -1350L, 5123L,
-1328L, 55L), RESIDUAL2014 = c(0L, -2490L, -2904L, 6550L, -1156L,
-192L), RESIDUAL2015 = c(0L, -1190L, -2509L, 4476L, -777L, -270L
), RESIDUAL2016 = c(0L, -271L, -1077L, 1047L, 301L, -114L), RESIDUAL2017 =
c(0L,
-636L, -1129L, 1196L, 569L, -106L), RESIDUAL2018 = c(0L, -796L,
-1227L, 1288L, 735L, -102L), RBIRTH2011 = c(12.79898857, 11.646389369,
12.449493906, 13.0753983, 13.564866164, 12.455601786), RBIRTH2012 =
c(12.589173852,
11.454833676, 12.353389372, 12.900715293, 13.172754439, 12.287820829
), RBIRTH2013 = c(12.511116578, 11.384582534, 12.318197145, 12.770698648,
13.1250523, 12.012410502), RBIRTH2014 = c(12.493440163, 11.301146646,
12.363692308, 12.814734, 12.993051496, 12.179749675), RBIRTH2015 =
c(12.493175596,
11.324209532, 12.357461907, 12.843808208, 12.92441189, 12.301816868
), RBIRTH2016 = c(12.309933949, 11.20434042, 12.242454436, 12.663079639,
12.619264908, 12.222387438), RBIRTH2017 = c(12.039095529, 10.996948983,
11.989119413, 12.357287884, 12.333939366, 11.962999487), RBIRTH2018 =
c(11.820984126,
10.863177115, 11.789576855, 12.078306222, 12.128940451, 11.720998206
), RDEATH2011 = c(8.0928244199, 8.4846099623, 8.7504877826, 8.3388830191,
6.7917918366, 10.187095914), RDEATH2012 = c(7.9990857588, 8.2779015368,
8.6968381072, 8.2343067033, 6.7700904074, 10.060744313), RDEATH2013 =
c(8.2803198685,
8.5962112289, 8.9723230665, 8.5807898649, 6.9298356343, 10.542582104
), RDEATH2014 = c(8.1408206164, 8.4020820365, 8.8249187702, 8.4524499397,
6.8267702932, 10.274434632), RDEATH2015 = c(8.4484528254, 8.7250748685,
9.2388679994, 8.7443343664, 7.0592978512, 10.689339673), RDEATH2016 =
c(8.3975028099,
8.5692003816, 9.1188486402, 8.6935469035, 7.1552465339, 10.632332792
), RDEATH2017 = c(8.5756150392, 8.9363320099, 9.1155717285, 8.8857783149,
7.3396052849, 10.889883997), RDEATH2018 = c(8.6277792774, 9.0371195009,
9.1016891619, 8.9320830002, 7.4291216994, 10.944391939), RNATURALINC2011 =
c(4.7061641498,
3.161779407, 3.6990061239, 4.7365152812, 6.7730743272, 2.2685058724
), RNATURALINC2012 = c(4.5900880929, 3.1769321388, 3.656551265,
4.66640859, 6.402664032, 2.2270765159), RNATURALINC2013 = c(4.2307967093,
2.7883713049, 3.3458740787, 4.1899087829, 6.1952166656, 1.4698283977
), RNATURALINC2014 = c(4.3526195469, 2.89906461, 3.5387735378,
4.3622840605, 6.1662812026, 1.9053150433), RNATURALINC2015 =
c(4.0447227708,
2.5991346635, 3.1185939072, 4.0994738414, 5.8651140389, 1.6124771946
), RNATURALINC2016 = c(3.912431139, 2.6351400388, 3.123605796,
3.969532736, 5.4640183742, 1.5900546466), RNATURALINC2017 =
c(3.4634804902,
2.0606169731, 2.8735476848, 3.4715095687, 4.9943340813, 1.0731154898
), RNATURALINC2018 = c(3.1932048488, 1.8260576141, 2.687887693,
3.1462232219, 4.6998187519, 0.7766062675), RINTERNATIONALMIG2011 =
c(2.5539481982,
3.7247036946, 1.7438348531, 2.4715029092, 2.5385138982, 0.9841112772
), RINTERNATIONALMIG2012 = c(2.7460490726, 3.7275831375, 1.7993217139,
2.9505576333, 2.5429438207, 1.2219173785), RINTERNATIONALMIG2013 =
c(2.7017267715,
3.4759149144, 1.8781318506, 2.7997195452, 2.7121923767, 1.0597112344
), RINTERNATIONALMIG2014 = c(2.988275652, 3.9792291689, 1.9851256285,
3.0689308523, 3.0260314993, 0.7759790947), RINTERNATIONALMIG2015 =
c(3.3285982753,
4.0561842059, 2.1052580818, 3.5654043717, 3.5102060089, 0.9664136698
), RINTERNATIONALMIG2016 = c(3.3215493142, 4.2230961065, 2.1323795548,
3.5885415898, 3.2920380658, 1.2245437674), RINTERNATIONALMIG2017 =
c(2.9410856198,
3.8503376372, 1.8510505744, 3.2892897676, 2.6864164429, 0.6550398799
), RINTERNATIONALMIG2018 = c(3.0010858795, 4.0950670621, 1.8698304564,
3.3695510667, 2.6156748143, 0.685035969), RDOMESTICMIG2011 = c(0,
-2.879569389, -2.786843372, 2.9081645678, 0.1508443529, -0.467223314
), RDOMESTICMIG2012 = c(0, -3.686820778, -2.69589683, 2.8855541222,
0.6834160664, 0.0122732593), RDOMESTICMIG2013 = c(0, -3.872925953,
-1.835626629, 2.4903472978, 0.6316815776, 0.5475831286), RDOMESTICMIG2014
= c(0,
-4.903180146, -2.700781819, 3.1374707924, 1.1220952977, -0.156105573
), RDOMESTICMIG2015 = c(0, -6.067919504, -3.462920156, 3.7630900106,
1.6177886489, -0.320350145), RDOMESTICMIG2016 = c(0, -6.653555548,
-3.359190761, 3.6365043774, 2.0802759896, -0.40687782), RDOMESTICMIG2017 =
c(0,
-5.651919379, -2.370672066, 2.963134779, 1.4785645494, 0.4240305179
), RDOMESTICMIG2018 = c(0, -5.222289092, -2.301663494, 2.7793734944,
1.350093835, 1.1713623417), RNETMIG2011 = c(2.5539481982, 0.845134306,
-1.043008519, 5.379667477, 2.6893582511, 0.516887963), RNETMIG2012 =
c(2.7460490726,
0.0407623599, -0.896575116, 5.8361117555, 3.2263598871, 1.2341906378
), RNETMIG2013 = c(2.7017267715, -0.397011039, 0.0425052219,
5.2900668429, 3.3438739543, 1.6072943629), RNETMIG2014 = c(2.988275652,
-0.923950977, -0.71565619, 6.2064016447, 4.148126797, 0.6198735214
), RNETMIG2015 = c(3.3285982753, -2.011735298, -1.357662074,
7.3284943823, 5.1279946578, 0.6460635248), RNETMIG2016 = c(3.3215493142,
-2.430459441, -1.226811206, 7.2250459672, 5.3723140554, 0.8176659475
), RNETMIG2017 = c(2.9410856198, -1.801581742, -0.519621492,
6.2524245465, 4.1649809923, 1.0790703978), RNETMIG2018 = c(3.0010858795,
-1.12722203, -0.431833037, 6.1489245611, 3.9657686492, 1.8563983107
)), .Names = c("SUMLEV", "REGION", "DIVISION", "STATE", "NAME",
"CENSUS2010POP", "ESTIMATESBASE2010", "POPESTIMATE2010",
"POPESTIMATE2011",
"POPESTIMATE2012", "POPESTIMATE2013", "POPESTIMATE2014",
"POPESTIMATE2015",
"POPESTIMATE2016", "POPESTIMATE2017", "POPESTIMATE2018", "NPOPCHG_2010",
"NPOPCHG_2011", "NPOPCHG_2012", "NPOPCHG_2013", "NPOPCHG_2014",
"NPOPCHG_2015", "NPOPCHG_2016", "NPOPCHG_2017", "NPOPCHG_2018",
"BIRTHS2010", "BIRTHS2011", "BIRTHS2012", "BIRTHS2013", "BIRTHS2014",
"BIRTHS2015", "BIRTHS2016", "BIRTHS2017", "BIRTHS2018", "DEATHS2010",
"DEATHS2011", "DEATHS2012", "DEATHS2013", "DEATHS2014", "DEATHS2015",
"DEATHS2016", "DEATHS2017", "DEATHS2018", "NATURALINC2010",
"NATURALINC2011",
"NATURALINC2012", "NATURALINC2013", "NATURALINC2014", "NATURALINC2015",
"NATURALINC2016", "NATURALINC2017", "NATURALINC2018",
"INTERNATIONALMIG2010",
"INTERNATIONALMIG2011", "INTERNATIONALMIG2012", "INTERNATIONALMIG2013",
"INTERNATIONALMIG2014", "INTERNATIONALMIG2015", "INTERNATIONALMIG2016",
"INTERNATIONALMIG2017", "INTERNATIONALMIG2018", "DOMESTICMIG2010",
"DOMESTICMIG2011", "DOMESTICMIG2012", "DOMESTICMIG2013",
"DOMESTICMIG2014",
"DOMESTICMIG2015", "DOMESTICMIG2016", "DOMESTICMIG2017",
"DOMESTICMIG2018",
"NETMIG2010", "NETMIG2011", "NETMIG2012", "NETMIG2013", "NETMIG2014",
"NETMIG2015", "NETMIG2016", "NETMIG2017", "NETMIG2018", "RESIDUAL2010",
"RESIDUAL2011", "RESIDUAL2012", "RESIDUAL2013", "RESIDUAL2014",
"RESIDUAL2015", "RESIDUAL2016", "RESIDUAL2017", "RESIDUAL2018",
"RBIRTH2011", "RBIRTH2012", "RBIRTH2013", "RBIRTH2014", "RBIRTH2015",
"RBIRTH2016", "RBIRTH2017", "RBIRTH2018", "RDEATH2011", "RDEATH2012",
"RDEATH2013", "RDEATH2014", "RDEATH2015", "RDEATH2016", "RDEATH2017",
"RDEATH2018", "RNATURALINC2011", "RNATURALINC2012", "RNATURALINC2013",
"RNATURALINC2014", "RNATURALINC2015", "RNATURALINC2016",
"RNATURALINC2017",
"RNATURALINC2018", "RINTERNATIONALMIG2011", "RINTERNATIONALMIG2012",
"RINTERNATIONALMIG2013", "RINTERNATIONALMIG2014", "RINTERNATIONALMIG2015",
"RINTERNATIONALMIG2016", "RINTERNATIONALMIG2017", "RINTERNATIONALMIG2018",
"RDOMESTICMIG2011", "RDOMESTICMIG2012", "RDOMESTICMIG2013",
"RDOMESTICMIG2014",
"RDOMESTICMIG2015", "RDOMESTICMIG2016", "RDOMESTICMIG2017",
"RDOMESTICMIG2018",
"RNETMIG2011", "RNETMIG2012", "RNETMIG2013", "RNETMIG2014", "RNETMIG2015",
"RNETMIG2016", "RNETMIG2017", "RNETMIG2018"), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
In order to help you out, an example data using dput(head(population.data)) would be helpful. Based on your comments, your data is in what is called 'wide' format, meaning each observation is contained in a column, rather than a row (pupulation 2010, population 2011 etc.).
As i hinted in my comment, a sub-goal within statistical modelling is always to clean and reshape data to a proper format, that will work for running models. In this case the problem is that your format is in an incorrect shape. The most common is likely melting to long format via the reshape2 or data.table package as explained in this link. I personally prefer the data.table package, as it seems to have better large scale performance. Their usage however is identical.
Lets say you have a column 'NAME' for states and 9 columns for population estimates (2010 population estimates, 2011 population estimates and so on), we could then convert these columns into a long format, using melt from either of the two suggested packages (They are identical in use)
require(data.table)
value_columns <- paste(2010:2018, "Population Estimates")
population.data_long <- melt(population.data, id.vars = "NAME",
measure.vars = value_columns, #Columns containing values we (that are grouped by their column names)
variable.name = 'Year (Population Estimate)', #Name of the column which tells us [(Year) Population Estimate]
value.name = 'Population Estimate') #Name of the column with values
population.data_long$year <- as.integer(substr(population.data_long$`Year (Population Estimate)`, 1, 4)) #Create a year column in a bit of a hacky way
Note i have ignored any additional columns, and these should be included in your melt statement. From here on a linear regression should follow any standard example that you have found.
Any help would be greatly appreciated!!
I'm trying to create a choropleth map in R that shows the counties of texas, color-coded by their population ranges.
My problem is that the range of populations is too large. The highest population is over 4 million, but most of the counties have a population under 50,000. The criteria for the fill is: (0-1mil), (1-2mil), (2-3mil), (3-4mil), (4-5mil) but almost all fall under 0-1mil.
How can I change the legend to account for different ranges of numbers? For example, maybe:
(0-1,000), (1,000-10,000), (10,000-100,000), (100,000-1mil), (1mil-5mil)
Here's the code I wrote to plot the data:
txplot <- ggplot(txczpop, aes(fill=pop2014)) + geom_map(txmap)
tm_shape(txmap) +
tm_fill("pop2014", title="TX County Population", palette = "PRGn") +
tm_borders(alpha=.5) +
tm_style_beaver()
Here's the result:
[![enter image description here][1]][1]
I'm using a census county shapefile and population also retrieved from a census file.
Here's the output of my population data:
txczpop <- structure(list(county_fips = c(48001L, 48003L, 48005L, 48007L,
48009L, 48011L, 48013L, 48015L, 48017L, 48019L, 48021L, 48023L,
48025L, 48027L, 48029L, 48031L, 48033L, 48035L, 48037L, 48039L,
48041L, 48043L, 48045L, 48047L, 48049L, 48051L, 48053L, 48055L,
48057L, 48059L, 48061L, 48063L, 48065L, 48067L, 48069L, 48071L,
48073L, 48075L, 48077L, 48079L, 48081L, 48083L, 48085L, 48087L,
48089L, 48091L, 48093L, 48095L, 48097L, 48099L, 48101L, 48103L,
48105L, 48107L, 48109L, 48111L, 48113L, 48115L, 48117L, 48119L,
48121L, 48123L, 48125L, 48127L, 48129L, 48131L, 48133L, 48135L,
48137L, 48141L, 48139L, 48143L, 48145L, 48147L, 48149L, 48151L,
48153L, 48155L, 48157L, 48159L, 48161L, 48163L, 48165L, 48167L,
48169L, 48171L, 48173L, 48175L, 48177L, 48179L, 48181L, 48183L,
48185L, 48187L, 48189L, 48191L, 48193L, 48195L, 48197L, 48199L,
48201L, 48203L, 48205L, 48207L, 48209L, 48211L, 48213L, 48215L,
48217L, 48219L, 48221L, 48223L, 48225L, 48227L, 48229L, 48231L,
48233L, 48235L, 48237L, 48239L, 48241L, 48243L, 48245L, 48247L,
48249L, 48251L, 48253L, 48255L, 48257L, 48259L, 48261L, 48263L,
48265L, 48267L, 48269L, 48271L, 48273L, 48275L, 48283L, 48277L,
48279L, 48281L, 48285L, 48287L, 48289L, 48291L, 48293L, 48295L,
48297L, 48299L, 48301L, 48303L, 48305L, 48313L, 48315L, 48317L,
48319L, 48321L, 48323L, 48307L, 48309L, 48311L, 48325L, 48327L,
48329L, 48331L, 48333L, 48335L, 48337L, 48339L, 48341L, 48343L,
48345L, 48347L, 48349L, 48351L, 48353L, 48355L, 48357L, 48359L,
48361L, 48363L, 48365L, 48367L, 48369L, 48371L, 48373L, 48375L,
48377L, 48379L, 48381L, 48383L, 48385L, 48387L, 48389L, 48391L,
48393L, 48395L, 48397L, 48399L, 48401L, 48403L, 48405L, 48407L,
48409L, 48411L, 48413L, 48415L, 48417L, 48419L, 48421L, 48423L,
48425L, 48427L, 48429L, 48431L, 48433L, 48435L, 48437L, 48439L,
48441L, 48443L, 48445L, 48447L, 48449L, 48451L, 48453L, 48455L,
48457L, 48459L, 48461L, 48463L, 48465L, 48467L, 48469L, 48471L,
48473L, 48475L, 48477L, 48479L, 48481L, 48483L, 48485L, 48487L,
48489L, 48491L, 48493L, 48495L, 48497L, 48499L, 48501L, 48503L,
48505L, 48507L), county_name = c("Anderson", "Andrews", "Angelina",
"Aransas", "Archer", "Armstrong", "Atascosa", "Austin", "Bailey",
"Bandera", "Bastrop", "Baylor", "Bee", "Bell", "Bexar", "Blanco",
"Borden", "Bosque", "Bowie", "Brazoria", "Brazos", "Brewster",
"Briscoe", "Brooks", "Brown", "Burleson", "Burnet", "Caldwell",
"Calhoun", "Callahan", "Cameron", "Camp", "Carson", "Cass", "Castro",
"Chambers", "Cherokee", "Childress", "Clay", "Cochran", "Coke",
"Coleman", "Collin", "Collingsworth", "Colorado", "Comal", "Comanche",
"Concho", "Cooke", "Coryell", "Cottle", "Crane", "Crockett",
"Crosby", "Culberson", "Dallam", "Dallas", "Dawson", "Deaf Smith",
"Delta", "Denton", "DeWitt", "Dickens", "Dimmit", "Donley", "Duval",
"Eastland", "Ector", "Edwards", "El Paso", "Ellis", "Erath",
"Falls", "Fannin", "Fayette", "Fisher", "Floyd", "Foard", "Fort Bend",
"Franklin", "Freestone", "Frio", "Gaines", "Galveston", "Garza",
"Gillespie", "Glasscock", "Goliad", "Gonzales", "Gray", "Grayson",
"Gregg", "Grimes", "Guadalupe", "Hale", "Hall", "Hamilton", "Hansford",
"Hardeman", "Hardin", "Harris", "Harrison", "Hartley", "Haskell",
"Hays", "Hemphill", "Henderson", "Hidalgo", "Hill", "Hockley",
"Hood", "Hopkins", "Houston", "Howard", "Hudspeth", "Hunt", "Hutchinson",
"Irion", "Jack", "Jackson", "Jasper", "Jeff Davis", "Jefferson",
"Jim Hogg", "Jim Wells", "Johnson", "Jones", "Karnes", "Kaufman",
"Kendall", "Kenedy", "Kent", "Kerr", "Kimble", "King", "Kinney",
"Kleberg", "Knox", "La Salle", "Lamar", "Lamb", "Lampasas", "Lavaca",
"Lee", "Leon", "Liberty", "Limestone", "Lipscomb", "Live Oak",
"Llano", "Loving", "Lubbock", "Lynn", "Madison", "Marion", "Martin",
"Mason", "Matagorda", "Maverick", "McCulloch", "McLennan", "McMullen",
"Medina", "Menard", "Midland", "Milam", "Mills", "Mitchell",
"Montague", "Montgomery", "Moore", "Morris", "Motley", "Nacogdoches",
"Navarro", "Newton", "Nolan", "Nueces", "Ochiltree", "Oldham",
"Orange", "Palo Pinto", "Panola", "Parker", "Parmer", "Pecos",
"Polk", "Potter", "Presidio", "Rains", "Randall", "Reagan", "Real",
"Red River", "Reeves", "Refugio", "Roberts", "Robertson", "Rockwall",
"Runnels", "Rusk", "Sabine", "San Augustine", "San Jacinto",
"San Patricio", "San Saba", "Schleicher", "Scurry", "Shackelford",
"Shelby", "Sherman", "Smith", "Somervell", "Starr", "Stephens",
"Sterling", "Stonewall", "Sutton", "Swisher", "Tarrant", "Taylor",
"Terrell", "Terry", "Throckmorton", "Titus", "Tom Green", "Travis",
"Trinity", "Tyler", "Upshur", "Upton", "Uvalde", "Val Verde",
"Van Zandt", "Victoria", "Walker", "Waller", "Ward", "Washington",
"Webb", "Wharton", "Wheeler", "Wichita", "Wilbarger", "Willacy",
"Williamson", "Wilson", "Winkler", "Wise", "Wood", "Yoakum",
"Young", "Zapata", "Zavala"), pop2014 = c(57627L, 17477L, 87750L,
24972L, 8811L, 1955L, 47774L, 29114L, 6910L, 20892L, 78069L,
3592L, 32863L, 329140L, 1855866L, 10812L, 652L, 17780L, 93275L,
338124L, 209152L, 9173L, 1536L, 7194L, 37653L, 17253L, 44943L,
39810L, 21797L, 13513L, 420392L, 12621L, 6013L, 30261L, 7781L,
38145L, 50902L, 7089L, 10370L, 2935L, 3254L, 8430L, 885241L,
3017L, 20719L, 123694L, 13550L, 4050L, 38761L, 75562L, 1415L,
4950L, 3812L, 5899L, 2266L, 7135L, 2518638L, 13372L, 19195L,
5238L, 753363L, 20684L, 2218L, 11089L, 3543L, 11533L, 18176L,
153904L, 1879L, 833487L, 159317L, 40147L, 16989L, 33752L, 24833L,
3831L, 5949L, 1275L, 685345L, 10600L, 19762L, 18531L, 19425L,
314198L, 6435L, 25520L, 1291L, 7549L, 20462L, 23044L, 123534L,
123204L, 27172L, 147250L, 34720L, 3147L, 8199L, 5509L, 3928L,
55621L, 4441370L, 67336L, 6089L, 5769L, 185025L, 4180L, 79290L,
831073L, 34848L, 23577L, 53921L, 35921L, 22741L, 36651L, 3211L,
88493L, 21773L, 1574L, 8855L, 14739L, 35552L, 2204L, 252235L,
5255L, 41353L, 157456L, 19936L, 14906L, 111236L, 38880L, 400L,
785L, 50562L, 4438L, 262L, 3526L, 32190L, 3858L, 7474L, 49523L,
13574L, 20156L, 19721L, 16742L, 16861L, 78117L, 23524L, 3553L,
12091L, 19510L, 86L, 293974L, 5771L, 13861L, 10149L, 5460L, 4071L,
36519L, 57023L, 8199L, 243441L, 805L, 47894L, 2147L, 155830L,
24256L, 4870L, 9076L, 19416L, 518947L, 22148L, 12743L, 1153L,
65301L, 48195L, 14138L, 15093L, 356221L, 10758L, 2070L, 83433L,
28096L, 23769L, 123164L, 9908L, 15893L, 46079L, 121627L, 6976L,
11032L, 128220L, 3755L, 3371L, 12446L, 14349L, 7302L, 928L, 16500L,
87809L, 10416L, 53923L, 10350L, 8610L, 27099L, 66915L, 5622L,
3162L, 17328L, 3343L, 25515L, 3084L, 218842L, 8694L, 62955L,
9405L, 1339L, 1403L, 3972L, 7581L, 1945360L, 135143L, 927L, 12739L,
1608L, 32506L, 116608L, 1151145L, 14224L, 21418L, 40354L, 3454L,
27117L, 48974L, 52910L, 91081L, 69789L, 46820L, 11625L, 34438L,
266673L, 41168L, 5714L, 132355L, 12973L, 21903L, 489250L, 46402L,
7821L, 61638L, 42852L, 8286L, 18350L, 14319L, 12267L)), .Names = c("county_fips",
"county_name", "pop2014"), row.names = c(5100L, 5101L, 5103L,
5106L, 5107L, 5109L, 5112L, 5114L, 5116L, 5118L, 5120L, 5121L,
5124L, 5126L, 5128L, 5129L, 5131L, 5133L, 5136L, 5137L, 5140L,
5141L, 5143L, 5146L, 5147L, 5150L, 5152L, 5153L, 5156L, 5158L,
5159L, 5161L, 5163L, 5166L, 5168L, 5170L, 5171L, 5174L, 5176L,
5178L, 5179L, 5182L, 5183L, 5185L, 5188L, 5190L, 5192L, 5194L,
5195L, 5198L, 5200L, 5201L, 5203L, 5205L, 5208L, 5209L, 5212L,
5214L, 5215L, 5218L, 5219L, 5221L, 5224L, 5226L, 5228L, 5230L,
5232L, 5233L, 5235L, 5239L, 5237L, 5242L, 5244L, 5245L, 5248L,
5249L, 5251L, 5254L, 5256L, 5257L, 5260L, 5261L, 5264L, 5265L,
5268L, 5270L, 5272L, 5274L, 5276L, 5278L, 5280L, 5281L, 5284L,
5286L, 5288L, 5290L, 5292L, 5293L, 5296L, 5298L, 5300L, 5301L,
5303L, 5306L, 5308L, 5309L, 5312L, 5314L, 5316L, 5317L, 5319L,
5321L, 5323L, 5326L, 5327L, 5330L, 5332L, 5334L, 5335L, 5337L,
5339L, 5341L, 5343L, 5346L, 5348L, 5349L, 5352L, 5354L, 5356L,
5357L, 5360L, 5362L, 5364L, 5365L, 5368L, 5369L, 5372L, 5374L,
5382L, 5376L, 5378L, 5379L, 5383L, 5385L, 5388L, 5390L, 5392L,
5394L, 5396L, 5398L, 5400L, 5401L, 5404L, 5412L, 5413L, 5416L,
5418L, 5419L, 5421L, 5406L, 5407L, 5409L, 5423L, 5425L, 5427L,
5429L, 5432L, 5434L, 5435L, 5438L, 5440L, 5442L, 5443L, 5446L,
5448L, 5449L, 5451L, 5453L, 5456L, 5457L, 5460L, 5461L, 5464L,
5465L, 5468L, 5470L, 5472L, 5474L, 5476L, 5477L, 5480L, 5482L,
5484L, 5486L, 5488L, 5489L, 5491L, 5494L, 5496L, 5498L, 5499L,
5501L, 5504L, 5505L, 5508L, 5510L, 5511L, 5514L, 5516L, 5518L,
5520L, 5522L, 5524L, 5526L, 5527L, 5530L, 5531L, 5533L, 5536L,
5537L, 5540L, 5542L, 5544L, 5546L, 5547L, 5550L, 5552L, 5554L,
5555L, 5558L, 5559L, 5562L, 5563L, 5566L, 5568L, 5569L, 5571L,
5574L, 5575L, 5578L, 5579L, 5582L, 5584L, 5585L, 5587L, 5590L,
5592L, 5594L, 5595L, 5598L, 5600L, 5602L, 5604L, 5606L), class = "data.frame")
I just created a new column in the population dataframe that summarizes the population based on the ranges that I want to use, and then use that as the criteria for the fill:
txczpop$poprange[txczpop$pop2014 >= 0 & txczpop < 1000] <- "0-1,000"
txczpop$poprange[txczpop$pop2014 >= 1000 & txczpop < 10000] <- "1-10,000"
txczpop$poprange[txczpop$pop2014 >= 10000 & txczpop$pop2014 < 100000] <- "10,000-100,000"
txczpop$poprange[txczpop$pop2014 >= 100000 & txczpop$pop2014 < 1000000] <- "100,000 - 1,000,000"
txczpop$poprange[txczpop$pop2014 >= 1000000 & txczpop$pop2014 <= 5000000] <- "1,000,000 - 5,000,000"
I'm trying to fit some curves from the data below with a mono-exponential "decay". Graphical display is not as important as is pulling out the time constant. the y-axis is pA and the x is time in seconds.
dput(stackover_data)
structure(list(Time = c(0.09990001, 0.19990001, 0.29990001, 0.39990001,
0.49990001, 0.59990001, 0.69990001, 0.79990001, 0.89990001, 0.99990001,
1.09990001, 1.19990001, 1.29990001, 1.39990001, 1.49990001, 1.59990001,
1.69990001, 1.79990001, 1.89990001, 1.99990001, 2.09990001, 2.19990001,
2.29990001, 2.39990001, 2.49990001, 2.59990001, 2.69990001, 2.79990001,
2.89990001, 2.99990001, 3.09990001, 3.19990001, 3.29990001, 3.39990001,
3.49990001, 3.59990001, 3.69990001, 3.79990001, 3.89990001, 3.99990001,
4.09990001, 4.19990001, 4.29990001, 4.39990001, 4.49990001, 4.59990001,
4.69990001, 4.79990001, 4.89990001, 4.99990001, 5.09990001, 5.19990001,
5.29990001, 5.39990001, 5.49990001, 5.59990001, 5.69990001, 5.79990001,
5.89990001, 5.99990001, 6.09990001, 6.19990001, 6.29990001, 6.39990001,
6.49990001, 6.59990001, 6.69990001, 6.79990001, 6.89990001, 6.99990001,
7.09990001, 7.19990001, 7.29990001, 7.39990001, 7.49990001, 7.59990001,
7.69990001, 7.79990001, 7.89990001, 7.99990001, 8.09990001, 8.19990001,
8.29990001, 8.39990001, 8.49990001, 8.59990001, 8.69990001, 8.79990001,
8.89990001, 8.99990001, 9.09990001, 9.19990001, 9.29990001, 9.39990001,
9.49990001, 9.59990001, 9.69990001, 9.79990001, 9.89990001, 9.99990001,
10.09990001, 10.19990001, 10.29990001, 10.39990001, 10.49990001,
10.59990001, 10.69990001, 10.79990001, 10.89990001, 10.99990001,
11.09990001, 11.19990001, 11.29990001, 11.39990001, 11.49990001,
11.59990001, 11.69990001, 11.79990001, 11.89990001, 11.99990001,
12.09990001, 12.19990001, 12.29990001, 12.39990001, 12.49990001,
12.59990001, 12.69990001, 12.79990001, 12.89990001, 12.99990001,
13.09990001, 13.19990001, 13.29990001, 13.39990001, 13.49990001,
13.59990001, 13.69990001, 13.79990001, 13.89990001, 13.99990001,
14.09990001, 14.19990001, 14.29990001, 14.39990001, 14.49990001,
14.59990001, 14.69990001, 14.79990001, 14.89990001, 14.99990001,
15.09990001, 15.19990001, 15.29990001, 15.39990001, 15.49990001,
15.59990001, 15.69990001, 15.79990001, 15.89990001, 15.99990001,
16.09990001, 16.19990001, 16.29990001, 16.39990001, 16.49990001,
16.59990001, 16.69990001, 16.79990001, 16.89990001, 16.99990001,
17.09990001, 17.19990001, 17.29990001, 17.39990001, 17.49990001,
17.59990001, 17.69990001, 17.79990001, 17.89990001, 17.99990001,
18.09990001, 18.19990001, 18.29990001, 18.39990001, 18.49990001,
18.59990001, 18.69990001, 18.79990001, 18.89990001, 18.99990001,
19.09990001, 19.19990001, 19.29990001, 19.39990001, 19.49990001,
19.59990001, 19.69990001, 19.79990001, 19.89990001, 19.99990001
), `Trace 1` = c(-3.08656892325052, 9.36821982641837, 8.13806079083122,
10.7039590839898, 7.25670468903547, 4.31122291688919, 1.77905971163193,
-6.27606834721828, -8.65955381985049, -10.1445673910916, -10.6649772153892,
-6.52301948183154, -11.9757817854835, -0.976254254762154, 0.237467076202677,
-11.8114896779541, -11.0022757370468, -16.1845427042923, 2.70565927469852,
-18.9048281652216, -22.153682283437, -4363.32044948884, -3470.59111611883,
-2877.51064886248, -2253.51505229908, -1876.62974792002, -1541.14546478629,
-1288.26617158403, -1232.52313999354, -1042.98549163259, -938.795146277054,
-810.913567086442, -736.390770574588, -682.044521632168, -638.575324886466,
-596.587948389699, -629.282103146111, -569.200610245336, -587.387817942122,
-521.939307772762, -533.776693538631, -514.782451411908, -445.949395199026,
-451.026618716539, -461.669600651513, -442.372477296489, -426.132547857502,
-452.471101919398, -449.377497412324, -436.817659066873, -487.464805660851,
-442.915455035179, -458.666741489705, -454.990437793055, -455.173154690614,
-426.885702219019, -456.985443835707, -408.869318611773, -442.418742303429,
-407.42593099033, -414.538253423006, -316.036755461507, -248.395118743017,
-198.708015370115, -183.88480278352, -160.041754268138, -139.020785864805,
-123.701641615743, -137.253323547789, -124.240619047461, -121.512859107349,
-113.719386521321, -102.98429740535, -118.721098087137, -109.789471870234,
-122.145399997255, -109.542467905009, -96.6725326170008, -108.16233877188,
-94.1092968366083, -88.0987495250118, -89.2245425850472, -96.3495439993499,
-110.340434956898, -98.8777265479938, -88.0674013452629, -102.755317774957,
-91.0752842494157, -107.959830375198, -78.8424385901398, -93.7743443479161,
-98.3591889757604, -72.3214090579355, -85.6296201608712, -112.596656084088,
-115.518068650615, -110.973655632476, -78.8021307215932, -85.6443070182152,
-93.1744356389988, -100.483987323044, -87.6672104421484, -83.6481077757535,
-82.1465876740715, -86.7838666454595, -75.9066755520263, -86.6416980998645,
-88.9405806921788, -74.0592581080291, -86.3433338300531, -93.5114839187431,
-91.1875849041866, -82.7018083540351, -102.859075734953, -82.1494206590809,
-79.2197323780198, -78.1558787387238, -86.2649418863144, -93.5271994290692,
-84.6678528566242, -71.4828270654073, -69.5618263581887, -67.2920558863641,
-58.1490330439793, -59.8163238740351, -73.6957797622946, -61.7673947702343,
-63.6492255747164, -64.043638367468, -73.8301991524909, -69.3055992018769,
-65.9342860783478, -74.9891521715357, -74.5779808619617, -69.9029875902787,
-69.746935396023, -71.35455530782, -73.5279471991205, -77.8000626250279,
-74.6065254864801, -64.0834786591292, -45.1346212136811, -43.1615385011179,
-42.554841323715, -50.276566542849, -56.1940469314277, -49.6368473019083,
-53.9842269565738, -54.1480156577708, -48.2160751112714, -43.2902743874793,
-46.4358385791469, -58.4459814967147, -49.0443619038303, -56.8007684056415,
-56.3345802020277, -40.1704617527471, -38.2624410086947, -39.9339279963857,
-27.7637250414188, -40.8686520798649, -30.9246406597275, -32.70021580322,
-28.7536071219405, -32.4403271637661, -30.3389870650563, -23.2866770834185,
-40.349370835336, -39.7478049410975, -38.4648592612063, -37.4191914226093,
-52.1259539643142, -38.0993644763616, -37.5493511061199, -25.0794144286873,
-32.6165749053184, -17.2616237291667, -32.1515419380766, -27.0814392288745,
-26.2440551871993, -17.5882979851251, -19.852012057918, -22.1408312947152,
-21.3395092026716, -28.0789344732281, -19.6443281019371, -26.6585778398899,
-24.2994817465722, -27.847437388783, -22.0565059455712, -24.8747905836351,
-30.7544250721296, -29.9924061105416, -22.5728855329732, -24.9767037068316,
-18.8208568928653, -10.7306087216159, -16.4281210876173, -19.3057174287183,
-15.9745523586581), `Trace 2` = c(5.94927992143286, 1.42121402161905,
6.78788136514507, 3.33970424403748, -3.73956433922802, -7.3097330836793,
-9.18242380095097, 3.29952017048882, 5.17208246028, 1.53238537592179,
6.90832098860733, 3.16748380079213, 5.49988319742749, 3.86758484926656,
11.981378748128, -0.208159377585758, -7.2781503569274, 1.71389537416221,
-4.77614396689646, -0.561115871778583, -14.5385068323377, -4944.63006397872,
-3891.23141079918, -3065.66300899921, -2527.61853791531, -2060.05223709386,
-1763.27814995793, -1451.77884853301, -1368.42409410389, -1115.3385743493,
-990.942590410893, -852.951182088994, -920.516421149141, -860.486324532349,
-751.257935697359, -768.757880984424, -713.935725464811, -715.011716140783,
-616.355633698422, -672.133827760044, -629.418857335266, -607.342562427532,
-599.657392949772, -553.265193759536, -572.878306401819, -583.153736972572,
-589.661926862833, -527.552306430313, -553.672277977987, -539.906254466276,
-525.491319968336, -564.876755897633, -505.363603776286, -575.649358123809,
-497.897048596953, -544.360501913781, -483.501835830955, -533.085516954168,
-478.007776808595, -453.333986756674, -447.125338584774, -380.268661132557,
-253.127091546528, -227.877427383931, -191.488369613846, -166.799480062655,
-171.446527550207, -156.775073739292, -148.804014388301, -151.79670303645,
-152.599645619669, -113.40778617347, -133.161498039771, -129.929086626311,
-156.827022418459, -146.618934465797, -94.20997521855, -124.514491421933,
-89.8538131075507, -91.6500841191965, -113.887119052802, -109.971138752404,
-122.532101557384, -107.714865018275, -120.258046104427, -133.296582343699,
-111.726093380577, -121.533575165211, -93.2177144160483, -97.3820468959891,
-111.294540122284, -100.913019601926, -90.8456358331201, -94.143801546019,
-89.9179990101121, -79.3197988311624, -87.0020882554621, -103.315348613102,
-92.6091563297179, -87.3900461813473, -106.61173718685, -117.18534688083,
-106.417237368758, -114.977458547258, -101.759575332149, -93.2156781534656,
-79.4452885332048, -83.8035016923155, -85.7770898942133, -80.151862850598,
-72.4787832080729, -75.4887322569144, -70.8074563945233, -76.9714115362765,
-74.2305756099668, -72.7994824773122, -86.4890002605112, -68.5241155035978,
-76.723559709657, -60.5721619300473, -64.185161607482, -81.0777473166996,
-59.7023719535658, -48.0615168793405, -63.7668096308392, -69.0412888697281,
-70.9894443873153, -48.8366672284658, -43.1003899300401, -50.5591327356918,
-65.184353265464, -52.2850955061746, -66.0074402595226, -69.3081002474696,
-69.3876079446205, -53.0919105504865, -69.3289906081927, -63.5198462609014,
-70.9316181916525, -68.753726760587, -57.1360889764902, -45.7297495037924,
-45.0225501894778, -45.7487568762369, -46.486850874694, -46.7437348684622,
-41.1396209831975, -38.5056250833188, -49.8076229939374, -46.9868446480308,
-38.4639795698766, -40.8336417843587, -49.9810721301961, -58.7820616810459,
-51.4348042554539, -56.9132093579117, -53.527642322397, -49.7620292214747,
-54.6821412367706, -66.7962460110546, -52.0791798888695, -43.3022792658504,
-47.5584813904168, -36.3352478868053, -39.8699383685185, -43.9973042247199,
-49.3191665577514, -42.0818922168797, -37.7538461067954, -29.1591142899914,
-31.6815992654018, -35.3664789321372, -29.6435427630652, -45.5528322348158,
-37.0568327299924, -29.6739001502498, -37.0952936191428, -44.1786405275305,
-50.929131558954, -31.7842698176079, -31.4300485665082, -33.960404707688,
-34.0566040705914, -41.2299273666286, -30.1094672132779, -28.6802701495622,
-37.1973277887207, -31.7114680610898, -32.5450840790921, -28.5823680785893,
-27.5268500574853, -15.2389866754792, -19.2079199191329, -28.9156878395334,
-16.5863741697772, -31.6638747515138, -34.679018047114, -32.6358481677933,
-26.3463300811612, -18.542719903123), `Trace 3` = c(4.74984904312473,
-6.83237755613888, -4.15774846489683, 0.0116753456567835, 4.3974208810568,
8.56175743226069, -0.271281722577697, -10.7982854290066, -18.31704442037,
-15.7577103721081, -20.5775487342614, -17.7307614062074, -14.2844531791322,
-5.43267691733291, 2.52755064598145, -0.79977817636393, -8.07074631773528,
-14.6631228094247, -9.70657043283029, -11.4521183354252, -10.8401698276907,
-5495.45466682125, -4324.53856178012, -3525.03574657791, -2774.37269062517,
-2175.8494069729, -1790.54797875842, -1574.71991002551, -1342.90235937345,
-1212.59715782746, -1042.56659527622, -1006.18885759924, -962.27473179716,
-870.459562514915, -847.89784200018, -770.205392585522, -709.279500982242,
-740.484104629636, -739.054108223511, -645.039948927494, -712.347389757369,
-609.734365947307, -613.120930696929, -630.805982834916, -581.828460576409,
-608.843958294059, -602.432567890874, -547.023947351376, -571.475614300591,
-561.707380041648, -535.094375334464, -578.783728464712, -535.290908514374,
-489.922390143035, -522.824368605682, -520.322259788742, -556.271028784585,
-536.474251127913, -546.811709881227, -541.304029428261, -563.702125503968,
-460.326259578172, -348.718859066443, -261.740363110507, -244.084411205067,
-182.533320181224, -205.356578064485, -157.759947570376, -162.214202634405,
-180.011654146528, -176.904445787135, -167.116794266109, -163.416234638164,
-155.821745831568, -134.760994564946, -131.702791057298, -138.598664148971,
-139.818370538306, -134.700400696581, -137.275258643317, -119.921721377143,
-118.187525264885, -118.490007264421, -130.391338033262, -138.05114734155,
-127.505366899025, -132.636233878671, -123.490111075107, -113.42942794286,
-122.40925776411, -114.48254277334, -122.78054774327, -90.9528981496313,
-96.9508488566491, -107.842237007564, -91.0521871208113, -91.0347011279681,
-85.7054147126896, -89.7363815316723, -77.6588521980595, -76.6733051688234,
-85.821987540449, -90.3216600489126, -66.1137192454051, -77.7157754909198,
-89.8637060331546, -80.9149315468257, -71.7434481475478, -97.6600776026528,
-103.405900069071, -83.7502802699966, -77.4600968679799, -77.8705250264031,
-78.9012879762878, -87.4698127883497, -92.9587198547098, -96.1222986385502,
-91.1990314226718, -83.8744116889207, -61.7720026226848, -83.6616594930646,
-91.3899042204887, -74.2552104995002, -94.3117384183545, -81.130091800586,
-87.6638514042855, -79.061763018934, -78.7472359255489, -72.9642062301352,
-83.5759889935287, -74.066505526926, -62.6191749912397, -75.7150349466975,
-71.8704211546101, -68.4798935028027, -77.0470926055507, -76.6024078920054,
-66.5760393943856, -77.8848130194484, -77.5151449198185, -65.5482413040489,
-47.6087884294611, -35.6865291924552, -47.9648700291328, -62.8176093048552,
-48.969044800301, -46.853228499419, -52.562083189922, -58.7677963803543,
-63.6615259259388, -51.7351340481719, -54.5967510836755, -54.1294816023731,
-47.7621427567758, -40.9244974392914, -50.5286026229961, -45.9961132210891,
-43.143208905187, -43.8395623054062, -49.3326618452772, -48.308625153823,
-44.9833219970479, -32.4206723427767, -45.6628898472981, -41.4748918817457,
-43.6438737242104, -42.9844450147366, -30.3729810016378, -44.9519080631137,
-45.8241111308495, -40.4766896430523, -40.3007872752484, -46.4770136239361,
-49.3759491415156, -46.0844075639024, -34.3090862431636, -26.6047381016158,
-28.9119815377167, -33.1619464253006, -37.3813739690468, -46.5001186141503,
-54.5235621985407, -44.5233400090119, -55.2272273265567, -48.088333647706,
-55.3522589332341, -52.8146474401922, -60.1877313269088, -48.2512741329643,
-34.8540879147507, -55.2019332852645, -50.8488894007021, -49.9600753927381,
-48.5654335180739, -47.8918651630979, -45.1405419376454, -40.8504490169926,
-38.5815843253789, -45.001677748311, -43.0547862406721)), .Names = c("Time",
"Trace 1", "Trace 2", "Trace 3"), row.names = c(1000L, 2000L,
3000L, 4000L, 5000L, 6000L, 7000L, 8000L, 9000L, 10000L, 11000L,
12000L, 13000L, 14000L, 15000L, 16000L, 17000L, 18000L, 19000L,
20000L, 21000L, 22000L, 23000L, 24000L, 25000L, 26000L, 27000L,
28000L, 29000L, 30000L, 31000L, 32000L, 33000L, 34000L, 35000L,
36000L, 37000L, 38000L, 39000L, 40000L, 41000L, 42000L, 43000L,
44000L, 45000L, 46000L, 47000L, 48000L, 49000L, 50000L, 51000L,
52000L, 53000L, 54000L, 55000L, 56000L, 57000L, 58000L, 59000L,
60000L, 61000L, 62000L, 63000L, 64000L, 65000L, 66000L, 67000L,
68000L, 69000L, 70000L, 71000L, 72000L, 73000L, 74000L, 75000L,
76000L, 77000L, 78000L, 79000L, 80000L, 81000L, 82000L, 83000L,
84000L, 85000L, 86000L, 87000L, 88000L, 89000L, 90000L, 91000L,
92000L, 93000L, 94000L, 95000L, 96000L, 97000L, 98000L, 99000L,
100000L, 101000L, 102000L, 103000L, 104000L, 105000L, 106000L,
107000L, 108000L, 109000L, 110000L, 111000L, 112000L, 113000L,
114000L, 115000L, 116000L, 117000L, 118000L, 119000L, 120000L,
121000L, 122000L, 123000L, 124000L, 125000L, 126000L, 127000L,
128000L, 129000L, 130000L, 131000L, 132000L, 133000L, 134000L,
135000L, 136000L, 137000L, 138000L, 139000L, 140000L, 141000L,
142000L, 143000L, 144000L, 145000L, 146000L, 147000L, 148000L,
149000L, 150000L, 151000L, 152000L, 153000L, 154000L, 155000L,
156000L, 157000L, 158000L, 159000L, 160000L, 161000L, 162000L,
163000L, 164000L, 165000L, 166000L, 167000L, 168000L, 169000L,
170000L, 171000L, 172000L, 173000L, 174000L, 175000L, 176000L,
177000L, 178000L, 179000L, 180000L, 181000L, 182000L, 183000L,
184000L, 185000L, 186000L, 187000L, 188000L, 189000L, 190000L,
191000L, 192000L, 193000L, 194000L, 195000L, 196000L, 197000L,
198000L, 199000L, 200000L), class = "data.frame")
I've tried doing lm(y~x)but it doesn't seem to get the right answer (verified the right answer in Igor) and obviously this is because its a linear model and not a exponential. Any all suggestions are welcomed. I'm struggling on this!
Thanks all!
names(DF) <- make.names(names(DF))
plot(Trace.1 ~ Time, data = DF)
#remove the initial values that clearly don't follow the model
DF1 <- DF[-seq_len(which((diff(DF$Trace.1) < -1e3))),]
plot(Trace.1 ~ Time, data = DF1)
fit <- nls(Trace.1 ~ SSasymp(Time, Asym, R0, lrc), data = DF1)
summary(fit)
coef(fit)
help("SSasymp") #for an explanation of the parameters
lines(DF1$Time, predict(fit))
I'm not used to working with time series data in R, and I'm a bit stuck with this. I have a data frame of event references and the data the event was recorded. The data runs over a period of 7 years and want to summarise it into the number of event per month over the 7 year period and plot that with ggplot2.
I can't seem to get the date conversions to work together so I end up with a count and a date I can feed to ggplot2's scale_x_date() function
Here's an example of the data:
df <- structure(list(Ref = structure(c(127L, 33L, 232L, 392L, 490L,
242L, 437L, 346L, 443L, 560L, 598L, 568L, 103L, 262L, 463L, 17L,
114L, 276L, 361L, 422L), .Label = c("01090013", "0109005", "0109006",
"0109007", "0109009", "0109010", "0109011", "0109012", "0109014",
"0109016", "0109022", "0110001", "0110004", "0110007", "0110009",
"0110011", "0111001", "0111002", "0111012", "0111016", "0111017",
"0112001", "0112003", "0112008", "0112010", "015004", "015006",
"015008", "015010", "015013", "016002", "016003", "016004", "016005",
"016006", "016008", "016009", "016010", "016011", "016013", "016014",
"016016", "017001", "018001", "018004", "018005", "018007", "018008",
"018009", "020626", "0209024", "0209025", "0209026", "0209027",
"0209029", "0209031", "0209035", "0209037", "02100020", "0210017",
"0210018", "0210023", "0210026", "0210030", "0211018", "0211019",
"0211020", "0211022", "0211024", "0211025", "0211026", "0212018",
"0212021", "0212025", "0212027", "025018", "025021", "025022",
"025023", "025024", "025025", "025026", "025030", "026019", "026020",
"026021", "026023", "026025", "026027", "026030", "026032", "0270010",
"027010", "027012", "027013", "027014", "027016", "027017", "0309038",
"0309039", "0309041", "0309046", "0309050", "0309052", "0309053",
"0310035", "0310037", "0310041", "0310043", "0310044", "0311028",
"0311032", "0311035", "0311038", "0312031", "0312036", "0312037",
"0312043", "0312045", "0312047", "0312056", "0312058", "0312059",
"0312062", "035033", "035034", "035036", "035037", "035038",
"035040", "035041", "035042", "035043", "035045", "035049", "036036",
"036038", "036039", "036041", "036042", "036044", "036045", "036046",
"036047", "036048", "036050", "036051", "037021", "037026", "037029",
"038026", "038032", "038034", "038035", "038036", "0409056",
"0409057", "0409062", "0410046", "0410049", "0410050", "0410051",
"0410054", "0410055", "0410056", "0410057", "0410058", "0410060",
"0410062", "0410064", "0411047", "0411051", "0411052", "0411055",
"0412070", "0412074", "0412075", "0412076", "045054", "045056",
"045058", "045063", "045064", "045065", "045072", "046054", "046055",
"046058", "046060", "047035", "047036", "047037", "047038", "047041",
"047042", "047044", "047045", "047046", "048040", "048043", "048044",
"048045", "048048", "048050", "048051", "0509073", "0509080",
"0510066", "0510067", "0510082", "0511062", "0511065", "0511068",
"0511069", "0511072", "0512084", "0512088", "0512089", "0512091",
"055073", "055075", "055080", "055086", "055089", "055091", "055093",
"055094", "055095", "056064", "056066", "056067", "056068", "056070",
"056071", "056073", "056074", "057049", "057052", "057053", "057054",
"057058", "057059", "057060", "057061", "057063", "057065", "057066",
"057067", "057068", "057069", "058053", "058055", "058056", "058059",
"058062", "058064", "0609082", "0609086", "0609088", "0609089",
"0609090", "0609093", "0609095", "0609096", "0609097", "0609098",
"0609103", "0610086", "0610089", "0610095", "0610096", "0610098",
"0611073", "0611074", "0611080", "0611081", "0612109", "0612115",
"065096", "065099", "065103", "065105", "065106", "065109", "065114",
"066075", "066076", "066077", "066078", "066081", "066083", "067080",
"067081", "067084", "068065", "068070", "068074", "0709106",
"0709108", "0709113", "0709115", "0709116", "0709117", "0709120",
"0710104", "0710105", "0710107", "0710108", "0710110", "0710115",
"0710116", "0710117", "0710123", "0711083", "0711084", "0711085",
"0711086", "0711087", "0711088", "0711092", "0712122", "0712126",
"0712127", "0712128", "0712129", "075118", "075119", "075123",
"075124", "075125", "075126", "075127", "075130", "075132", "075133",
"076084", "076087", "076088", "076090", "076092", "076093", "076094",
"077103", "077105", "078079", "078080", "078081", "078082", "078085",
"078086", "0809126", "0809134", "0809137", "0809141", "0809143",
"0810125", "0810137", "0811099", "0811101", "0811106", "0811108",
"0811112", "0811113", "0811114", "0812142", "0812145", "0812150",
"0812152", "0814143", "085139", "085143", "085145", "085148",
"085149", "085150", "085154", "085156", "085160", "085163", "086098",
"086099", "086100", "086101", "086102", "086104", "086107", "086108",
"086109", "086110", "086111", "086112", "086114", "086115", "087106",
"087107", "087109", "087112", "088094", "088096", "088097", "088098",
"0909145", "0909155", "0909158", "0910145", "0910146", "0910147",
"0910149", "0910150", "0910153", "0910154", "0911116", "0911117",
"0911120", "0911121", "0911122", "0911123", "0911124", "0911130",
"0911131", "0912161", "0912163", "0912168", "0912171", "0912172",
"095166", "095167", "095170", "095171", "095172", "095178", "095180",
"096116", "096117", "096121", "097120", "097124", "097125", "097126",
"097132", "097133", "097136", "098110", "098115", "098116", "098119",
"100006825", "100006830", "1009160", "1009161", "1009162", "1009164",
"1009165", "1009166", "1009169", "1009170", "1009172", "1009173",
"1009174", "1010160", "1010162", "1010163", "1010164", "1010166",
"1010168", "1011133-A", "1011134", "1011140", "1011142", "1012179",
"1012184", "1012185", "1012194", "105185", "105186", "105187",
"105188", "105189", "105191", "105192", "105196", "105197", "105198",
"105199", "105201", "105202", "105207", "105208", "105211", "106127",
"106130", "106131", "107138", "107140", "107143", "107147", "107148",
"107149", "107153", "107155", "107156", "108122", "108123", "108127",
"108129", "108130", "108131", "108132", "108134", "108135", "108136",
"1109175", "1109176", "1109180", "1109182", "1110173", "1110176",
"1110177", "1110178", "1110185", "1110186", "1111145", "1111150",
"1111151", "1112196", "1112197", "1112201", "1112202", "1112206",
"1112208", "1112209", "1112212", "1112218", "1112220", "1112223",
"1112225", "1112226", "1112227", "115215", "115216", "115217",
"115218", "115219", "115223", "115225", "115226", "116139", "116143",
"116144", "116145", "117161", "117162", "117164", "117165", "117168",
"117175", "117180", "118139", "118140", "118143", "118147", "118148",
"118150", "118152", "118154", "118157", "118160", "118161", "1209188",
"1209189", "1209191", "1209193", "1209199", "1210191", "1210193",
"1211157", "1211158", "1211168", "1211169", "1211170", "1211171",
"1211173", "1212233", "1212235", "1212240", "125231", "125238",
"125241", "126147", "126149", "127182", "127183", "127186", "127187",
"127192", "127194", "128165", "128168", "128169", "128171", "128172",
"128175", "128176", "128177", "128182", "128183", "128184", "128186",
"128189", "128193"), class = "factor"), Date = structure(c(12846,
13154, 13284, 13391, 13434, 13655, 13766, 14067, 14119, 14183,
14209, 14211, 14322, 14412, 14897, 14960, 15049, 15155, 15201,
15597), class = "Date")), .Names = c("Ref", "Date"), row.names = c(NA,
-20L), class = "data.frame")
This is driving me crazy!
Thanks
H
I believe you are looking for this:
df <- transform(df, month = format(Date,"%m"), year = format(Date, "%Y"))
counts <- ddply(df,.(month,year),nrow)
Then to plot the date:
# make a new monthly date
counts <- transform(counts, new_date = as.Date(paste(year,month,'01',sep="-")))
# now plot
ggplot(counts,aes(x=new_date,y=V1)) + geom_point() + scale_x_date()
xts package is very handy for time series manipulations.
First I create the xts object :
library(xts)
dat.xts <- xts(df$Ref,order.by=as.POSIXct(df$Date))
Then I use apply.monthly to get the count by day, and plot it as xts object
count.month <- apply.monthly(dat.xts,FUN=length)
plot(count.month, type='b')
If you want to use ggplot2, you can transform the result to a data.frame.
as.data.frame(count.month)
Another option:
data$Month <- format(as.POSIXct(data$Date), "%Y-%m")
by.month.count <- data.frame(with(data, table(Month)))