Set different colour for each geom line with ggplot - r

I have this data
diffdec <- structure(list(ma = structure(c(14975, 14975, 15006, 15006, 15034, 5034, 15065, 15065, 15095, 15095, 15126, 15126, 15156, 15156, 15187, 15187, 15218, 15218, 15248, 15248, 15279, 15279, 15309, 15309, 15340, 15340, 15371, 15371, 15400, 15400, 15431, 15431, 15461, 15461, 15492, 15492, 15522, 15522, 15553, 15553, 15584, 15584, 15614, 15614, 15645, 15645, 15675, 15675, 15706, 15706, 15737, 15737, 15765, 15765, 15796, 15796, 15826, 15826, 15857, 15857, 15887, 15887, 15918, 15918, 15949, 15949, 15979, 15979, 16010, 16010, 16040, 16040, 16071, 16071, 16102, 16102, 16130, 16130, 16161, 16161, 16191, 16191, 16222, 16222, 16252, 16252, 16283, 16283, 16314, 16314, 16344, 16344, 16375, 16375, 16405, 16405, 16436, 16436, 16467, 16467, 16495, 16495, 16526, 16526, 16556, 16556, 16587, 16587, 16617, 16617, 16648, 16648, 16679, 16679, 16709, 16709, 16740, 16740, 16770, 16770, 16801, 16801, 16832, 16832, 16861, 16861, 16892, 16892, 16922, 16922, 16953, 16953, 16983, 16983, 17014, 17014, 17045, 17045, 17075, 17075, 17106, 17106, 17136, 17136, 17167, 17167, 17198, 17198, 17226, 17226, 17257, 17257, 17287, 17287, 17318, 17318, 17348, 17348, 17379, 17379, 17410, 17410, 17440, 17440, 17471, 17471, 17501, 17501, 17532, 17532, 17563, 17563, 17591, 17591, 17622, 17622, 17652, 17652, 17683, 17683, 17713, 17713, 17744, 17744, 17775, 17775, 17805, 17805, 17836, 17836, 17866, 17866, 17897, 17897, 17928, 17928, 17956, 17956, 17987, 17987, 18017, 18017, 18048, 18048, 18078, 18078, 18109, 18109, 18140, 18140, 18170, 18170, 18201, 18201, 18231, 18231, 18262, 18262, 18293, 18293, 18322, 18322, 18353, 18353, 18383, 18383, 18414, 18414, 18444, 18444, 18475, 18475, 18506, 18506, 18536, 18536, 18567, 18567, 18597, 18597, 18628, 18628, 18659, 18659, 18687, 18687, 18718, 18718, 18748, 18748, 18779, 18779, 18809, 18809, 18840, 18840, 18871, 18871, 18901, 18901, 18932, 18932, 18962, 18962), class = "Date"), NOME_REGIONE = c("Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte" ), CLASSE = c("over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80"), decessi = c(5635, 3755, 5087, 3391, 5396, 3541, 5018, 3442, 4707, 3216, 4464, 3153, 4804, 3238, 4789, 3123, 4358, 2991, 4930, 3276, 5096, 3352, 5384, 3660, 5922, 3842, 5487, 3429, 5585, 3609, 5266, 3382, 4969, 3124, 4875, 2993, 5022, 3178, 4878, 3093, 4628, 2962, 5065, 3262, 5499, 3331, 5602, 3603, 5939, 3729, 5265, 3326, 5734, 3554, 5178, 3186, 4819, 3084, 4718, 2887, 4992, 2998, 4838, 3094, 4636, 2882, 5255, 3290, 5304, 3248, 5682, 3434, 5892, 3586, 5320, 3172, 5698, 3408, 5111, 3223, 4878, 2996, 4737, 2900, 5029, 2966, 4897, 2879, 4718, 2825, 5101, 3135, 5437, 3264, 5632, 3410, 6654, 3841, 5914, 3267, 6353, 3589, 5769, 3326, 5468, 3156, 5279, 2922, 5577, 3168, 5476, 3089, 5168, 2918, 5744, 3236, 5977, 3441, 6289, 3493, 6384, 3570, 5509, 3099, 5890, 3412, 5622, 3066, 5185, 2997, 5002, 2831, 5278, 3053, 5155, 2934, 4881, 2737, 5400, 3124, 5721, 3092, 6245, 3481, 6877, 3745, 5968, 3284, 6312, 3545, 5871, 3118, 5423, 3051, 5450, 2953, 5619, 3046, 5585, 3002, 5174, 2790, 5846, 3203, 5987, 3254, 6438, 3483, 6765, 3623, 5874, 3176, 6434, 3488, 6017, 3126, 5507, 3019, 5398, 2871, 5709, 2977, 5599, 2879, 5199, 2880, 5804, 2986, 6028, 3137, 6406, 3362, 6937, 3442, 5953, 2946, 6454, 3175, 5962, 3036, 5576, 2901, 5439, 2752, 5701, 2881, 5549, 2820, 5299, 2677, 5765, 3050, 5929, 3156, 6632, 3382, 8694, 4100, 7472, 3585, 8246, 3792, 7368, 3605, 7005, 3340, 6827, 3299, 7108, 3420, 7115, 3439, 6680, 3150, 7265, 3494, 7695, 3531, 8055, 3823, 7282, 3711, 6414, 3333, 6686, 3530, 6170, 3237, 5791, 3080, 5641, 3031, 5958, 2995, 5910, 2979, 5473, 2850, 6186, 3298, 6421, 3198, 6630, 3562), deccov = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 702, 579, 1435, 710, 418, 156, 215, 64, 50, 10, 7, 4, 13, 5, 233, 85, 1283, 707, 920, 456, 560, 230, 416, 162, 511, 368, 313, 323, 87, 111, 11, 23, 2, 1, 11, 7, 26, 20, 31, 14, 43, 25, 37, 31), diff = c(5635, 3755, 5087, 3391, 5396, 3541, 5018, 3442, 4707, 3216, 4464, 3153, 4804, 3238, 4789, 3123, 4358, 2991, 4930, 3276, 5096, 3352, 5384, 3660, 5922, 3842, 5487, 3429, 5585, 3609, 5266, 3382, 4969, 3124, 4875, 2993, 5022, 3178, 4878, 3093, 4628, 2962, 5065, 3262, 5499, 3331, 5602, 3603, 5939, 3729, 5265, 3326, 5734, 3554, 5178, 3186, 4819, 3084, 4718, 2887, 4992, 2998, 4838, 3094, 4636, 2882, 5255, 3290, 5304, 3248, 5682, 3434, 5892, 3586, 5320, 3172, 5698, 3408, 5111, 3223, 4878, 2996, 4737, 2900, 5029, 2966, 4897, 2879, 4718, 2825, 5101, 3135, 5437, 3264, 5632, 3410, 6654, 3841, 5914, 3267, 6353, 3589, 5769, 3326, 5468, 3156, 5279, 2922, 5577, 3168, 5476, 3089, 5168, 2918, 5744, 3236, 5977, 3441, 6289, 3493, 6384, 3570, 5509, 3099, 5890, 3412, 5622, 3066, 5185, 2997, 5002, 2831, 5278, 3053, 5155, 2934, 4881, 2737, 5400, 3124, 5721, 3092, 6245, 3481, 6877, 3745, 5968, 3284, 6312, 3545, 5871, 3118, 5423, 3051, 5450, 2953, 5619, 3046, 5585, 3002, 5174, 2790, 5846, 3203, 5987, 3254, 6438, 3483, 6765, 3623, 5874, 3176, 6434, 3488, 6017, 3126, 5507, 3019, 5398, 2871, 5709, 2977, 5599, 2879, 5199, 2880, 5804, 2986, 6028, 3137, 6406, 3362, 6937, 3442, 5953, 2946, 6454, 3175, 5962, 3036, 5576, 2901, 5439, 2752, 5701, 2881, 5549, 2820, 5299, 2677, 5765, 3050, 5929, 3156, 6632, 3382, 8694, 4100, 7470, 3584, 7544, 3213, 5933, 2895, 6587, 3184, 6612, 3235, 7058, 3410, 7108, 3435, 6667, 3145, 7032, 3409, 6412, 2824, 7135, 3367, 6722, 3481, 5998, 3171, 6175, 3162, 5857, 2914, 5704, 2969, 5630, 3008, 5956, 2994, 5899, 2972, 5447, 2830, 6155, 3284, 6378, 3173, 6593, 3531)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -264L), groups = structure(list( ma = structure(c(14975, 15006, 15034, 15065, 15095, 15126, 15156, 15187, 15218, 15248, 15279, 15309, 15340, 15371, 15400, 15431, 15461, 15492, 15522, 15553, 15584, 15614, 15645, 15675, 15706, 15737, 15765, 15796, 15826, 15857, 15887, 15918, 15949, 15979, 16010, 16040, 16071, 16102, 16130, 16161, 16191, 16222, 16252, 16283, 16314, 16344, 16375, 16405, 16436, 16467, 16495, 16526, 16556, 16587, 16617, 16648, 16679, 16709, 16740, 16770, 16801, 16832, 16861, 16892, 16922, 16953, 16983, 17014, 17045, 17075, 17106, 17136, 17167, 17198, 17226, 17257, 17287, 17318, 17348, 17379, 17410, 17440, 17471, 17501, 17532, 17563, 17591, 17622, 17652, 17683, 17713, 17744, 17775, 17805, 17836, 17866, 17897, 17928, 17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414, 18444, 18475, 18506, 18536, 18567, 18597, 18628, 18659, 18687, 18718, 18748, 18779, 18809, 18840, 18871, 18901, 18932, 18962 ), class = "Date"), NOME_REGIONE = c("Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte" ), .rows = structure(list(1:2, 3:4, 5:6, 7:8, 9:10, 11:12, 13:14, 15:16, 17:18, 19:20, 21:22, 23:24, 25:26, 27:28, 29:30, 31:32, 33:34, 35:36, 37:38, 39:40, 41:42, 43:44, 45:46, 47:48, 49:50, 51:52, 53:54, 55:56, 57:58, 59:60, 61:62, 63:64, 65:66, 67:68, 69:70, 71:72, 73:74, 75:76, 77:78, 79:80, 81:82, 83:84, 85:86, 87:88, 89:90, 91:92, 93:94, 95:96, 97:98, 99:100, 101:102, 103:104, 105:106, 107:108, 109:110, 111:112, 113:114, 115:116, 117:118, 119:120, 121:122, 123:124, 125:126, 127:128, 129:130, 131:132, 133:134, 135:136, 137:138, 139:140, 141:142, 143:144, 145:146, 147:148, 149:150, 151:152, 153:154, 155:156, 157:158, 159:160, 161:162, 163:164, 165:166, 167:168, 169:170, 171:172, 173:174, 175:176, 177:178, 179:180, 181:182, 183:184, 185:186, 187:188, 189:190, 191:192, 193:194, 195:196, 197:198, 199:200, 201:202, 203:204, 205:206, 207:208, 209:210, 211:212, 213:214, 215:216, 217:218, 219:220, 221:222, 223:224, 225:226, 227:228, 229:230, 231:232, 233:234, 235:236, 237:238, 239:240, 241:242, 243:244, 245:246, 247:248, 249:250, 251:252, 253:254, 255:256, 257:258, 259:260, 261:262, 263:264), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame" ), row.names = c(NA, -132L), .drop = TRUE))
and used this code to plot them
ggplot(data=diffdec,aes(x=ma,y=decessi,col=CLASSE))+
geom_line(size=1,lty=2)+
geom_line(data=diffdec,aes(x=ma,y=diff,col=CLASSE),size=2)+
geom_line(aes(x=ma,y=deccov, col=CLASSE))+
xlim(c(my("0111"),my("1221")))+
theme(legend.position="bottom",
axis.text = element_text(size = 15),
axis.title=element_text(size = 20),
strip.text = element_text(size = 16),
plot.caption = element_text(size = 20, face = "italic"),
legend.title = element_text(size = 0),
legend.text = element_text(size = 15))+
ylab("Decessi")+
xlab("Data")
I'd like to show both the dashed lines in a different colour. Is there any way using the above code, or I need to use a different approach?

OP clarified that they were looking to change the dotted lines to have the same color, but be different than their respective groups. In other words, the dashed line for "over80" and for "under80" should both be a different color. OP did not indicate if the dashed lines should be included in the legend as an entry, but I am assuming that the answer is "yes".
Here's a descriptive example, and OP can extend to their own question, since we don't have any data to work from.
library(ggplot2)
df <- iris[which(iris$Species!="versicolor"),]
ggplot(df, aes(Sepal.Length, y=Sepal.Width, col=Species)) +
geom_line() +
geom_line(aes(y=Petal.Width), lty=2)
Since color= is specified globally for the plot via ggplot(aes(...)), every geom will by default have the color aesthetic mapped as if col=Species. To override that default, you can refer to the aesthetic inside a particular geom. In this case, we can color the lines by changing the call to the geom_line(). When col is specified there, it overwrites the original mapping.
ggplot(df, aes(Sepal.Length, y=Sepal.Width, col=Species)) +
geom_line() +
geom_line(aes(y=Petal.Width), col="blue", lty=2) +
theme_classic()
To get the line color to appear in the legend, you will need to add the col= aesthetic back inside aes(), except this time you will want to map to a simple character value (the name of our line). Doing this effectively "maps" all observations to a new variable where every value is the same. This means all observations are colored the same (what we want), and a legend entry is created.
ggplot(df, aes(Sepal.Length, y=Sepal.Width, col=Species)) +
geom_line() +
geom_line(aes(y=Petal.Width, col="Petals"), lty=2) +
labs(y="Width") + theme_classic()
Now the line is correct and legend entry is correct, but we didn't specify a color and now the legend entry for "Petals" is a solid line, whereas the line is a dashed line. One of the best options to fix this is likely to map the linetype aesthetic in the same manner as col and then use scale_*_manual() functions to explicitly define the colors and linetypes:
ggplot(df, aes(Sepal.Length, y=Sepal.Width, col=Species, lty=Species)) +
geom_line() +
geom_line(aes(y=Petal.Width, col="Petals", lty="Petals")) +
scale_color_manual(
values = c("setosa" = "red", "virginica" = "blue", "Petals" = "green3")) +
scale_linetype_manual(
values = c("setosa" = 1, "virginica" = 1, "Petals" = 2)) +
labs(y="Width") + theme_classic()

Related

Sorting date format column as month.abb order in R

I have a column which is in date format. However the months are not arrange in month.abb normal sequence (e.g, Jan - Dec).
I tried it but could not find a way that work for me.
Any way I can do it please?
Refer the code below
> # create layernames and date tibble
> names_dates <- tibble(names = names(idw2),
+ date = time(idw2)) %>%
+ select(date)
> head(names_dates, 20)
# A tibble: 20 x 1
date
<date>
1 1998-04-01
2 1999-04-01
3 2000-04-01
4 2001-04-01
5 2002-04-01
6 2003-04-01
7 2004-04-01
8 2005-04-01
9 2006-04-01
10 2007-04-01
11 2008-04-01
12 2009-04-01
13 2010-04-01
14 2011-04-01
15 2012-04-01
16 2013-04-01
17 2014-04-01
18 2015-04-01
19 2016-04-01
20 2017-04-01
dates_names_ordered <-
here is the dput of my data
> dput(names_dates)
structure(list(date = structure(c(10317, 10682, 11048, 11413,
11778, 12143, 12509, 12874, 13239, 13604, 13970, 14335, 14700,
15065, 15431, 15796, 16161, 16526, 16892, 17257, 10439, 10804,
11170, 11535, 11900, 12265, 12631, 12996, 13361, 13726, 14092,
14457, 14822, 15187, 15553, 15918, 16283, 16648, 17014, 17379,
10561, 10926, 11292, 11657, 12022, 12387, 12753, 13118, 13483,
13848, 14214, 14579, 14944, 15309, 15675, 16040, 16405, 16770,
17136, 17501, 10258, 10623, 10988, 11354, 11719, 12084, 12449,
12815, 13180, 13545, 13910, 14276, 14641, 15006, 15371, 15737,
16102, 16467, 16832, 17198, 10227, 10592, 10957, 11323, 11688,
12053, 12418, 12784, 13149, 13514, 13879, 14245, 14610, 14975,
15340, 15706, 16071, 16436, 16801, 17167, 10408, 10773, 11139,
11504, 11869, 12234, 12600, 12965, 13330, 13695, 14061, 14426,
14791, 15156, 15522, 15887, 16252, 16617, 16983, 17348, 10378,
10743, 11109, 11474, 11839, 12204, 12570, 12935, 13300, 13665,
14031, 14396, 14761, 15126, 15492, 15857, 16222, 16587, 16953,
17318, 10286, 10651, 11017, 11382, 11747, 12112, 12478, 12843,
13208, 13573, 13939, 14304, 14669, 15034, 15400, 15765, 16130,
16495, 16861, 17226, 10347, 10712, 11078, 11443, 11808, 12173,
12539, 12904, 13269, 13634, 14000, 14365, 14730, 15095, 15461,
15826, 16191, 16556, 16922, 17287, 10531, 10896, 11262, 11627,
11992, 12357, 12723, 13088, 13453, 13818, 14184, 14549, 14914,
15279, 15645, 16010, 16375, 16740, 17106, 17471, 10500, 10865,
11231, 11596, 11961, 12326, 12692, 13057, 13422, 13787, 14153,
14518, 14883, 15248, 15614, 15979, 16344, 16709, 17075, 17440,
10470, 10835, 11201, 11566, 11931, 12296, 12662, 13027, 13392,
13757, 14123, 14488, 14853, 15218, 15584, 15949, 16314, 16679,
17045, 17410), class = "Date")), row.names = c(NA, -240L), class = c("tbl_df",
"tbl", "data.frame"))
A possible solution, using lubridate:
library(dplyr)
library(lubridate)
df %>%
arrange(ymd(date) %>% year, ymd(date) %>% month)
#> # A tibble: 240 x 1
#> date
#> <date>
#> 1 1998-01-01
#> 2 1998-02-01
#> 3 1998-03-01
#> 4 1998-04-01
#> 5 1998-05-01
#> 6 1998-06-01
#> 7 1998-07-01
#> 8 1998-08-01
#> 9 1998-09-01
#> 10 1998-10-01
#> # ... with 230 more rows

Looping to create a new column of counts from specific rows

I'd like to make a new column here in my dataset. I want to pick counts out from the full_name column for the species Stegastes planifrons and make a new column with counts (including transects where the count is 0). I used this code:
Fish1$Stegastes_planifrons <- rep(0, nrow(Fish1))
for (i in 1:nrow(Fish1)) {
if (Fish1$Full_name[i] == "Stegastes planifrons") {
Fish1$Stegastes_planifrons[i] == Fish1$count[i]
}
}
EDIT: Data in dput form:
structure(list(Year = c(2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019), Location = c("Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela"), Site = c("AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD"), Depth = c(10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10), Transect = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), Family = c("Pomacentridae",
"Haemulidae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Serranidae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Haemulidae", "Pomacentridae", "Pomacentridae",
"Acanthuridae", "Haemulidae", "Haemulidae", "Haemulidae", "Scaridae",
"Carangidae", "Scaridae", "Labridae", "Scaridae", "Scaridae",
"Haemulidae", "Scaridae", "Scaridae", "Scaridae", "Scaridae",
"Scaridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Scaridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Scaridae", "Pomacentridae", "Pomacentridae",
"Haemulidae", "Pomacentridae", "Pomacentridae", "Haemulidae",
"Lutjanidae", "Scaridae", "Pomacentridae", "Pomacentridae", "Pomacanthidae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Labridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Labridae", "Chaetodontidae", "Haemulidae", "Haemulidae", "Pomacentridae",
"Labridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Labridae", "Pomacentridae", "Pomacentridae", "Haemulidae", "Haemulidae",
"Pomacentridae"), Genus = c("Stegastes", "Anisotremus", "Stegastes",
"Stegastes", "Stegastes", "Stegastes", "Stegastes", "Stegastes",
"Stegastes", "Stegastes", "Stegastes", "Stegastes", "Stegastes",
"Stegastes", "Serranus", "Stegastes", "Stegastes", "Stegastes",
"Stegastes", "Microspathodon", "Abudefduf", "Microspathodon",
"Haemulon", "Microspathodon", "Abudefduf", "Acanthurus", "Haemulon",
"Anisotremus", "Anisotremus", "Sparisoma", "Caranx", "Scarus",
"Halichoeres", "Scarus", "Sparisoma", "Anisotremus", "Sparisoma",
"Scarus", "Scarus", "Scarus", "Scarus", "Stegastes", "Stegastes",
"Stegastes", "Stegastes", "Scarus", "Microspathodon", "Stegastes",
"Stegastes", "Microspathodon", "Microspathodon", "Stegastes",
"Stegastes", "Scarus", "Stegastes", "Stegastes", "Haemulon",
"Stegastes", "Stegastes", "Anisotremus", "Lutjanus", "Scarus",
"Stegastes", "Stegastes", "Holacanthus", "Stegastes", "Stegastes",
"Stegastes", "Stegastes", "Stegastes", "Halichoeres", "Stegastes",
"Stegastes", "Microspathodon", "Stegastes", "Stegastes", "Stegastes",
"Stegastes", "Stegastes", "Stegastes", "Stegastes", "Stegastes",
"Stegastes", "Stegastes", "Stegastes", "Thalassoma", "Chaetodon",
"Haemulon", "Haemulon", "Stegastes", "Thalassoma", "Stegastes",
"Stegastes", "Stegastes", "Halichoeres", "Stegastes", "Chromis",
"Haemulon", "Haemulon", "Stegastes"), Species = c("planifrons",
"virginicus", "adustus", "partitus", "partitus", "adustus", "partitus",
"adustus", "adustus", "adustus", "variabilis", "partitus", "adustus",
"adustus", "tortugarum", "adustus", "partitus", "adustus", "partitus",
"chrysurus", "saxatilis", "chrysurus", "carbonarium", "chrysurus",
"saxatilis", "coeruleus", "chrysargyreum", "virginicus", "virginicus",
"viride", "ruber", "vetula", "bivittatus", "iseri", "viride",
"virginicus", "viride", "iseri", "iseri", "iseri", "vetula",
"partitus", "adustus", "adustus", "adustus", "iseri", "chrysurus",
"adustus", "adustus", "chrysurus", "chrysurus", "adustus", "adustus",
"iseri", "diencaeus", "adustus", "sciurus", "adustus", "adustus",
"virginicus", "mahogoni", "vetula", "partitus", "adustus", "tricolor",
"partitus", "adustus", "adustus", "partitus", "partitus", "maculipinna",
"partitus", "variabilis", "chrysurus", "adustus", "variabilis",
"adustus", "partitus", "adustus", "adustus", "partitus", "adustus",
"adustus", "diencaeus", "partitus", "bifasciatum", "capistratus",
"carbonarium", "carbonarium", "adustus", "bifasciatum", "adustus",
"partitus", "diencaeus", "bivittatus", "adustus", "insolata",
"sciurus", "carbonarium", "adustus"), Full_name = c("Stegastes planifrons",
"Anisotremus virginicus", "Stegastes adustus", "Stegastes partitus",
"Stegastes partitus", "Stegastes adustus", "Stegastes partitus",
"Stegastes adustus", "Stegastes adustus", "Stegastes adustus",
"Stegastes variabilis", "Stegastes partitus", "Stegastes adustus",
"Stegastes adustus", "Serranus tortugarum", "Stegastes adustus",
"Stegastes partitus", "Stegastes adustus", "Stegastes partitus",
"Microspathodon chrysurus", "Abudefduf saxatilis", "Microspathodon chrysurus",
"Haemulon carbonarium", "Microspathodon chrysurus", "Abudefduf saxatilis",
"Acanthurus coeruleus", "Haemulon chrysargyreum", "Anisotremus virginicus",
"Anisotremus virginicus", "Sparisoma viride", "Caranx ruber",
"Scarus vetula", "Halichoeres bivittatus", "Scarus iseri", "Sparisoma viride",
"Anisotremus virginicus", "Sparisoma viride", "Scarus iseri",
"Scarus iseri", "Scarus iseri", "Scarus vetula", "Stegastes partitus",
"Stegastes adustus", "Stegastes adustus", "Stegastes adustus",
"Scarus iseri", "Microspathodon chrysurus", "Stegastes adustus",
"Stegastes adustus", "Microspathodon chrysurus", "Microspathodon chrysurus",
"Stegastes adustus", "Stegastes adustus", "Scarus iseri", "Stegastes diencaeus",
"Stegastes adustus", "Haemulon sciurus", "Stegastes adustus",
"Stegastes adustus", "Anisotremus virginicus", "Lutjanus mahogoni",
"Scarus vetula", "Stegastes partitus", "Stegastes adustus", "Holacanthus tricolor",
"Stegastes partitus", "Stegastes adustus", "Stegastes adustus",
"Stegastes partitus", "Stegastes partitus", "Halichoeres maculipinna",
"Stegastes partitus", "Stegastes variabilis", "Microspathodon chrysurus",
"Stegastes adustus", "Stegastes variabilis", "Stegastes adustus",
"Stegastes partitus", "Stegastes adustus", "Stegastes adustus",
"Stegastes partitus", "Stegastes adustus", "Stegastes adustus",
"Stegastes diencaeus", "Stegastes partitus", "Thalassoma bifasciatum",
"Chaetodon capistratus", "Haemulon carbonarium", "Haemulon carbonarium",
"Stegastes adustus", "Thalassoma bifasciatum", "Stegastes adustus",
"Stegastes partitus", "Stegastes diencaeus", "Halichoeres bivittatus",
"Stegastes adustus", "Chromis insolata", "Haemulon sciurus",
"Haemulon carbonarium", "Stegastes adustus"), Guild = c("Omnivore",
"Invertivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Omnivore",
"Herbivore", "Herbivore", "Herbivore", "Planktivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Invertivore",
"Herbivore", "Invertivore", "Herbivore", "Invertivore", "Herbivore",
"Invertivore", "Invertivore", "Invertivore", "Herbivore", "Carnivore",
"Herbivore", "Invertivore", "Herbivore", "Herbivore", "Invertivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Invertivore", "Herbivore", "Herbivore", "Invertivore", "Carnivore",
"Herbivore", "Herbivore", "Herbivore", "Invertivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Invertivore",
"Herbivore", "Omnivore", "Herbivore", "Herbivore", "Omnivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Invertivore",
"Invertivore", "Invertivore", "Invertivore", "Herbivore", "Invertivore",
"Herbivore", "Herbivore", "Herbivore", "Invertivore", "Herbivore",
"Planktivore", "Invertivore", "Invertivore", "Herbivore"), count = c(13,
4, 41, 10, 10, 41, 10, 41, 41, 41, 2, 10, 41, 41, 2, 41, 10,
41, 10, 4, 3, 4, 1, 4, 3, 1, 1, 4, 4, 3, 1, 1, 1, 2, 3, 4, 3,
6, 6, 6, 2, 2, 40, 40, 40, 6, 6, 40, 40, 6, 6, 40, 40, 6, 1,
40, 1, 40, 40, 1, 1, 2, 14, 52, 1, 14, 52, 52, 14, 14, 1, 14,
5, 4, 52, 5, 52, 14, 52, 52, 14, 52, 52, 2, 14, 2, 2, 3, 3, 52,
2, 52, 14, 2, 1, 52, 1, 2, 3, 52), `Transect ID` = c("2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10"
), Stegastes_planifrons = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0)), row.names = c(1L, 2L, 3L, 4L, 5L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L,
52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L,
65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L,
78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 89L, 90L, 91L,
92L, 93L, 94L, 95L, 96L, 97L, 98L, 104L, 105L, 106L, 107L, 108L,
109L, 110L), class = "data.frame")
Without your data (I can't extract your data from the image you posted), I can't test/troubleshoot this. The best way to share data is to dput(Fish1) and copy/paste the value returned in the console as an edit to your original question. If that is too cumbersome because the dataset is large, you could share a subset of your data or an example dummy dataset. This will get more eyes on your question and will lead to a higher quality answer that's more likely to solve your problem.
Anyways, here is a demonstration of how to summarize counts by group using dplyr. If you still have issues after working through this example, please let me know. I'll help the best I can. Thanks!
data(iris)
library(dplyr)
# Dummy transect and count variable for demo
iris$transect <- sample.int(n = 3, size = nrow(iris), replace = TRUE)
iris$count <- iris$Sepal.Length
virginica _counts <- iris %>%
filter(Species == "virginica") %>%
group_by(transect) %>%
summarize(counts = sum(count))
virginica_counts
#> # A tibble: 3 x 2
#> transect counts
#> <int> <dbl>
#> 1 1 112.
#> 2 2 130.
#> 3 3 87.9
Created on 2022-04-05 by the reprex package (v2.0.1)
EDIT
Using your data:
Fish1$Transect_ID <- Fish1$`Transect ID`
Fish1 %>%
group_by(Transect_ID, .drop = FALSE) %>%
filter(Full_name == "Stegastes planifrons") %>%
summarize(counts = sum(count))
Transect_ID counts
<chr> <dbl>
1 2019_Tela_AD_1_10 13
2 2019_Tela_AD_2_10 0
3 2019_Tela_AD_3_10 0
EDIT 2
Based on the conversation in the comments, I think this is what you have described:
Fish1_Splaniforms <- Fish1 %>%
group_by(Transect_ID, .drop = FALSE) %>%
mutate(S.planiforms_counts = sum(count[Full_name == "Stegastes planifrons"]))
However, if the ultimate plan is for each species to get its own column that indicates counts (of that species) and rows that indicate transects, you could convert from long to wide:
library(reshape2)
Fish1$Transect_ID <- Fish1$`Transect ID`
Fish1_wide <- dcast(Fish1, Transect_ID ~ Full_name, value.var="count", fun.aggregate = sum)
head(Fish1_wide)
#> Transect_ID Abudefduf saxatilis Acanthurus coeruleus
#> 1 2019_Tela_AD_1_10 6 1
#> 2 2019_Tela_AD_2_10 0 0
#> 3 2019_Tela_AD_3_10 0 0
#> Anisotremus virginicus Caranx ruber Chaetodon capistratus Chromis insolata
#> 1 16 1 0 0
#> 2 1 0 0 0
#> 3 0 0 2 1
#> Haemulon carbonarium Haemulon chrysargyreum Haemulon sciurus
#> 1 1 1 0
#> 2 0 0 1
#> 3 9 0 2
#> Halichoeres bivittatus Halichoeres maculipinna Holacanthus tricolor
#> 1 1 0 0
#> 2 0 0 0
#> 3 1 1 1
#> Lutjanus mahogoni Microspathodon chrysurus Scarus iseri Scarus vetula
#> 1 0 12 2 1
#> 2 1 18 30 4
#> 3 0 4 0 0
#> Serranus tortugarum Sparisoma viride Stegastes adustus Stegastes diencaeus
#> 1 2 9 369 0
#> 2 0 0 400 1
#> 3 0 0 676 4
#> Stegastes partitus Stegastes planifrons Stegastes variabilis
#> 1 60 13 2
#> 2 2 0 0
#> 3 126 0 10
#> Thalassoma bifasciatum
#> 1 0
#> 2 0
#> 3 4

From half-hour to hourly data

My data set contains observations taken every 30 minutes. Basically, I would like to have such as 01:00:00 as the new time; 02/01/2019 as an exampe of date and the sum of the measurement values between 00:00:00-00:30:00 and 00:30:00-01:00:00 As an output
time variable value
01:00:00 02/01/2019 234.3 (example)
How can I aggregate my data to 1 hour?
Sample data: Selected only the first 300
structure(list(time = structure(c(1800, 3600, 5400, 7200, 9000,
10800, 12600, 14400, 16200, 18000, 19800, 21600, 23400, 25200,
27000, 28800, 30600, 32400, 34200, 36000, 37800, 39600, 41400,
43200, 45000, 46800, 48600, 50400, 52200, 54000, 55800, 57600,
59400, 61200, 63000, 64800, 66600, 68400, 70200, 72000, 73800,
75600, 77400, 79200, 81000, 82800, 84600, 86400, 1800, 3600,
5400, 7200, 9000, 10800, 12600, 14400, 16200, 18000, 19800, 21600,
23400, 25200, 27000, 28800, 30600, 32400, 34200, 36000, 37800,
39600, 41400, 43200, 45000, 46800, 48600, 50400, 52200, 54000,
55800, 57600, 59400, 61200, 63000, 64800, 66600, 68400, 70200,
72000, 73800, 75600, 77400, 79200, 81000, 82800, 84600, 86400,
1800, 3600, 5400, 7200, 9000, 10800, 12600, 14400, 16200, 18000,
19800, 21600, 23400, 25200, 27000, 28800, 30600, 32400, 34200,
36000, 37800, 39600, 41400, 43200, 45000, 46800, 48600, 50400,
52200, 54000, 55800, 57600, 59400, 61200, 63000, 64800, 66600,
68400, 70200, 72000, 73800, 75600, 77400, 79200, 81000, 82800,
84600, 86400, 1800, 3600, 5400, 7200, 9000, 10800, 12600, 14400,
16200, 18000, 19800, 21600, 23400, 25200, 27000, 28800, 30600,
32400, 34200, 36000, 37800, 39600, 41400, 43200, 45000, 46800,
48600, 50400, 52200, 54000, 55800, 57600, 59400, 61200, 63000,
64800, 66600, 68400, 70200, 72000, 73800, 75600, 77400, 79200,
81000, 82800, 84600, 86400, 1800, 3600, 5400, 7200, 9000, 10800,
12600, 14400, 16200, 18000, 19800, 21600, 23400, 25200, 27000,
28800, 30600, 32400, 34200, 36000, 37800, 39600, 41400, 43200,
45000, 46800, 48600, 50400, 52200, 54000, 55800, 57600, 59400,
61200, 63000, 64800, 66600, 68400, 70200, 72000, 73800, 75600,
77400, 79200, 81000, 82800, 84600, 86400, 1800, 3600, 5400, 7200,
9000, 10800, 12600, 14400, 16200, 18000, 19800, 21600, 23400,
25200, 27000, 28800, 30600, 32400, 34200, 36000, 37800, 39600,
41400, 43200, 45000, 46800, 48600, 50400, 52200, 54000, 55800,
57600, 59400, 61200, 63000, 64800, 66600, 68400, 70200, 72000,
73800, 75600, 77400, 79200, 81000, 82800, 84600, 86400, 1800,
3600, 5400, 7200, 9000, 10800, 12600, 14400, 16200, 18000, 19800,
21600), class = c("hms", "difftime"), units = "secs"), variable = c("02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "08/01/2019", "08/01/2019", "08/01/2019",
"08/01/2019", "08/01/2019", "08/01/2019", "08/01/2019", "08/01/2019",
"08/01/2019", "08/01/2019", "08/01/2019", "08/01/2019"), value = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0)), row.names = c(NA, -300L), class = c("tbl_df",
"tbl", "data.frame"))
I've got an answer, that uses {lubridate}:
library(lubridate)
a_df %>%
mutate(datetime = lubridate::dmy_hms(str_c(variable, time))) %>%
mutate(duration_elapsed = lubridate::interval(datetime[1], datetime),
duration_elapsed = as.duration(duration_elapsed)) %>%
mutate(hourly_group = duration_elapsed %/% dhours()) %>%
glimpse() %>%
group_by(hourly_group) %>%
summarise(
value = sum(value),
time = last(time),
variable = last(variable),
datetime = last(datetime)
) %>%
# select(-hourly_group, -time, -variable) %>%
select(-hourly_group) %>%
print(width = Inf, n = 50)
Note that a_df is the data-frame you've provided.
This should work, and it should be robust if you have missing numbers,
etc.

ggplot issues (geom_line() shows no line, large numbers not displayed correctly on Y axis, sorting months by name on a plot X axis)

I need to make a few ggplots for my project but I have a lot of issues making them work. I don't know why is geom_line() not making a line, how do I fix the numbers displayed on the left (I'm working with large numbers) and is there any way to sort months without formatting them to numbers. Thanks.
GGPlot
Data I'm working with
day_wise %>%
select(Date,Confirmed,Recovered,Deaths) %>% mutate(AsDate=as.Date(Date,format="%Y-%m-%d")) %>% mutate(Month=format(AsDate,format="%B")) %>% group_by(Month) %>% summarise("Infected"=sum(Confirmed),Recovered=sum(Recovered),Deaths=sum(Deaths)) %>% arrange(Recovered)
day_wise %>%
select(Date,Confirmed,Recovered,Deaths) %>% mutate(AsDate=as.Date(Date,format="%Y-%m-%d")) %>% mutate(Month=format(AsDate,format="%B")) %>% group_by(Month) %>% summarise("Infected"=sum(Confirmed),Recovered=sum(Recovered),Deaths=sum(Deaths)) %>% arrange(Recovered)
DPUT
structure(list(Date = c("2020-01-22", "2020-01-23", "2020-01-24",
"2020-01-25", "2020-01-26", "2020-01-27", "2020-01-28", "2020-01-29",
"2020-01-30", "2020-01-31", "2020-02-01", "2020-02-02", "2020-02-03",
"2020-02-04", "2020-02-05", "2020-02-06", "2020-02-07", "2020-02-08",
"2020-02-09", "2020-02-10", "2020-02-11", "2020-02-12", "2020-02-13",
"2020-02-14", "2020-02-15", "2020-02-16", "2020-02-17", "2020-02-18",
"2020-02-19", "2020-02-20", "2020-02-21", "2020-02-22", "2020-02-23",
"2020-02-24", "2020-02-25", "2020-02-26", "2020-02-27", "2020-02-28",
"2020-02-29", "2020-03-01", "2020-03-02", "2020-03-03", "2020-03-04",
"2020-03-05", "2020-03-06", "2020-03-07", "2020-03-08", "2020-03-09",
"2020-03-10", "2020-03-11", "2020-03-12", "2020-03-13", "2020-03-14",
"2020-03-15", "2020-03-16", "2020-03-17", "2020-03-18", "2020-03-19",
"2020-03-20", "2020-03-21", "2020-03-22", "2020-03-23", "2020-03-24",
"2020-03-25", "2020-03-26", "2020-03-27", "2020-03-28", "2020-03-29",
"2020-03-30", "2020-03-31", "2020-04-01", "2020-04-02", "2020-04-03",
"2020-04-04", "2020-04-05", "2020-04-06", "2020-04-07", "2020-04-08",
"2020-04-09", "2020-04-10", "2020-04-11", "2020-04-12", "2020-04-13",
"2020-04-14", "2020-04-15", "2020-04-16", "2020-04-17", "2020-04-18",
"2020-04-19", "2020-04-20", "2020-04-21", "2020-04-22", "2020-04-23",
"2020-04-24", "2020-04-25", "2020-04-26", "2020-04-27", "2020-04-28",
"2020-04-29", "2020-04-30", "2020-05-01", "2020-05-02", "2020-05-03",
"2020-05-04", "2020-05-05", "2020-05-06", "2020-05-07", "2020-05-08",
"2020-05-09", "2020-05-10", "2020-05-11", "2020-05-12", "2020-05-13",
"2020-05-14", "2020-05-15", "2020-05-16", "2020-05-17", "2020-05-18",
"2020-05-19", "2020-05-20", "2020-05-21", "2020-05-22", "2020-05-23",
"2020-05-24", "2020-05-25", "2020-05-26", "2020-05-27", "2020-05-28",
"2020-05-29", "2020-05-30", "2020-05-31", "2020-06-01", "2020-06-02",
"2020-06-03", "2020-06-04", "2020-06-05", "2020-06-06", "2020-06-07",
"2020-06-08", "2020-06-09", "2020-06-10", "2020-06-11", "2020-06-12",
"2020-06-13", "2020-06-14", "2020-06-15", "2020-06-16", "2020-06-17",
"2020-06-18", "2020-06-19", "2020-06-20", "2020-06-21", "2020-06-22",
"2020-06-23", "2020-06-24", "2020-06-25", "2020-06-26", "2020-06-27",
"2020-06-28", "2020-06-29", "2020-06-30", "2020-07-01", "2020-07-02",
"2020-07-03", "2020-07-04", "2020-07-05", "2020-07-06", "2020-07-07",
"2020-07-08", "2020-07-09", "2020-07-10", "2020-07-11", "2020-07-12",
"2020-07-13", "2020-07-14", "2020-07-15", "2020-07-16", "2020-07-17",
"2020-07-18", "2020-07-19", "2020-07-20", "2020-07-21", "2020-07-22",
"2020-07-23", "2020-07-24", "2020-07-25", "2020-07-26", "2020-07-27"
), Confirmed = c(555L, 654L, 941L, 1434L, 2118L, 2927L, 5578L,
6166L, 8234L, 9927L, 12038L, 16787L, 19887L, 23898L, 27643L,
30802L, 34334L, 37068L, 40095L, 42633L, 44675L, 46561L, 60206L,
66690L, 68765L, 70879L, 72815L, 74609L, 75030L, 75577L, 76206L,
77967L, 78290L, 78854L, 79707L, 80670L, 82034L, 83411L, 85306L,
87690L, 89664L, 92241L, 94540L, 97331L, 101274L, 105312L, 109266L,
113166L, 118190L, 125853L, 131603L, 146008L, 157114L, 168260L,
182919L, 198757L, 218343L, 246261L, 275869L, 308175L, 341585L,
383750L, 424889L, 475706L, 538666L, 603066L, 670723L, 730300L,
794939L, 871355L, 947569L, 1028968L, 1112123L, 1192586L, 1264304L,
1336976L, 1413849L, 1497624L, 1584249L, 1671907L, 1748872L, 1845653L,
1915247L, 1985174L, 2066003L, 2162715L, 2250439L, 2324396L, 2404919L,
2478258L, 2553508L, 2630314L, 2719327L, 2806267L, 2891199L, 2964146L,
3032850L, 3108149L, 3185195L, 3268876L, 3355922L, 3437608L, 3515244L,
3591321L, 3671310L, 3761332L, 3850418L, 3941935L, 4027781L, 4104027L,
4180268L, 4263867L, 4348619L, 4445724L, 4542073L, 4637485L, 4715994L,
4804278L, 4900702L, 5003730L, 5110064L, 5216964L, 5322253L, 5417579L,
5504542L, 5597064L, 5699664L, 5818978L, 5940145L, 6077978L, 6185530L,
6280725L, 6401536L, 6520924L, 6647861L, 6778724L, 6914666L, 7026925L,
7129150L, 7253492L, 7387517L, 7525631L, 7654725L, 7790735L, 7924156L,
8043794L, 8185197L, 8327050L, 8466978L, 8647784L, 8805336L, 8933875L,
9071733L, 9237071L, 9408254L, 9586141L, 9777487L, 9955597L, 10117227L,
10275799L, 10449697L, 10667386L, 10875091L, 11078585L, 11272152L,
11454847L, 11622190L, 11833034L, 12044836L, 12273063L, 12505640L,
12721968L, 12914636L, 13107415L, 13328867L, 13559984L, 13812525L,
14054563L, 14292198L, 14506845L, 14713623L, 14947078L, 15227725L,
15510481L, 15791645L, 16047190L, 16251796L, 16480485L), Deaths = c(17L,
18L, 26L, 42L, 56L, 82L, 131L, 133L, 171L, 213L, 259L, 362L,
426L, 492L, 564L, 634L, 719L, 806L, 906L, 1013L, 1113L, 1118L,
1371L, 1523L, 1666L, 1770L, 1868L, 2008L, 2123L, 2246L, 2250L,
2457L, 2467L, 2627L, 2707L, 2767L, 2810L, 2867L, 2936L, 2990L,
3079L, 3154L, 3249L, 3342L, 3454L, 3553L, 3797L, 3981L, 4260L,
4604L, 4909L, 5406L, 5823L, 6464L, 7144L, 7948L, 8845L, 9951L,
11429L, 13134L, 14831L, 16748L, 19016L, 21793L, 24800L, 28318L,
31997L, 35470L, 39634L, 44478L, 50029L, 56334L, 62319L, 68160L,
73181L, 79013L, 86915L, 93650L, 101279L, 108551L, 114620L, 120351L,
126098L, 132996L, 141308L, 148591L, 157481L, 163952L, 168522L,
173965L, 181122L, 187877L, 194727L, 201401L, 206979L, 210862L,
215511L, 221974L, 228742L, 234704L, 239881L, 245206L, 248659L,
252787L, 258658L, 265327L, 270736L, 276304L, 280569L, 284135L,
287608L, 293155L, 298383L, 303651L, 308866L, 313037L, 316366L,
319657L, 324441L, 329326L, 334112L, 339396L, 343385L, 346525L,
347703L, 351906L, 357119L, 361820L, 366562L, 370718L, 373606L,
376674L, 381497L, 387069L, 392218L, 396994L, 400875L, 403617L,
407314L, 412236L, 417441L, 422215L, 426512L, 430750L, 434124L,
437549L, 444416L, 449671L, 454700L, 460973L, 465222L, 469185L,
472756L, 478067L, 483328L, 489955L, 494782L, 499268L, 502357L,
506078L, 511210L, 516221L, 521341L, 526336L, 530705L, 534150L,
537947L, 544054L, 549373L, 554831L, 560142L, 565039L, 568993L,
572808L, 578468L, 583961L, 589760L, 596503L, 602130L, 606159L,
610319L, 616557L, 623540L, 633506L, 639650L, 644517L, 648621L,
654036L), Recovered = c(28L, 30L, 36L, 39L, 52L, 61L, 107L, 125L,
141L, 219L, 281L, 459L, 604L, 821L, 1071L, 1418L, 1903L, 2470L,
3057L, 3714L, 4417L, 4849L, 5930L, 7613L, 8902L, 10319L, 11951L,
13693L, 15394L, 17369L, 17966L, 21849L, 22304L, 24047L, 26652L,
29077L, 31919L, 35306L, 38314L, 41208L, 44085L, 46681L, 49619L,
52237L, 54270L, 56760L, 59092L, 60891L, 62802L, 65113L, 66434L,
68359L, 70729L, 74139L, 76192L, 78944L, 81427L, 83064L, 85509L,
89775L, 95990L, 96456L, 105997L, 111445L, 119804L, 128508L, 136800L,
146261L, 161707L, 174074L, 189434L, 206052L, 221060L, 241072L,
254477L, 270812L, 293665L, 322017L, 346349L, 367477L, 392991L,
411864L, 438395L, 463014L, 498925L, 529015L, 554287L, 577789L,
608557L, 629862L, 664043L, 693207L, 721689L, 771329L, 798239L,
825969L, 852382L, 884680L, 925752L, 989616L, 1026501L, 1066362L,
1097577L, 1130526L, 1166155L, 1210894L, 1249311L, 1284849L, 1337367L,
1370108L, 1416204L, 1452191L, 1506905L, 1545712L, 1592880L, 1648546L,
1688699L, 1740909L, 1792256L, 1850441L, 1900768L, 2008541L, 2062802L,
2117555L, 2180605L, 2235118L, 2297613L, 2363746L, 2440127L, 2509981L,
2585589L, 2639599L, 2743083L, 2821430L, 2890776L, 2959037L, 3030214L,
3084718L, 3235640L, 3317121L, 3395154L, 3480121L, 3558933L, 3644048L,
3714006L, 3793406L, 3890800L, 4008201L, 4088826L, 4183298L, 4298603L,
4366875L, 4458093L, 4561696L, 4677005L, 4769458L, 4875774L, 4981808L,
5070592L, 5164494L, 5281459L, 5397083L, 5681477L, 5790942L, 5986375L,
6105546L, 6228768L, 6373513L, 6531016L, 6665237L, 6804254L, 6929711L,
7041174L, 7181139L, 7322897L, 7482320L, 7634241L, 7793760L, 7944550L,
8032235L, 8190777L, 8364986L, 8541255L, 8710969L, 8939705L, 9158743L,
9293464L, 9468087L), Active = c(510L, 606L, 879L, 1353L, 2010L,
2784L, 5340L, 5908L, 7922L, 9495L, 11498L, 15966L, 18857L, 22585L,
26008L, 28750L, 31712L, 33792L, 36132L, 37906L, 39145L, 40594L,
52905L, 57554L, 58197L, 58790L, 58996L, 58908L, 57513L, 55962L,
55990L, 53661L, 53519L, 52180L, 50348L, 48826L, 47305L, 45238L,
44056L, 43492L, 42500L, 42406L, 41672L, 41752L, 43550L, 44999L,
46377L, 48294L, 51128L, 56136L, 60260L, 72243L, 80562L, 87657L,
99583L, 111865L, 128071L, 153246L, 178931L, 205266L, 230764L,
270546L, 299876L, 342468L, 394062L, 446240L, 501926L, 548569L,
593598L, 652803L, 708106L, 766582L, 828744L, 883354L, 936646L,
987151L, 1033269L, 1081957L, 1136621L, 1195879L, 1241261L, 1313438L,
1350754L, 1389164L, 1425770L, 1485109L, 1538671L, 1582655L, 1627840L,
1674431L, 1708343L, 1749230L, 1802911L, 1833537L, 1885981L, 1927315L,
1964957L, 2001495L, 2030701L, 2044556L, 2089540L, 2126040L, 2169008L,
2208008L, 2246497L, 2285111L, 2330371L, 2380782L, 2409845L, 2449784L,
2476456L, 2518521L, 2543331L, 2596361L, 2640327L, 2675902L, 2710929L,
2743712L, 2784005L, 2823963L, 2875184L, 2869027L, 2916066L, 2953499L,
2976234L, 3010040L, 3044932L, 3093412L, 3133456L, 3197279L, 3226335L,
3264452L, 3276956L, 3312425L, 3364867L, 3422693L, 3483577L, 3538590L,
3486196L, 3524135L, 3574922L, 3623295L, 3669280L, 3715937L, 3776026L,
3812839L, 3849981L, 3869178L, 3923452L, 4003513L, 4041511L, 4097815L,
4140884L, 4197308L, 4247921L, 4326728L, 4406931L, 4474521L, 4544278L,
4605227L, 4657028L, 4754082L, 4672273L, 4761307L, 4755072L, 4815151L,
4855475L, 4915467L, 4964447L, 5052995L, 5141244L, 5227218L, 5304469L,
5353468L, 5427502L, 5493703L, 5588524L, 5664300L, 5745518L, 5868451L,
5912527L, 5965535L, 6062930L, 6166006L, 6212290L, 6243930L, 6309711L,
6358362L), New.cases = c(0L, 99L, 287L, 493L, 684L, 809L, 2651L,
588L, 2068L, 1693L, 2111L, 4749L, 3100L, 4011L, 3745L, 3159L,
3532L, 2734L, 3027L, 2538L, 2042L, 1886L, 13645L, 6484L, 2075L,
2114L, 1936L, 1794L, 421L, 547L, 629L, 1761L, 323L, 564L, 853L,
963L, 1364L, 1377L, 1895L, 2384L, 1974L, 2577L, 2299L, 2791L,
3943L, 4038L, 3954L, 3900L, 5024L, 7663L, 5750L, 14405L, 11106L,
11146L, 14659L, 15838L, 19586L, 27918L, 29608L, 32306L, 33410L,
42165L, 41154L, 50817L, 62960L, 64400L, 67657L, 59577L, 64639L,
76416L, 76214L, 81399L, 83155L, 80463L, 71718L, 72672L, 76873L,
83775L, 86625L, 87658L, 76965L, 96802L, 69594L, 69927L, 80829L,
96712L, 87724L, 73958L, 80523L, 73339L, 75250L, 78994L, 89013L,
96974L, 84932L, 72948L, 68704L, 75404L, 79558L, 83681L, 87046L,
81853L, 77636L, 76078L, 79989L, 90022L, 90669L, 92997L, 85846L,
76255L, 76298L, 83619L, 84917L, 97106L, 96349L, 95412L, 78509L,
88284L, 96633L, 103028L, 106438L, 106900L, 105289L, 95326L, 87335L,
92742L, 102600L, 119314L, 121167L, 137833L, 107552L, 95195L,
121577L, 119389L, 126937L, 130863L, 135942L, 112259L, 102225L,
124342L, 134025L, 138114L, 129094L, 136010L, 133421L, 119638L,
141403L, 141853L, 139928L, 180954L, 157552L, 128539L, 137858L,
165338L, 171183L, 177887L, 191346L, 178110L, 162349L, 158572L,
173898L, 217689L, 207705L, 203495L, 193567L, 182695L, 167343L,
210844L, 211802L, 228227L, 232577L, 216328L, 192668L, 192779L,
221452L, 231122L, 252544L, 242038L, 237635L, 214647L, 206778L,
233565L, 280647L, 282756L, 281164L, 255545L, 204606L, 228693L
), New.deaths = c(0L, 1L, 8L, 16L, 14L, 26L, 49L, 2L, 38L, 42L,
46L, 103L, 64L, 66L, 72L, 70L, 85L, 87L, 100L, 107L, 100L, 5L,
253L, 152L, 143L, 104L, 98L, 140L, 115L, 123L, 4L, 207L, 10L,
160L, 80L, 60L, 43L, 57L, 69L, 54L, 89L, 75L, 95L, 93L, 112L,
99L, 244L, 184L, 279L, 344L, 305L, 497L, 417L, 641L, 680L, 804L,
897L, 1106L, 1478L, 1705L, 1697L, 1917L, 2268L, 2777L, 3007L,
3518L, 3679L, 3473L, 4164L, 4844L, 5551L, 6305L, 5985L, 5841L,
5021L, 5832L, 7902L, 6735L, 7629L, 7272L, 6069L, 5731L, 5747L,
6898L, 8312L, 7283L, 8890L, 6471L, 4570L, 5443L, 7157L, 6755L,
6850L, 6674L, 5578L, 3883L, 4649L, 6463L, 6768L, 5962L, 5177L,
5325L, 3453L, 4128L, 5871L, 6669L, 5409L, 5568L, 4265L, 3566L,
3473L, 5547L, 5228L, 5268L, 5215L, 4171L, 3329L, 3291L, 4784L,
4885L, 4786L, 5284L, 3989L, 3140L, 1178L, 4203L, 5213L, 4701L,
4742L, 4156L, 2888L, 3068L, 4823L, 5572L, 5149L, 4776L, 3881L,
2742L, 3697L, 4922L, 5205L, 4774L, 4297L, 4238L, 3374L, 3425L,
6867L, 5255L, 5029L, 6273L, 4249L, 3963L, 3571L, 5311L, 5261L,
6627L, 4827L, 4486L, 3089L, 3721L, 5132L, 5011L, 5120L, 4995L,
4369L, 3445L, 3797L, 6107L, 5319L, 5458L, 5311L, 4897L, 3954L,
3815L, 5660L, 5493L, 5799L, 6743L, 5627L, 4029L, 4160L, 6238L,
6983L, 9966L, 6144L, 4867L, 4104L, 5415L), New.recovered = c(0L,
2L, 6L, 3L, 13L, 9L, 46L, 18L, 16L, 78L, 62L, 178L, 145L, 217L,
250L, 347L, 485L, 567L, 587L, 657L, 703L, 432L, 1081L, 1683L,
1289L, 1417L, 1632L, 1742L, 1701L, 1975L, 597L, 3883L, 455L,
1743L, 2605L, 2425L, 2842L, 3387L, 3008L, 2894L, 2877L, 2596L,
2938L, 2618L, 2033L, 2490L, 2332L, 1799L, 1911L, 2311L, 1321L,
1925L, 2370L, 3410L, 2053L, 2752L, 2483L, 1637L, 2445L, 4266L,
6215L, 466L, 9541L, 5448L, 8359L, 8704L, 8292L, 9461L, 15446L,
12367L, 15360L, 16618L, 15008L, 20012L, 13405L, 16335L, 22853L,
28352L, 24332L, 21128L, 25514L, 18873L, 26531L, 24619L, 35911L,
30090L, 25272L, 23502L, 30768L, 21305L, 34181L, 29164L, 28482L,
49640L, 26910L, 27730L, 26413L, 32298L, 41072L, 63864L, 36885L,
39861L, 31215L, 32949L, 35629L, 44739L, 38417L, 35538L, 52518L,
32741L, 46096L, 35987L, 54714L, 38807L, 47168L, 55666L, 40153L,
52210L, 51347L, 58185L, 50327L, 107773L, 54261L, 54753L, 63050L,
54513L, 62495L, 66133L, 76381L, 69854L, 75608L, 54010L, 103484L,
78347L, 69346L, 68261L, 71177L, 54504L, 150922L, 81481L, 78033L,
84967L, 78812L, 85115L, 69958L, 79400L, 97394L, 117401L, 80625L,
94472L, 115305L, 68272L, 91218L, 103603L, 115309L, 92453L, 106316L,
106034L, 88784L, 93902L, 116965L, 115624L, 284394L, 109465L,
195433L, 119171L, 123222L, 144745L, 157503L, 134221L, 139017L,
125457L, 111463L, 139965L, 141758L, 159423L, 151921L, 159519L,
150790L, 87685L, 158542L, 174209L, 176269L, 169714L, 228736L,
219038L, 134721L, 174623L), Deaths...100.Cases = c(3.06, 2.75,
2.76, 2.93, 2.64, 2.8, 2.35, 2.16, 2.08, 2.15, 2.15, 2.16, 2.14,
2.06, 2.04, 2.06, 2.09, 2.17, 2.26, 2.38, 2.49, 2.4, 2.28, 2.28,
2.42, 2.5, 2.57, 2.69, 2.83, 2.97, 2.95, 3.15, 3.15, 3.33, 3.4,
3.43, 3.43, 3.44, 3.44, 3.41, 3.43, 3.42, 3.44, 3.43, 3.41, 3.37,
3.48, 3.52, 3.6, 3.66, 3.73, 3.7, 3.71, 3.84, 3.91, 4, 4.05,
4.04, 4.14, 4.26, 4.34, 4.36, 4.48, 4.58, 4.6, 4.7, 4.77, 4.86,
4.99, 5.1, 5.28, 5.47, 5.6, 5.72, 5.79, 5.91, 6.15, 6.25, 6.39,
6.49, 6.55, 6.52, 6.58, 6.7, 6.84, 6.87, 7, 7.05, 7.01, 7.02,
7.09, 7.14, 7.16, 7.18, 7.16, 7.11, 7.11, 7.14, 7.18, 7.18, 7.15,
7.13, 7.07, 7.04, 7.05, 7.05, 7.03, 7.01, 6.97, 6.92, 6.88, 6.88,
6.86, 6.83, 6.8, 6.75, 6.71, 6.65, 6.62, 6.58, 6.54, 6.51, 6.45,
6.4, 6.32, 6.29, 6.27, 6.22, 6.17, 6.1, 6.04, 6, 5.96, 5.94,
5.9, 5.86, 5.8, 5.74, 5.71, 5.68, 5.65, 5.61, 5.57, 5.53, 5.48,
5.44, 5.43, 5.4, 5.37, 5.33, 5.28, 5.25, 5.21, 5.18, 5.14, 5.11,
5.06, 5.01, 4.97, 4.92, 4.89, 4.84, 4.79, 4.75, 4.71, 4.66, 4.63,
4.6, 4.56, 4.52, 4.48, 4.44, 4.41, 4.37, 4.34, 4.31, 4.27, 4.24,
4.21, 4.18, 4.15, 4.12, 4.09, 4.08, 4.05, 4.02, 3.99, 3.97),
Recovered...100.Cases = c(5.05, 4.59, 3.83, 2.72, 2.46, 2.08,
1.92, 2.03, 1.71, 2.21, 2.33, 2.73, 3.04, 3.44, 3.87, 4.6,
5.54, 6.66, 7.62, 8.71, 9.89, 10.41, 9.85, 11.42, 12.95,
14.56, 16.41, 18.35, 20.52, 22.98, 23.58, 28.02, 28.49, 30.5,
33.44, 36.04, 38.91, 42.33, 44.91, 46.99, 49.17, 50.61, 52.48,
53.67, 53.59, 53.9, 54.08, 53.81, 53.14, 51.74, 50.48, 46.82,
45.02, 44.06, 41.65, 39.72, 37.29, 33.73, 31, 29.13, 28.1,
25.14, 24.95, 23.43, 22.24, 21.31, 20.4, 20.03, 20.34, 19.98,
19.99, 20.03, 19.88, 20.21, 20.13, 20.26, 20.77, 21.5, 21.86,
21.98, 22.47, 22.32, 22.89, 23.32, 24.15, 24.46, 24.63, 24.86,
25.3, 25.42, 26.01, 26.35, 26.54, 27.49, 27.61, 27.87, 28.1,
28.46, 29.06, 30.27, 30.59, 31.02, 31.22, 31.48, 31.76, 32.19,
32.45, 32.59, 33.2, 33.38, 33.88, 34.06, 34.65, 34.77, 35.07,
35.55, 35.81, 36.24, 36.57, 36.98, 37.2, 38.5, 38.76, 39.09,
39.61, 39.93, 40.31, 40.62, 41.08, 41.3, 41.8, 42.03, 42.85,
43.27, 43.48, 43.65, 43.82, 43.9, 45.39, 45.73, 45.96, 46.24,
46.49, 46.77, 46.87, 47.16, 47.53, 48.13, 48.29, 48.37, 48.82,
48.88, 49.14, 49.38, 49.71, 49.75, 49.87, 50.04, 50.12, 50.26,
50.54, 50.59, 52.24, 52.27, 53.11, 53.3, 53.59, 53.86, 54.22,
54.31, 54.41, 54.47, 54.52, 54.79, 54.94, 55.18, 55.27, 55.45,
55.59, 55.37, 55.67, 55.96, 56.09, 56.16, 56.61, 57.07, 57.18,
57.45), Deaths...100.Recovered = c(60.71, 60, 72.22, 107.69,
107.69, 134.43, 122.43, 106.4, 121.28, 97.26, 92.17, 78.87,
70.53, 59.93, 52.66, 44.71, 37.78, 32.63, 29.64, 27.28, 25.2,
23.06, 23.12, 20.01, 18.71, 17.15, 15.63, 14.66, 13.79, 12.93,
12.52, 11.25, 11.06, 10.92, 10.16, 9.52, 8.8, 8.12, 7.66,
7.26, 6.98, 6.76, 6.55, 6.4, 6.36, 6.26, 6.43, 6.54, 6.78,
7.07, 7.39, 7.91, 8.23, 8.72, 9.38, 10.07, 10.86, 11.98,
13.37, 14.63, 15.45, 17.36, 17.94, 19.55, 20.7, 22.04, 23.39,
24.25, 24.51, 25.55, 26.41, 27.34, 28.19, 28.27, 28.76, 29.18,
29.6, 29.08, 29.24, 29.54, 29.17, 29.22, 28.76, 28.72, 28.32,
28.09, 28.41, 28.38, 27.69, 27.62, 27.28, 27.1, 26.98, 26.11,
25.93, 25.53, 25.28, 25.09, 24.71, 23.72, 23.37, 22.99, 22.66,
22.36, 22.18, 21.91, 21.67, 21.5, 20.98, 20.74, 20.31, 20.19,
19.8, 19.64, 19.39, 18.99, 18.73, 18.36, 18.1, 17.8, 17.58,
16.9, 16.65, 16.36, 15.95, 15.74, 15.54, 15.31, 15.02, 14.77,
14.45, 14.27, 13.91, 13.72, 13.57, 13.42, 13.23, 13.08, 12.59,
12.43, 12.3, 12.13, 11.98, 11.82, 11.69, 11.53, 11.42, 11.22,
11.12, 11.02, 10.82, 10.74, 10.6, 10.48, 10.33, 10.27, 10.15,
10.02, 9.91, 9.8, 9.68, 9.56, 9.18, 9.09, 8.87, 8.75, 8.64,
8.54, 8.41, 8.32, 8.23, 8.15, 8.08, 7.98, 7.9, 7.8, 7.73,
7.65, 7.58, 7.55, 7.45, 7.37, 7.3, 7.27, 7.16, 7.04, 6.98,
6.91), No..of.countries = c(6L, 8L, 9L, 11L, 13L, 16L, 16L,
18L, 20L, 24L, 25L, 25L, 25L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 27L, 27L, 27L, 27L, 27L, 28L, 28L, 30L,
30L, 31L, 36L, 41L, 47L, 51L, 57L, 61L, 66L, 73L, 76L, 80L,
84L, 93L, 94L, 99L, 102L, 105L, 111L, 113L, 120L, 133L, 137L,
143L, 146L, 150L, 154L, 161L, 163L, 168L, 169L, 171L, 174L,
175L, 176L, 176L, 176L, 177L, 179L, 179L, 180L, 180L, 180L,
182L, 183L, 183L, 183L, 183L, 184L, 184L, 184L, 184L, 184L,
184L, 184L, 184L, 184L, 184L, 184L, 184L, 184L, 184L, 184L,
184L, 184L, 184L, 184L, 184L, 186L, 186L, 186L, 186L, 186L,
186L, 186L, 186L, 186L, 186L, 186L, 186L, 186L, 187L, 187L,
187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L,
187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L,
187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L,
187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L,
187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L,
187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L,
187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L, 187L,
187L, 187L, 187L, 187L)), class = "data.frame", row.names = c(NA,
-188L))
UPDATE
I got it to look like this now I just have a problem with numbers on Y axis
Updated

