I've seen this question answered a few times, but can't seem to solve it using the advice on the various posts I've found.
I have a sample data set attached (dput
output at bottom). I'm trying this ggplot call to overlay a line graph of the 'scrapcount' column grouped by year, but getting a mix of vertical lines with some horizontal connections. How should I properly group this to get a single line for each year?
p01 <- ggplot(data, aes(x = factor(MeltMonth))) +
geom_bar(stat='identity', aes(y = weights)) +
geom_line(aes(y = scrapcount * 1000000, group = factor(MeltYear), color = factor(MeltYear)))
Sample Data:
data <- structure(list(MeltYear = c(2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018), MeltMonth = c(1,
1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7,
7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11,
11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 1, 1, 1, 1, 1, 1, 1,
1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7,
7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12), ScrapPct = c("0",
"0(T)", "28", "28(T)", "50", "50(T)", "70", NA, "0", "0(T)",
"28", "28(T)", "50", "50(T)", "60", "70", "70(T)", NA, "0", "28",
"28(T)", "50", "50(T)", "60(T)", "70", "70(T)", NA, "0", "28",
"50", "50(T)", "60(T)", "70", "70(T)", NA, "(T)", "0", "0(T)",
"25", "28", "28(T)", "50", "50(T)", "60", "70", "70(T)", "T",
NA, "0", "0(T)", "25", "28", "28(T)", "50", "50(T)", "60", "70",
"70(T)", NA, "0", "0(T)", "28", "28(T)", "50", "50(T)", "70",
"70(T)", NA, "0", "0(T)", "28", "28(T)", "50", "50(T)", "70",
"70(T)", NA, "0", "0(T)", "28", "28(T)", "50", "50(T)", "60(T)",
"70", NA, "0", "0(T)", "28", "28(T)", "50", "50(T)", "60", "70",
"70(T)", NA, "0", "0(T)", "25", "28", "28(T)", "50", "70", NA,
"0", "0(T)", "28", "28(T)", "50", "50(T)", "70", "70(T)", NA,
"0", "0(T)", "28", "28(T)", "50", "50(T)", "70", "70(T)", NA,
"0", "0(T)", "28", "28(T)", "50", "50(T)", "70", NA, "0", "0(T)",
"28", "28(T)", "50", "50(83 SCRAP)", "50(T)", "60", "70", NA,
"0", "28", "28(T)", "28(T)(83 SCRAP)", "50", "50(T)", "60(T)",
"70", "70(T)", NA, "0", "25", "28", "28(83 SCRAP)", "50", "70",
NA, "0", "28", "50", "50(83 GRADE SCRAP)", "70", NA, "0", "0(T)",
"25", "28", "28(T)", "50", "50(T)", "60(T)", "70", "70(T)", NA,
"0", "0(T)", "25", "28", "28(83 GRADE)", "28(83 GRADES SCRAP)",
"28(T)", "50", "50(T)", "70", "70(T)", NA, "0", "28", "28(T)",
"50", "50(T)", "70", "70(T)", NA, "0", "0(T)", "25", "28", "28(83 GRADE)",
"28(83 GRADE)*", "50", "50(T)", "70", "70(T)", NA, "0", "0(T)",
"25", "28", "28(83 GRADE)", "28(T)", "50", "50(T)", "70", "70(T)",
NA, "0", "25", "28", "28(83 GRADE)", "28(T)", "50", "50(T)",
"60", "70", NA, "0", "28", "28 (83 GRADE)", "28(83 GRADE)", "50",
"60", "70", NA, "0", "25", "28", "50", "70", "TURNINGS PUCKS",
NA, "0", "0(T)", "25", "28", "28(83 GRADE)", "50", "60", "70",
NA, "0", "28", "28(83 GRADE)", "50", "60", "70", NA, "0", "0(T)",
"25", "28", "50", "70", NA, "0", "0(T)", "28", "50", "60", "70",
NA, "0", "0(T)", "28", "50", "60", "70", NA, "0", "0(T)", "28",
"28(83 GRADE)", "50", "60", "70", NA, "0", "0(T)", "28", "28(83 GRADE)",
"50", "60", "70", NA, "0", "0(T)", "2", "25", "28", "28(83 GRADE)",
"28(T)", "50", "70", NA, "0", "0(T)", "25", "28", "28(83 GRADE)",
"28(T)", "50", "50(T)", "70", NA, "0", "25", "28", "28(83 GRADE)",
"28(T)", "50", "60", "70", NA), counts = c(11L, 1L, 21L, 1L,
38L, 6L, 4L, 4L, 6L, 1L, 1L, 5L, 23L, 41L, 2L, 5L, 2L, 1L, 3L,
1L, 8L, 8L, 49L, 1L, 4L, 1L, 2L, 9L, 3L, 20L, 17L, 3L, 18L, 1L,
9L, 1L, 10L, 1L, 1L, 3L, 2L, 23L, 22L, 3L, 12L, 11L, 1L, 2L,
10L, 7L, 1L, 7L, 2L, 16L, 12L, 2L, 14L, 19L, 6L, 8L, 4L, 8L,
2L, 16L, 6L, 9L, 4L, 5L, 24L, 5L, 6L, 7L, 17L, 5L, 20L, 3L, 3L,
20L, 1L, 3L, 5L, 16L, 8L, 3L, 23L, 1L, 8L, 8L, 9L, 11L, 19L,
11L, 2L, 14L, 4L, 4L, 30L, 1L, 1L, 10L, 1L, 33L, 16L, 7L, 32L,
2L, 12L, 1L, 23L, 2L, 23L, 1L, 1L, 16L, 5L, 8L, 10L, 20L, 8L,
22L, 6L, 1L, 15L, 20L, 3L, 5L, 16L, 2L, 31L, 1L, 18L, 3L, 8L,
2L, 32L, 2L, 1L, 4L, 24L, 5L, 7L, 9L, 2L, 4L, 29L, 10L, 2L, 19L,
4L, 1L, 19L, 1L, 11L, 5L, 35L, 13L, 3L, 27L, 5L, 27L, 2L, 21L,
5L, 21L, 1L, 2L, 13L, 1L, 18L, 4L, 1L, 21L, 3L, 3L, 35L, 1L,
1L, 12L, 2L, 1L, 7L, 12L, 12L, 19L, 3L, 3L, 18L, 9L, 1L, 11L,
6L, 18L, 11L, 5L, 21L, 2L, 4L, 3L, 6L, 1L, 9L, 16L, 46L, 12L,
8L, 18L, 2L, 1L, 8L, 2L, 8L, 18L, 19L, 18L, 7L, 6L, 17L, 1L,
26L, 1L, 6L, 21L, 2L, 4L, 21L, 3L, 23L, 35L, 1L, 2L, 27L, 1L,
28L, 5L, 19L, 2L, 26L, 27L, 32L, 1L, 7L, 27L, 1L, 1L, 29L, 1L,
11L, 1L, 39L, 4L, 6L, 29L, 4L, 27L, 3L, 30L, 10L, 17L, 2L, 2L,
34L, 38L, 17L, 5L, 19L, 5L, 22L, 28L, 1L, 19L, 1L, 16L, 1L, 22L,
31L, 2L, 41L, 3L, 23L, 2L, 26L, 3L, 24L, 2L, 33L, 7L, 15L, 4L,
27L, 2L, 20L, 1L, 18L, 3L, 8L, 1L, 1L, 1L, 34L, 3L, 1L, 34L,
31L, 3L, 16L, 1L, 3L, 30L, 3L, 6L, 40L, 3L, 40L, 1L, 8L, 1L,
30L, 1L, 1L, 30L, 4L, 33L, 6L), weights = c(207434, 20558, 405457,
20983, 773304, 124639, 82286, 61910, 113145, 20605, 20614, 103468,
469209, 824891, 40609, 103194, 41078, 19747, 40659, 12966, 164561,
166481, 988762, 20301, 82447, 20150, 36892, 126047, 46905, 386183,
324783, 62331, 339607, 12822, 159687, 20606, 139596, 20767, 10781,
47873, 40499, 454152, 421639, 61942, 233491, 204732, 20609, 16852,
188002, 125642, 10906, 137620, 41018, 287540, 215056, 41362,
260027, 370500, 101522, 109587, 81951, 165314, 41309, 291380,
121235, 180902, 66739, 82713, 347050, 103145, 123289, 143572,
331285, 102734, 406688, 61605, 41479, 294302, 20544, 61436, 101996,
315582, 149754, 61943, 458560, 796, 116777, 165160, 184396, 226196,
363011, 226587, 41220, 263464, 78782, 47515, 521489, 20615, 10940,
206040, 20568, 621913, 295439, 121067, 584891, 34120, 247005,
20446, 447898, 31850, 439005, 20280, 20795, 247398, 102617, 153709,
194364, 382904, 155901, 394641, 121533, 17776, 228900, 396567,
61301, 97307, 313930, 37251, 589038, 10876, 291268, 62104, 164330,
38625, 659758, 41353, 20362, 81574, 484278, 95130, 103180, 184596,
40916, 82539, 578253, 196441, 41283, 386344, 80641, 4524, 302367,
10904, 212691, 102562, 682525, 264078, 47083, 474865, 101992,
527168, 40921, 423338, 79317, 329508, 20551, 21425, 248116, 18992,
363368, 81955, 20544, 419755, 58566, 35268, 529855, 20411, 10056,
195126, 26996, 14011, 135918, 228135, 224486, 383255, 60683,
52010, 273119, 176914, 21049, 216170, 117754, 330027, 219768,
78750, 325536, 33499, 41276, 56717, 121974, 13701, 162125, 281719,
872330, 236383, 126602, 250243, 33408, 10873, 146578, 40011,
163488, 317153, 311218, 325656, 138412, 104445, 238556, 10732,
491907, 20490, 117758, 372741, 27347, 82084, 389548, 37320, NA,
654496, 20512, 40933, 464243, 20472, 547677, 84113, 257198, 21176,
477812, 522095, 627723, 13605, 81221, NA, 20473, 9786, 575789,
20550, 211013, 20434, 766259, 62348, 101575, 568512, 82770, 535744,
61289, 597523, 156643, 271068, 40732, 20816, 620291, 733227,
310834, 75924, 282832, 98107, 433121, 514064, 20641, 355869,
308, 238412, 18500, 397917, 567989, 41108, 779787, 40822, 369426,
40708, 456805, 47740, 467083, 41388, NA, 114518, 218779, 75025,
495130, 40359, 390211, 20806, 344959, 43407, 107150, 14774, 18447,
9812, 652705, 61640, 20162, 642691, 601030, 42583, 230841, 14764,
31447, 575805, 61468, 117472, 766114, 60831, 750711, 19982, 120162,
10239, 553813, 19018, 19955, NA, 79198, 597375, 107136), scrapcount = c(1L,
0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 3L, 0L, 1L, 1L,
0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 2L,
0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L,
1L, 0L, 0L, 0L, 0L, 1L, 0L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
3L, 0L, 0L, 1L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 1L, 0L, 2L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 2L, 0L, 1L,
0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 3L, 0L, 0L,
0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 2L, 0L, 0L, 0L, 1L, 0L, 1L,
0L, 0L, 0L, 0L, 0L, 3L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 2L, 0L, 0L, 0L, 1L, 0L,
1L, 0L, 2L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 1L, 0L, 1L, 7L, 1L, 0L,
1L, 0L, 0L, 2L, 2L, 0L, 5L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L,
1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 5L, 0L, 0L, 1L, 0L, 0L, 0L,
0L, 1L, 1L, 0L, 1L, 2L, 1L, 1L, 0L, 1L, 0L, 0L, 2L, 0L, 2L, 0L,
0L, 1L, 0L, 0L, 0L, 1L, 0L, 8L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 4L, 0L, 0L, 1L, 0L, 0L, 0L, 2L, 0L,
0L, 0L, 0L, 0L, 1L, 0L, 0L, 3L, 0L, 0L, 0L, 0L, 2L, 0L, 3L, 1L,
2L, 2L, 0L, 2L, 1L, 3L, 0L, 3L, 0L, 0L, 0L, 1L, 0L, 5L, 0L, 1L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 2L, 2L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
2L, 0L)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -323L), vars = c("MeltYear", "MeltMonth"
), drop = TRUE, indices = list(0:7, 8:17, 18:26, 27:34, 35:47,
48:58, 59:67, 68:76, 77:85, 86:95, 96:103, 104:112, 113:121,
122:129, 130:139, 140:149, 150:156, 157:162, 163:173, 174:185,
186:193, 194:204, 205:215, 216:225, 226:233, 234:240, 241:249,
250:256, 257:263, 264:270, 271:277, 278:285, 286:293, 294:303,
304:313, 314:322), group_sizes = c(8L, 10L, 9L, 8L, 13L,
11L, 9L, 9L, 9L, 10L, 8L, 9L, 9L, 8L, 10L, 10L, 7L, 6L, 11L,
12L, 8L, 11L, 11L, 10L, 8L, 7L, 9L, 7L, 7L, 7L, 7L, 8L, 8L, 10L,
10L, 9L), biggest_group_size = 13L, labels = structure(list(MeltYear = c(2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018), MeltMonth = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12)), class = "data.frame", row.names = c(NA,
-36L), vars = c("MeltYear", "MeltMonth"), drop = TRUE))
Current Output:
A sample of a different graph I made with correct grouping, for sake of showing what I'm attempting to do: