Home > Enterprise >  How to create a second y-axis based off of grouping aesthetic
How to create a second y-axis based off of grouping aesthetic

Time:07-13

I've seen some posts about how to make a secondary y-axis when you have two different columns for example, but I want to make a secondary y-axis that only affects one of my grouping variables.

Here is my code. It's a huge data set so I only listed the first 500 rows:

> dput(totalallclutches[1:500, ])
structure(list(Year = structure(c(8958, 8958, 8958, 10419, 10419, 
10784, 10784, 10784, 10784, 10784, 11515, 11515, 11515, 11515, 
11515, 11515, 11515, 11515, 11515, 11515, 11515, 11515, 11515, 
11515, 11515, 11515, 11515, 11515, 11515, 11515, 11515, 11515, 
11515, 11515, 11515, 11515, 11515, 11515, 11515, 11515, 11515, 
11515, 11515, 11515, 11515, 11515, 11515, 11515, 11515, 11515, 
11515, 11515, 11515, 11515, 11515, 11515, 11515, 11515, 11515, 
12245, 12245, 12245, 12245, 12245, 12245, 12245, 12245, 12245, 
12245, 12245, 12245, 12245, 12245, 12245, 12245, 12245, 12245, 
12245, 12245, 12245, 12245, 12245, 12245, 12245, 12245, 12245, 
12245, 12245, 12245, 12245, 12245, 12245, 12245, 12245, 12245, 
12245, 12245, 12245, 12976, 14802, 14802, 14802, 14802, 14802, 
14802, 14802, 14802, 14802, 14802, 14802, 14802, 14802, 14802, 
14802, 14802, 14802, 14802, 14802, 14802, 14802, 14802, 14802, 
14802, 14802, 14802, 14802, 14802, 14802, 14802, 14802, 14802, 
14802, 14802, 14802, 14802, 14802, 14802, 14802, 14802, 14802, 
14802, 15533, 15533, 15533, 15533, 15533, 15533, 15533, 15533, 
15533, 15533, 15533, 15533, 15533, 15533, 15533, 15533, 15533, 
15533, 15533, 15533, 15533, 15533, 15533, 15533, 15533, 15533, 
15533, 15533, 15533, 15533, 15533, 15533, 15533, 15533, 15533, 
15533, 15533, 15533, 15533, 15533, 15533, 15533, 15533, 15533, 
15533, 15533, 15533, 15533, 15533, 15533, 15533, 15533, 15533, 
15533, 15533, 15533, 15533, 15533, 15533, 15533, 15533, 15533, 
15533, 15533, 15533, 15533, 15533, 15533, 15533, 15898, 15898, 
15898, 15898, 15898, 15898, 15898, 15898, 15898, 15898, 15898, 
15898, 15898, 15898, 15898, 15898, 15898, 15898, 15898, 15898, 
15898, 15898, 15898, 15898, 15898, 15898, 15898, 15898, 15898, 
15898, 15898, 15898, 15898, 15898, 15898, 15898, 15898, 15898, 
15898, 15898, 15898, 15898, 15898, 15898, 15898, 15898, 15898, 
15898, 15898, 15898, 15898, 15898, 15898, 15898, 15898, 16263, 
16263, 16263, 16263, 16263, 16263, 16263, 16263, 16263, 16263, 
16263, 16263, 16263, 16263, 16263, 16263, 16263, 16263, 16263, 
16263, 16263, 16263, 16263, 16263, 16263, 16263, 16263, 16263, 
16263, 16263, 16263, 16263, 16263, 16263, 16263, 16263, 16263, 
16263, 16263, 16263, 16263, 16263, 16263, 16263, 16263, 16263, 
16263, 16263, 16263, 16263, 16263, 16263, 16263, 16263, 16263, 
16263, 16263, 16263, 16628, 16628, 16628, 16628, 16628, 16628, 
16628, 16628, 16994, 16994, 16994, 16994, 16994, 16994, 16994, 
16994, 16994, 16994, 16994, 16994, 16994, 17359, 17359, 17359, 
17359, 17359, 17359, 17359, 17359, 17359, 17359, 17359, 17359, 
17359, 17359, 17359, 17359, 17724, 17724, 17724, 17724, 17724, 
17724, 17724, 17724, 17724, 17724, 17724, 17724, 17724, 17724, 
17724, 17724, 17724, 17724, 17724, 17724, 17724, 17724, 17724, 
17724, 18455, 18455, 18455, 18455, 18455, 18455, 18455, 18455, 
18455, 18455, 18455, 18455, 18455, 18455, 18820, 18820, 18820, 
18820, 18820, 18820, 18820, 18820, 18820, 18820, 18820, 18820, 
18820, 18820, 8958, 8958, 8958, 8958, 9323, 9323, 9323, 9323, 
9323, 9323, 9323, 9323, 9323, 9323, 9323, 9689, 9689, 9689, 9689, 
10054, 10054, 10054, 10054, 10054, 10419, 10419, 10419, 10419, 
10419, 10419, 10419, 10419, 10419, 10419, 10419, 10419, 10419, 
10419, 10419, 10419, 10419, 10419, 10419, 10784, 10784, 10784, 
10784, 10784, 10784, 10784, 10784, 10784, 10784, 10784, 10784, 
10784, 10784, 10784, 10784, 10784, 10784, 10784, 10784, 10784, 
10784, 10784, 10784, 10784, 10784, 10784, 10784, 10784, 10784, 
10784, 10784, 10784, 10784, 10784, 10784, 10784, 10784, 11150, 
11150, 11150, 11150, 11150, 11150, 11150), class = "Date"), PVC = c("5011583", 
"6013894", "6021627", "6014326", "6070862", "6008710", "6008806", 
"601404_", "6014829", "6019049", "226", "24V", "259", "27X", 
"28R", "28W", "2A7", "2AV", "2CS", "2D3", "2HZ", "37V", "A3J", 
"A9T", "AHZ", "AP8", "B3S", "B7L", "BMU", "BTL", "BXT", "BY0", 
"F1X", "FCF", "FU5", "FW2", "H94", "HJ4", "HRJ", "HT5", "HX1", 
"HY3", "J4H", "JBV", "SJM", "SUL", "SVJ", "USM", "UUA", "UX8", 
"UYF", "W2D", "X11", "XFS", "XLA", "XM5", "Z8T", "ZHD", "ZNC", 
"2CF", "2JJ", "355", "3CF", "3M4", "65W", "6BZ", "6HU", "6NV", 
"6P2", "AA2R", "AASC", "AB4D", "AB4H", "AUR", "AW5", "B4M", "BC4", 
"BR3", "BY0", "BZU", "FS4", "FZ8", "H7M", "H80", "HD3", "HL5", 
"HNT", "J4H", "NAZJ", "NAZR", "NAZV", "UHA", "UJ6", "UTS", "UVF", 
"XXN", "Z88", "ZAT", "6006194", "57F", "6IZ", "AAFT", "AAXY", 
"AB7D", "ADAW", "AFHD", "AFNZ", "AHDR", "AL83", "AL94", "ALDW", 
"AMDA", "AMDS", "AMN2", "AMNA", "AMWH", "AN76", "AN7Z", "ANUD", 
"ANZR", "AP21", "APPS", "APYJ", "ARH2", "AS96", "ASU9", "ASZC", 
"AT4C", "AT5P", "AT7D", "ATA2", "ATFC", "ATSZ", "ATXF", "AVAT", 
"AVMJ", "AWA2", "AWAU", "AXH3", "BDJ2", "ST9", "5081099", "6034409", 
"67D", "67L", "AA1D", "AA5N", "AB4R", "AB9Y", "ABL5", "ABRB", 
"ACDS", "AFC1", "AFHF", "AFR8", "AFYN", "AHCF", "AL83", "AL8M", 
"ALX9", "AM0B", "AM7F", "AMDA", "ANU1", "AP3Z", "AP7U", "APMP", 
"APMY", "APYW", "AR1M", "AR5Z", "AR6W", "AR8Z", "ARW3", "ARWV", 
"ARYH", "ARYY", "AS38", "ASUB", "ASZC", "AT0W", "AT11", "AT4U", 
"ATDT", "ATFA", "ATFH", "ATLX", "ATXB", "AUNN", "AUZR", "AV8W", 
"AVC6", "AVVU", "AVZ2", "AWA0", "AWCB", "AWHY", "AY0B", "AY2F", 
"AYN5", "AZWN", "BBAS", "BDJB", "BDJC", "BDJF", "FS4", "FSD", 
"HLN", "SHB", "UUC", "3M4", "59V", "6HJ", "6P5", "AAFM", "ABDH", 
"AD10", "AD5W", "AFXZ", "AHMU", "AHNL", "AL50", "AMM6", "AN0W", 
"AN3T", "ANRP", "ANYH", "AP2S", "APAB", "APXR", "ARXF", "ARZ9", 
"AS0B", "AS1J", "AS38", "ASPF", "ASZC", "AT63", "AT98", "ATBY", 
"AUNM", "AV3V", "AV4C", "AV8W", "AVPU", "AWJM", "AXM4", "AXRH", 
"AXSL", "AXWU", "AY0P", "AYFU", "AYVV", "AZWN", "BA8W", "BAF4", 
"BALT", "BANV", "BAXL", "BAYN", "BBAU", "BC9M", "BDHP", "W1P", 
"ZYD", "3JF", "66L", "AAV9", "AB47", "ABMW", "AFJF", "AHAN", 
"AHDZ", "AMCZ", "AMNH", "AMNV", "AMXA", "AMY7", "ANWD", "APNH", 
"AR1B", "AR56", "ARZZ", "ASWF", "ATLN", "ATMT", "ATXB", "ATZ5", 
"ATZN", "AVCZ", "AVF7", "AVHL", "AVLB", "AVPA", "AWFY", "AX0D", 
"AXW4", "AXWU", "AXZ8", "AY1Y", "AY45", "AZHH", "AZPS", "BA8S", 
"BAS2", "BAWA", "BBH6", "BBM5", "BBN6", "BBSY", "BC2J", "BC78", 
"BC9M", "BDM6", "BDN3", "BDNZ", "BLW0", "BLW9", "BLWB", "BMT5", 
"BMYT", "S9F", "ZCA", "AADJ", "AD4A", "ANWH", "APD0", "AS1P", 
"BBAF", "BBR3", "BCY2", "ACAD", "ADHY", "ALX7", "AT4Y", "BAHF", 
"BBHW", "BBR7", "BN02", "BN8B", "BPT1", "BPVC", "BRM5", "BSAY", 
"57F", "AABB", "AMXA", "AP6Z", "ARZ9", "AT4Y", "AVU6", "AX88", 
"BAXH", "BDJV", "BLU4", "BNSS", "BPC3", "BR10", "BT7B", "BT88", 
"6201191", "6216732", "ACCZ", "ACWW", "AM65", "ASX6", "AUA1", 
"AXJZ", "BJ89", "BMN1", "BPLU", "BPT5", "BPYW", "BRP8", "BSD3", 
"BT4A", "BTAU", "BTLU", "BTSB", "BTSX", "BTU1", "BTYS", "BV29", 
"BW3U", "24N", "AZ2B", "BBHP", "BL0F", "BN22", "BSAT", "BTPJ", 
"BUJJ", "BV7W", "BW4L", "BX6V", "BX93", "BYW8", "CLF4", "ALC5", 
"AR4F", "ATHH", "AZUU", "BTRJ", "BV11", "BWCX", "BXPV", "BYNR", 
"CANM", "CAUD", "CAV0", "CCS3", "CDDW", "FLF", "H02", "SHX", 
"SYB", "AWC", "B4U", "BBF", "FJX", "FL0", "FSU", "FZD", "HF0", 
"HRS", "HSN", "HUW", "BRS", "FLF", "SHX", "SYB", "BX8", "FF4", 
"SRF", "SV2", "UUU", "A3F", "ALX", "B84", "BJ5", "FS3", "FYA", 
"HF0", "HP7", "HVF", "S0J", "SN8", "SY3", "SZL", "UF1", "UF2", 
"UJP", "US9", "UZV", "XU2", "AJX", "ARVN", "B47", "B5V", "BB4", 
"BR9", "BY7", "BYF", "FDP", "FNJ", "FS3", "H06", "H60", "H7J", 
"H7V", "H87", "HBL", "HBN", "HCN", "HDZ", "HHW", "HSX", "HYM", 
"S87", "SD5", "SD7", "SHJ", "SMF", "SNZ", "SXS", "UF9", "ULN", 
"UUR", "UZ2", "W0B", "X3P", "XM9", "XT1", "AX0", "B52", "B6S", 
"FUJ", "HBL", "JVF", "S6L"), totalV = c(131.6018329, 121.6331389, 
116.9765837, 105.667161, 118.4299258, 114.9047203, 117.5233062, 
116.5322925, 118.1339155, 116.2714585, 102.4863282, 108.5285789, 
113.1364641, 117.747205, 122.3467623, 104.3666827, 107.8726138, 
106.9625831, 113.7929149, 110.8721442, 115.9880061, 111.5857755, 
119.1873412, 116.7911294, 112.2536346, 115.0591397, 121.0337775, 
116.7466473, 113.1418614, 128.5070682, 122.2704427, 106.3206962, 
118.9550288, 121.7254435, 111.603327, 110.0876149, 106.7025234, 
117.2408514, 120.7062814, 103.7206323, 116.4009805, 128.045744, 
121.3478511, 118.9532019, 49.45295491, 128.6760267, 132.6346037, 
120.6009934, 118.7817434, 124.7662675, 111.2594598, 104.5553709, 
122.4633892, 119.3739294, 126.682638, 106.9713708, 121.1934882, 
130.0889188, 112.8780362, 119.9326055, 122.0006055, 112.172087, 
108.7872223, 126.0288601, 115.8676876, 106.4316843, 127.6369473, 
113.6148329, 118.1658092, 112.2360554, 119.7377086, 117.765548, 
102.4747253, 128.2271789, 117.3562433, 118.4866548, 114.183012, 
110.4440261, 108.2646864, 113.1514836, 111.4815298, 108.7433465, 
104.3432003, 110.5550054, 117.440845, 117.9241665, 108.4544035, 
111.6629775, 125.0572435, 129.7674922, 123.861489, 126.7124926, 
112.8391993, 113.8922681, 123.4608411, 114.356986, 129.1599628, 
121.516281, 117.9503116, 121.5676602, 122.0829463, 108.012758, 
134.3486695, 103.4605476, 107.5687722, 116.1094002, 119.714098, 
117.7733424, 121.8298802, 121.7160697, 110.9904055, 118.0865673, 
123.4568218, 117.3452546, 131.0026367, 117.7924058, 119.3052676, 
104.1455946, 114.811271, 99.0447486, 105.177033, 118.6790913, 
127.2524459, 117.154889, 119.7971737, 118.1718387, 117.4795318, 
106.0466234, 114.3501252, 113.0164811, 109.2823174, 117.3524757, 
113.6854112, 122.3405476, 130.7284853, 102.5161107, 122.9776554, 
116.7859683, 119.2017297, 126.241793, 118.1155626, 103.1499388, 
119.0884567, 124.6705907, 126.0636876, 130.3184846, 105.5303233, 
113.61759, 129.0353953, 105.4736549, 121.4086987, 107.8785127, 
121.9076598, 108.8212205, 124.7127931, 111.4993225, 111.5563785, 
115.3521284, 114.6425629, 114.21554, 118.9558923, 119.0700037, 
116.7009704, 123.921602, 117.5557628, 116.5174096, 131.9558219, 
126.9829357, 118.079028, 121.4487015, 113.2053392, 116.5042148, 
122.7048988, 111.4258781, 121.12622, 101.2834365, 123.411701, 
108.3372806, 116.7830533, 120.7845205, 126.1979244, 111.8676328, 
111.870228, 102.5205114, 108.1974661, 119.2761263, 110.4055771, 
121.4487015, 123.8270158, 126.387104, 117.3609637, 121.435421, 
119.0107638, 118.0037677, 120.0228054, 121.8205003, 125.3974372, 
123.0644717, 111.5511923, 107.3733467, 114.1060517, 109.3090944, 
117.2363808, 110.0107061, 110.2841132, 110.3899746, 116.6335558, 
121.3379728, 124.6134135, 119.6555039, 124.1803107, 126.1938849, 
115.9446234, 115.9505752, 127.974689, 126.666155, 122.7534953, 
110.5494607, 115.7819844, 116.8549059, 115.7259175, 109.2734209, 
108.9435086, 117.4885748, 123.9135282, 107.3678973, 113.4920653, 
118.6197091, 127.0241214, 111.1504814, 116.088352, 122.1693606, 
126.6287896, 116.4231309, 111.5751727, 119.3398449, 121.5877516, 
113.0287151, 112.7432828, 116.3476639, 115.3097815, 116.8899204, 
108.6268232, 127.6278386, 121.0176412, 128.1309084, 106.7970588, 
118.6722185, 112.5626723, 107.0222123, 100.2796993, 132.3378163, 
104.3913088, 125.1045596, 109.662067, 119.6144768, 100.4648747, 
119.3865455, 116.3864732, 120.5757669, 117.827604, 108.8201353, 
104.3975385, 111.3303685, 122.0257886, 119.0668479, 119.3023355, 
121.2683027, 105.2890015, 120.814141, 129.1543049, 124.1409123, 
117.0501127, 128.9673083, 133.6906251, 116.2739231, 114.6621303, 
110.8665325, 120.5439847, 133.9128103, 130.9654076, 126.3771326, 
113.2217083, 134.0968422, 105.196334, 109.6392382, 127.2512258, 
121.8459572, 114.5902236, 121.0630058, 101.0235483, 127.3161672, 
115.2335646, 117.9205713, 125.1979801, 125.1684674, 132.7065513, 
114.1796664, 121.0630058, 116.8617977, 68.36697055, 125.2691129, 
120.5737015, 108.1392565, 124.2522764, 123.9940081, 125.0058615, 
118.7543814, 118.9010421, 119.498612, 115.2657491, 121.8470915, 
121.1704198, 121.9905032, 130.8111257, 116.6688774, 124.8231665, 
106.8357579, 114.5683171, 119.4905499, 116.7229925, 111.5789216, 
112.4101634, 111.7051246, 114.6708194, 113.8119408, 122.7011856, 
122.6943538, 121.1713403, 115.0867532, 123.7806437, 115.5399468, 
135.4582258, 126.1642917, 111.5943309, 120.0303614, 120.6775038, 
115.4367804, 107.6632678, 117.5165196, 109.7481209, 130.4845644, 
119.8516737, 123.7833659, 117.8585523, 122.032622, 119.469508, 
125.9634657, 101.4344062, 104.126189, 131.223116, 128.6743773, 
127.2468355, 130.6262905, 120.1339877, 129.9922532, 129.1644314, 
117.8089786, 123.3267751, 122.5651423, 103.2079772, 110.881453, 
123.4850515, 114.4174006, 107.0808335, 125.0812198, 131.8007237, 
108.3691646, 134.9262667, 125.0465665, 107.8226334, 116.4330426, 
112.194929, 118.022605, 114.3843496, 115.3801048, 115.6865254, 
110.0846727, 124.4159014, 118.296001, 106.63435, 126.2929992, 
122.0851843, 104.2476729, 119.354081, 123.602579, 109.817004, 
120.5975484, 124.4127023, 116.6287398, 120.0886523, 118.2483875, 
123.2076419, 123.3892528, 114.522925, 115.9233177, 113.9265951, 
129.1353777, 129.9883277, 95.73088265, 121.7574686, 130.631093, 
121.024781, 122.2031774, 112.0123373, 115.6749087, 112.0091529, 
111.9336735, 120.8355632, 105.3312384, 108.2824554, 122.9375219, 
111.848167, 112.8317712, 122.3045888, 103.0296228, 131.3337536, 
108.3938634, 130.5314026, 122.1001988, 116.9822811, 120.5157432, 
123.1375218, 107.5338747, 103.7477955, 110.8088961, 119.3797976, 
134.2686722, 111.848167, 122.3045888, 103.0296228, 119.3419983, 
113.3529114, 118.8081843, 125.8480059, 115.3044369, 114.2992601, 
122.3745066, 120.0332689, 117.0906551, 113.5586701, 101.1290673, 
118.5793027, 121.3770691, 128.8513528, 123.4508524, 123.0707692, 
112.3101236, 125.615032, 116.1109767, 110.7473293, 135.6858345, 
107.2053246, 106.4504622, 118.3513584, 125.1690869, 124.7997977, 
113.719745, 114.4273861, 109.4165807, 123.1805267, 116.5864038, 
117.6517911, 117.9783448, 120.8033836, 117.0910664, 119.5280712, 
108.8670093, 113.1329848, 122.7060133, 126.4322064, 134.9426364, 
112.3628963, 120.457483, 116.9261156, 123.7309108, 114.920379, 
112.0286569, 99.73531631, 126.3667774, 114.6714749, 122.1042563, 
115.4482439, 106.2185984, 114.4795498, 115.7840558, 107.5628015, 
110.5925107, 111.0374244, 107.5702436, 113.918606, 112.057225, 
128.9314039, 112.5191407, 124.8951979, 118.3342956, 123.1573414, 
132.1582224, 127.9064171, 120.6854505), averageV = c(65.80091644, 
60.81656944, 58.48829186, 52.83358048, 59.21496291, 57.45236013, 
58.76165312, 58.26614623, 59.06695775, 58.13572923, 51.24316411, 
54.26428947, 56.56823203, 58.87360248, 61.17338114, 52.18334137, 
53.9363069, 53.48129157, 56.89645744, 55.4360721, 57.99400307, 
55.79288776, 59.59367062, 58.39556471, 56.12681732, 57.52956987, 
60.51688874, 58.37332364, 56.57093068, 64.2535341, 61.13522137, 
53.16034811, 59.47751438, 60.86272173, 55.80166352, 55.04380747, 
53.35126172, 58.62042568, 60.35314071, 51.86031615, 58.20049025, 
64.022872, 60.67392557, 59.47660093, 24.72647746, 64.33801337, 
66.31730187, 60.3004967, 59.39087168, 62.38313375, 55.62972991, 
52.27768547, 61.23169461, 59.68696469, 63.34131901, 53.48568538, 
60.59674412, 65.04445939, 56.43901809, 59.96630277, 61.00030276, 
56.08604349, 54.39361117, 63.01443005, 57.93384378, 53.21584213, 
63.81847367, 56.80741645, 59.08290458, 56.11802771, 59.86885432, 
58.88277399, 51.23736264, 64.11358945, 58.67812166, 59.2433274, 
57.09150601, 55.22201306, 54.1323432, 56.57574178, 55.74076491, 
54.37167326, 52.17160014, 55.27750269, 58.7204225, 58.96208327, 
54.22720173, 55.83148874, 62.52862177, 64.88374609, 61.9307445, 
63.35624632, 56.41959964, 56.94613403, 61.73042056, 57.17849302, 
64.57998139, 60.75814049, 58.9751558, 60.78383008, 61.04147313, 
54.00637898, 67.17433477, 51.73027379, 53.7843861, 58.05470008, 
59.857049, 58.88667118, 60.91494009, 60.85803486, 55.49520275, 
59.04328367, 61.72841092, 58.67262732, 65.50131833, 58.8962029, 
59.65263382, 52.07279731, 57.40563549, 49.5223743, 52.58851651, 
59.33954564, 63.62622295, 58.57744452, 59.89858684, 59.08591937, 
58.73976588, 53.02331172, 57.17506262, 56.50824057, 54.64115872, 
58.67623783, 56.84270562, 61.17027378, 65.36424265, 51.25805536, 
61.48882768, 58.39298415, 59.60086487, 63.12089649, 59.05778131, 
51.5749694, 59.54422837, 62.33529536, 63.03184381, 65.15924229, 
52.76516166, 56.80879498, 64.51769764, 52.73682745, 60.70434935, 
53.93925635, 60.95382992, 54.41061026, 62.35639654, 55.74966127, 
55.77818927, 57.67606421, 57.32128146, 57.10777002, 59.47794615, 
59.53500183, 58.35048522, 61.96080101, 58.7778814, 58.25870482, 
65.97791095, 63.49146784, 59.03951399, 60.72435074, 56.60266961, 
58.25210739, 61.35244939, 55.71293904, 60.56311, 50.64171825, 
61.70585051, 54.1686403, 58.39152665, 60.39226025, 63.0989622, 
55.93381638, 55.93511398, 51.26025572, 54.09873307, 59.63806314, 
55.20278856, 60.72435074, 61.9135079, 63.19355202, 58.68048187, 
60.71771048, 59.50538191, 59.00188383, 60.0114027, 60.91025016, 
62.69871861, 61.53223585, 55.77559613, 53.68667336, 57.05302586, 
54.65454718, 58.6181904, 55.00535306, 55.14205661, 55.19498728, 
58.3167779, 60.66898639, 62.30670674, 59.82775195, 62.09015534, 
63.09694244, 57.9723117, 57.9752876, 63.9873445, 63.33307752, 
61.37674765, 55.27473035, 57.89099221, 58.42745296, 57.86295874, 
54.63671045, 54.4717543, 58.74428739, 61.95676412, 53.68394865, 
56.74603267, 59.30985455, 63.51206069, 55.57524072, 58.04417598, 
61.08468029, 63.3143948, 58.21156543, 55.78758635, 59.66992247, 
60.79387578, 56.51435753, 56.37164139, 58.17383193, 57.65489074, 
58.44496018, 54.31341158, 63.8139193, 60.50882058, 64.06545418, 
53.39852939, 59.33610926, 56.28133614, 53.51110617, 50.13984963, 
66.16890816, 52.19565439, 62.55227981, 54.83103348, 59.80723839, 
50.23243734, 59.69327274, 58.19323658, 60.28788343, 58.91380201, 
54.41006763, 52.19876923, 55.66518427, 61.0128943, 59.53342396, 
59.65116774, 60.63415136, 52.64450075, 60.40707051, 64.57715243, 
62.07045615, 58.52505633, 64.48365413, 66.84531257, 58.13696157, 
57.33106513, 55.43326625, 60.27199235, 66.95640515, 65.4827038, 
63.1885663, 56.61085417, 67.04842108, 52.59816699, 54.8196191, 
63.62561288, 60.92297859, 57.29511182, 60.53150288, 50.51177416, 
63.65808361, 57.61678228, 58.96028564, 62.59899005, 62.58423368, 
66.35327567, 57.08983322, 60.53150288, 58.43089886, 34.18348528, 
62.63455644, 60.28685074, 54.06962825, 62.12613822, 61.99700403, 
62.50293074, 59.37719068, 59.45052104, 59.74930598, 57.63287456, 
60.92354576, 60.5852099, 60.99525161, 65.40556285, 58.33443869, 
62.41158325, 53.41787897, 57.28415856, 59.74527493, 58.36149627, 
55.78946078, 56.20508168, 55.85256231, 57.33540972, 56.90597041, 
61.35059282, 61.34717692, 60.58567014, 57.54337661, 61.89032186, 
57.7699734, 67.72911289, 63.08214587, 55.79716547, 60.0151807, 
60.33875188, 57.71839018, 53.83163389, 58.75825978, 54.87406044, 
65.2422822, 59.92583687, 61.89168296, 58.92927617, 61.01631098, 
59.734754, 62.98173285, 50.71720312, 52.06309452, 65.61155799, 
64.33718865, 63.62341777, 65.31314523, 60.06699385, 64.99612658, 
64.58221569, 58.90448932, 61.66338756, 61.28257115, 51.6039886, 
55.44072648, 61.74252575, 57.20870028, 53.54041677, 62.54060992, 
65.90036185, 54.18458231, 67.46313336, 62.52328324, 53.9113167, 
58.21652132, 56.09746448, 59.01130251, 57.19217479, 57.6900524, 
57.84326272, 55.04233635, 62.20795068, 59.14800052, 53.317175, 
63.14649958, 61.04259215, 52.12383647, 59.67704049, 61.80128948, 
54.90850199, 60.29877418, 62.20635116, 58.31436989, 60.04432616, 
59.12419377, 61.60382094, 61.6946264, 57.26146252, 57.96165885, 
56.96329754, 64.56768887, 64.99416383, 47.86544132, 60.87873428, 
65.3155465, 60.51239049, 61.1015887, 56.00616866, 57.83745436, 
56.00457647, 55.96683675, 60.41778159, 52.66561922, 54.1412277, 
61.46876097, 55.92408351, 56.41588558, 61.15229439, 51.51481141, 
65.66687679, 54.19693172, 65.26570129, 61.05009941, 58.49114053, 
60.25787162, 61.56876092, 53.76693735, 51.87389777, 55.40444805, 
59.68989882, 67.13433611, 55.92408351, 61.15229439, 51.51481141, 
59.67099915, 56.67645571, 59.40409213, 62.92400297, 57.65221845, 
57.14963005, 61.1872533, 60.01663443, 58.54532753, 56.77933503, 
50.56453363, 59.28965134, 60.68853456, 64.4256764, 61.72542621, 
61.53538461, 56.15506178, 62.80751598, 58.05548837, 55.37366463, 
67.84291727, 53.60266231, 53.22523112, 59.17567921, 62.58454346, 
62.39989884, 56.85987252, 57.21369303, 54.70829033, 61.59026336, 
58.29320191, 58.82589557, 58.98917238, 60.4016918, 58.54553321, 
59.76403559, 54.43350467, 56.56649242, 61.35300663, 63.21610319, 
67.47131818, 56.18144814, 60.22874152, 58.46305779, 61.86545538, 
57.46018951, 56.01432847, 49.86765815, 63.18338872, 57.33573745, 
61.05212817, 57.72412196, 53.10929919, 57.23977488, 57.89202789, 
53.78140075, 55.29625534, 55.51871219, 53.78512181, 56.95930301, 
56.02861249, 64.46570196, 56.25957036, 62.44759897, 59.16714782, 
61.57867069, 66.07911118, 63.95320856, 60.34272523),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L, 6L, 6L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 19L, 
17L, 16L, 15L, 14L, 14L, 14L, 12L, 12L, 12L, 11L, 11L, 11L, 11L, 
11L, 10L, 10L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 8L, 8L, 18L, 
8L, 8L, 8L, 7L, 7L, 7L, 7L, 7L, 7L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 9L, 27L, 13L, 3L, 3L, 21L, 18L, 
15L, 14L, 11L, 11L, 10L, 6L, 5L, 5L, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA), Clutch = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("2", 
"3"), class = "factor")), row.names = c(NA, 500L), class = "data.frame")