Convert specific columns of a timestamped matrix to rows for each timestamp index

I have a bit data.frame (now it's time stamped with xts, I hope it's not a problem), like this:
> head(testData)
Year Day Hour Min Sec. E1.S1 E1.S2 E1.S3 E1.S4 E1.S5 E1.S6
2000-05-01 10:25:59 2000 122 10 25.0 35.144 3.572000 2.912000 2.512 6.352 10.604000 8.078
2000-05-01 10:26:48 2000 122 10 26.0 31.570 2.203333 2.423333 6.160 13.000 9.463333 3.970
2000-05-01 10:27:48 2000 122 10 26.8 35.964 2.118000 3.044000 4.232 11.770 12.696000 6.088
E1.S7 E1.S8 E2.S1 E2.S2 E2.S3 E2.S4 E2.S5 E2.S6 E2.S7
2000-05-01 10:25:59 3.0444 3.574000 0.9244 1.5868 1.3224 1.852800 2.646000 2.910 2.382000
2000-05-01 10:26:48 3.3100 3.083333 1.1000 1.5440 1.9800 2.646667 2.646667 3.310 1.763333
2000-05-01 10:27:48 2.6440 2.646000 1.8528 1.8524 2.9120 2.648000 3.970000 5.292 1.850000
E2.S8 E3.S1 E3.S2 E3.S3 E3.S4 E3.S5 E3.S6 E3.S7
2000-05-01 10:25:59 1.586000 0.7920000 1.0568000 1.7184 1.720400 2.116400 0.9248 2.1180
2000-05-01 10:26:48 1.983333 0.4413333 0.2206667 1.5400 1.100667 2.646667 0.8800 0.8800
2000-05-01 10:27:48 1.452000 0.9264000 1.1908000 2.5140 1.718400 2.250400 2.2504 1.8504
E3.S8 E4.S1 E4.S2 E4.S3 E4.S4 E4.S5 E4.S6 E4.S7
2000-05-01 10:25:59 1.1880 0.9252000 0.1324000 0.6612 0.6604 0.5288000 0.132400 0.6612000
2000-05-01 10:26:48 2.2040 0.2206667 0.2206667 0.4400 0.4400 0.8813333 1.100667 0.8813333
2000-05-01 10:27:48 0.9240 0.7936000 0.7928000 0.9244 1.3208 0.6612000 0.132400 0.5288000
E4.S8 FP5.S1 FP5.S2 FP5.S3 FP5.S4 FP5.S5 FP5.S6 FP5.S7
2000-05-01 10:25:59 1.0560 0.0662000 0.3310 0.3968000 0.0662000 0.0000000 0.2648000 0.19860
2000-05-01 10:26:48 1.1000 0.1103333 0.3310 0.2206667 0.1103333 0.4413333 0.2206667 0.33100
2000-05-01 10:27:48 0.5288 0.2648000 0.4632 0.0000000 0.4632000 0.2648000 0.2648000 0.72660
FP5.S8 FP6.S1 FP6.S2 FP6.S3 FP6.S4 FP6.S5 FP6.S6 FP6.S7 FP6.S8
2000-05-01 10:25:59 0.2648000 0.1324 0.1986000 0.2648 0.00000 0.1324000 0.1324 0.1324 0.0662000
2000-05-01 10:26:48 0.2206667 0.0000 0.1103333 0.0000 0.00000 0.2206667 0.0000 0.0000 0.1103333
2000-05-01 10:27:48 0.1324000 0.0000 0.0000000 0.0000 0.00000 0.1986000 0.1324 0.1324 0.0662000
FP7.S1 FP7.S2 FP7.S3 FP7.S4 FP7.S5 FP7.S6 FP7.S7 FP7.S8 PA.LEFS60S1
2000-05-01 10:25:59 0.1324 0.0000 0.00000 0.0000 0.1324000 0.3310000 0.0000 0.39680 79.46
2000-05-01 10:26:48 0.0000 0.0000 0.00000 0.0000 0.4413333 0.1103333 0.0000 0.00000 78.50
2000-05-01 10:27:48 0.0000 0.3972 0.00000 0.1324 0.1324000 0.1986000 0.1324 0.00000 84.10
PA.LEFS60S2 PA.LEFS60S3 PA.LEFS60S4 PA.LEFS60S5 PA.LEFS60S6 PA.LEFS60S7
2000-05-01 10:25:59 83.26000 103.48 131.6000 157.6000 148.6000 120.60000
2000-05-01 10:26:48 99.93333 130.00 160.6667 153.6667 121.6667 92.96667
2000-05-01 10:27:48 95.68000 118.60 144.6000 155.0000 134.6000 109.10000
PA.LEFS60S8 BX BY BZ Bmag....nT.
2000-05-01 10:25:59 94.44000 3.608000 2.6620000 5.032000 6.8840
2000-05-01 10:26:48 75.63333 4.943333 -0.5133333 4.816667 6.9300
2000-05-01 10:27:48 89.44000 3.908000 0.9634000 5.490000 6.8460
> dput(head(testData))
structure(c(2000, 2000, 2000, 2000, 2000, 2000, 122, 122, 122,
122, 122, 122, 10, 10, 10, 10, 10, 10, 25, 26, 26.8, 28, 29,
30, 35.144, 31.57, 35.964, 21.4, 24.892, 25.354, 3.572, 2.20333333333333,
2.118, 3.8025, 4.628, 5.292, 2.912, 2.42333333333333, 3.044,
1.488, 3.97, 5.428, 2.512, 6.16, 4.232, 4.465, 3.836, 7.54, 6.352,
13, 11.77, 7.775, 8.604, 14.16, 10.604, 9.46333333333333, 12.696,
10.93, 16.68, 23.96, 8.078, 3.97, 6.088, 10.425, 15.612, 29.1,
3.0444, 3.31, 2.644, 5.135, 10.32, 20.612, 3.574, 3.08333333333333,
2.646, 3.1425, 3.966, 9.918, 0.9244, 1.1, 1.8528, 2.645, 4.368,
8.476, 1.5868, 1.544, 1.8524, 2.315, 3.572, 6.218, 1.3224, 1.98,
2.912, 3.4725, 6.34, 12.832, 1.8528, 2.64666666666667, 2.648,
6.4475, 8.86, 24.74, 2.646, 2.64666666666667, 3.97, 7.275, 12.948,
30.7, 2.91, 3.31, 5.292, 7.44, 12.988, 30.42, 2.382, 1.76333333333333,
1.85, 3.315, 12.174, 32.28, 1.586, 1.98333333333333, 1.452, 2.81,
7.286, 17.72, 0.792, 0.441333333333333, 0.9264, 1.655, 4.234,
10.194, 1.0568, 0.220666666666667, 1.1908, 1.1555, 3.8388, 7.016,
1.7184, 1.54, 2.514, 3.3105, 8.192, 14.004, 1.7204, 1.10066666666667,
1.7184, 4.1375, 11.504, 37.44, 2.1164, 2.64666666666667, 2.2504,
4.135, 17.876, 51.74, 0.9248, 0.88, 2.2504, 4.4675, 18.28, 52.38,
2.118, 0.88, 1.8504, 5.465, 14.696, 52.12, 1.188, 2.204, 0.924,
3.1425, 10.73, 30.82, 0.9252, 0.220666666666667, 0.7936, 0.331,
5.03, 14.152, 0.1324, 0.220666666666667, 0.7928, 1.983, 3.97,
10.714, 0.6612, 0.44, 0.9244, 1.653, 10.196, 20.1, 0.6604, 0.44,
1.3208, 3.9675, 20.76, 54.78, 0.5288, 0.881333333333333, 0.6612,
4.47, 20.52, 68, 0.1324, 1.10066666666667, 0.1324, 5.7925, 21.44,
78.42, 0.6612, 0.881333333333333, 0.5288, 3.4775, 24.604, 76.12,
1.056, 1.1, 0.5288, 2.313, 15.082, 42.86, 0.0662, 0.110333333333333,
0.2648, 0.579, 1.6552, 3.838, 0.331, 0.331, 0.4632, 1.1565, 2.446,
2.054, 0.3968, 0.220666666666667, 0, 0.41375, 1.7862, 3.97, 0.0662,
0.110333333333333, 0.4632, 0.9935, 2.976, 9.792, 0, 0.441333333333333,
0.2648, 0.6605, 2.78, 9.672, 0.2648, 0.220666666666667, 0.2648,
0.74425, 2.9782, 10.592, 0.1986, 0.331, 0.7266, 1.32375, 4.036,
9.136, 0.2648, 0.220666666666667, 0.1324, 0.908, 2.7144, 5.292,
0.1324, 0, 0, 0, 0, 0.1986, 0.1986, 0.110333333333333, 0, 0.1655,
0, 0, 0.2648, 0, 0, 0.1655, 0, 0, 0, 0, 0, 0.08275, 0.331, 0.1324,
0.1324, 0.220666666666667, 0.1986, 0.24825, 0.2648, 0.0662, 0.1324,
0, 0.1324, 0.331, 0.3972, 0.662, 0.1324, 0, 0.1324, 0, 0.1324,
0.1324, 0.0662, 0.110333333333333, 0.0662, 0.1655, 0.0662, 0.1324,
0.1324, 0, 0, 0, 0, 0.1324, 0, 0, 0.3972, 0.1655, 0.3968, 0,
0, 0, 0, 0.08275, 0.0662, 0.0662, 0, 0, 0.1324, 0.1655, 0, 0.2648,
0.1324, 0.441333333333333, 0.1324, 0, 0.1324, 0.0662, 0.331,
0.110333333333333, 0.1986, 0.331, 0.1986, 0.1986, 0, 0, 0.1324,
0, 0.2648, 0.1986, 0.3968, 0, 0, 0.08275, 0.1986, 0, 79.46, 78.5,
84.1, 89.2, 90.06, 93.8, 83.26, 99.9333333333333, 95.68, 92.4,
90.08, 87.88, 103.48, 130, 118.6, 108.75, 104.42, 97.36, 131.6,
160.666666666667, 144.6, 131.75, 127, 118, 157.6, 153.666666666667,
155, 149.75, 147.6, 141.2, 148.6, 121.666666666667, 134.6, 144,
147.6, 151.8, 120.6, 92.9666666666667, 109.1, 122, 127, 136,
94.44, 75.6333333333333, 89.44, 100.775, 104.4, 112.2, 3.608,
4.94333333333333, 3.908, 2.885, 2.548, 1.708, 2.662, -0.513333333333333,
0.9634, 2.0675, 2.544, 3.208, 5.032, 4.81666666666667, 5.49,
5.7875, 5.776, 5.768, 6.884, 6.93, 6.846, 6.8025, 6.838, 6.824
), .indexCLASS = c("POSIXlt", "POSIXt"), .indexTZ = "", tclass = c("POSIXlt",
"POSIXt"), tzone = "", class = c("xts", "zoo"), index = structure(c(957173159,
957173208, 957173268, 957173329, 957173389, 957173450), tzone = "", tclass = c("POSIXlt",
"POSIXt")), .Dim = c(6L, 73L), .Dimnames = list(NULL, c("Year",
"Day", "Hour", "Min", "Sec.", "E1.S1", "E1.S2", "E1.S3", "E1.S4",
"E1.S5", "E1.S6", "E1.S7", "E1.S8", "E2.S1", "E2.S2", "E2.S3",
"E2.S4", "E2.S5", "E2.S6", "E2.S7", "E2.S8", "E3.S1", "E3.S2",
"E3.S3", "E3.S4", "E3.S5", "E3.S6", "E3.S7", "E3.S8", "E4.S1",
"E4.S2", "E4.S3", "E4.S4", "E4.S5", "E4.S6", "E4.S7", "E4.S8",
"FP5.S1", "FP5.S2", "FP5.S3", "FP5.S4", "FP5.S5", "FP5.S6", "FP5.S7",
"FP5.S8", "FP6.S1", "FP6.S2", "FP6.S3", "FP6.S4", "FP6.S5", "FP6.S6",
"FP6.S7", "FP6.S8", "FP7.S1", "FP7.S2", "FP7.S3", "FP7.S4", "FP7.S5",
"FP7.S6", "FP7.S7", "FP7.S8", "PA.LEFS60S1", "PA.LEFS60S2", "PA.LEFS60S3",
"PA.LEFS60S4", "PA.LEFS60S5", "PA.LEFS60S6", "PA.LEFS60S7", "PA.LEFS60S8",
"BX", "BY", "BZ", "Bmag....nT.")))
For each timestamp, I need to plot (and get some stat info) of the E1.S* value versus the cos of the PA.LEFS60S* value. For instance, for the timestamp 2000-05-01 10:25:59:
# These are the values from PA.LEFS60S1 to PA.LEFS60S8
x = c(79.46, 83.26000, 103.48, 131.6000, 157.6000, 148.6000, 120.60000, 94.44000)
# These are the values from E1.S1 to E1.S8
y = c(3.572000, 2.912000, 2.512, 6.352, 10.604000, 8.078,3.0444, 3.574000)
plot(cos(x),y)
I will then need to plot something like the mean(y) vs the timestamp over a length of time... which I can only do after I get a way to automate the process.
So far I can only do this by hand. Although I can think of a way to do it with a for cycle and check each name from the table. But that seems too computationally slow and too complicated and not R like at all.
I tried my hand at the melt function - melt(testData) - from the rshape2 package, but the output just comes of a number of lines with a timeStamp as index and no column info.
Here's a solution for your problems. It involves some data reshaping.
Firstly, transform the data stored in values to rows. This is done separately for x and y data.
x <- stack(as.data.frame(t(testData[ , grep("^PA", names(testData))])))
y <- stack(as.data.frame(t(testData[ , grep("^E1", names(testData))])))
Combine data in one data frame:
xydf <- setNames(cbind(x, y[-2]), c("x", "time", "y"))
Figure 1
Plot y vs. cos(x) for the different timestamps. This is easy with the ggplot2 package.
library(ggplot2)
ggplot(xydf, aes(x = cos(x), y = y)) +
geom_point() +
facet_wrap( ~ time)
Figure 2
Calculate and plot the mean values of y along the timestamps.
yMeans <- aggregate(y ~ time, xydf, mean)
ggplot(yMeans, aes(x = time(testData), y = y)) +
geom_point() +
geom_line()

Resources