Attached below is the code and a picture of a graph of where I'm at right now. Basically, I want to be able to group by Clutch, but I want each clutch to have its own axis. This means that I'll be able to see the time series better. I don't want to use facet_wrap since I want them to be on the same graph.


ggplot(totalallclutches, aes(x= Year, y = totalV, group = Clutch, color = Clutch))  
 stat_summary(fun=mean, geom= "line")  
  theme_classic()

enter image description here

Any suggestions would be helpful!

CodePudding user response:

Since your sample data only has Clutch=2, I'll alter the data so that we have both 2 and 3.

ind <- seq_along(totalallclutches$Clutch) %% 2 == 0
totalallclutches$totalV[ind] <- totalallclutches$totalV[ind]   60
totalallclutches$Clutch[ind] <- "3"

From here, we need to recenter one of them, I'll choose to recenter clutch 3. For this, it helps to know both groups averages:

means <- tapply(totalallclutches$totalV, totalallclutches$Clutch, mean)
means
#        2        3 
# 117.4195 177.1964 

totalallclutches |>
  transform(totalV = ifelse(Clutch == "2", totalV, totalV - diff(means))) |>
  ggplot(aes(x= Year, y = totalV, group = Clutch, color = Clutch))  
  stat_summary(fun=mean, geom = "line")  
  scale_y_continuous(name = "Clutch 2", sec.axis = sec_axis(~ .   diff(means), name = "Clutch 3"))  
  theme_classic()

ggplot2 with second axis

It might be handy here to change the color of the axis ticks/numbers as well. To do this well, you may have to revert to manually controlling colors for each Clutch (scale_colour_manual) and use the same manual colors in your sec_axis call.

  •  Tags:  
  • r
  • Related