Having different shapes of points for each line and making them shown on the legends

58 views Asked by At

My goal is to make the graph look something like this My Goal. The problem I'm having is that ggplot won't let me use more than 6 kinds of shapes for the points, so my graph looks something like this right now current graph. How should I make it so that each line has it's unique shape of the points, but also making sure that they are shown cleanly and in the correct numeric order? (ie, not like this different shapes but unordered legend, and legend does not show the correct shapes of points, which is produced with the following code)

My data in data frame data_long:

structure(list(power1 = 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, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 
22L, 22L, 22L, 22L, 22L, 22L, 22L), levels = c("0", "0.25", "0.33", 
"Period", "0.35", "0.375", "0.4", "0.425", "0.45", "0.475", "0.5", 
"0.525", "0.55", "0.575", "0.6", "0.7", "0.8", "0.9", "1", "1.1", 
"1.2", "1.3"), class = "factor"), group = c("1", "2", "3", "4", 
"5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23"), value = c(5.13864366306564, 
4.77939817821398, 4.09270853368547, 4.03500151720244, 3.72410744566162, 
3.19737465216476, 3.07771847969777, 3.04646629915112, 3.09333501144575, 
3.07796931479018, 2.77184102938775, 2.69761571716445, 2.69833987013258, 
2.66773835778714, 2.70531213747428, 2.58847957343357, 2.3158764347392, 
2.23121402835477, 2.16775312280514, 2.13027139817901, 2.15173018229461, 
2.0347211721044, 1.95208115543054, 8.13785280093794, 6.96041838646675, 
6.78973741388835, 6.79482783007157, 5.51168003923104, 5.59774993516746, 
5.47433228431448, 4.54796886657372, 4.35881755510305, 4.1217081751311, 
3.78612559323399, 3.20203658023547, 3.17002447500251, 3.20088399232933, 
3.21344327464311, 3.1938649207562, 2.97592722852895, 2.92676801137995, 
2.87757679578107, 2.6243014891993, 2.61014656340088, 2.40438110442984, 
2.36816998897389, 10.8471028290159, 9.56250940326214, 8.52310766717855, 
8.10947146614225, 7.4060839154296, 7.50121965706743, 6.44213757389488, 
5.38135683525506, 5.18741750910549, 5.14012654850439, 4.42463386554498, 
3.49144215236728, 3.52917993107774, 3.54875213198698, 3.31809891604032, 
3.26701697479364, 3.30074955356834, 3.12896354934427, 3.09928118424347, 
2.9478337572486, 2.80904968813277, 2.62410531547891, 2.42655669907775, 
12.8905210074294, 11.4615923959331, 10.4205749531164, 8.37013990030496, 
7.53449207137587, 7.15475172462848, 6.55663296004952, 6.17413890450544, 
5.44402864811583, 5.25741905081155, 4.65530164472199, 4.35332667914473, 
3.78558290554753, 3.62109790828893, 3.62793925573247, 3.64600181220007, 
3.37447580847868, 3.37865241123273, 3.4219426458623, 3.14647237932075, 
2.84336192895752, 2.62663744620319, 2.56043135798194, 11.7995051996129, 
10.749886578179, 9.16788641237401, 8.38108173946591, 8.20393019709473, 
8.25167100160226, 6.66742430744288, 5.64564288700107, 5.58348875895986, 
5.36151701687943, 4.62383257394448, 3.6368739291867, 3.60881131531282, 
3.64529026096197, 3.34587426674957, 3.37960728571196, 3.30552625393981, 
3.21252537769868, 3.11037538335181, 3.04367012603089, 2.86478161966272, 
2.68679986240903, 2.44188890006912, 13.0814100913058, 12.7485824177513, 
10.1359939529249, 9.39161249319762, 9.24723797685855, 9.03798643979528, 
6.97535025689521, 6.08552141033587, 6.11140873800865, 5.66980178677372, 
4.90246206056175, 3.83872789371619, 3.72174109809901, 3.76766227678154, 
3.5008801026785, 3.42853909591354, 3.29763150057679, 3.32459304919249, 
3.13270360558604, 3.16556796341629, 2.93835565237745, 2.77034015332477, 
2.46149530701129, 15.4346929284771, 14.949332971757, 11.3477731591385, 
11.2526231354424, 10.7562379662041, 9.46022583168103, 7.31701680907091, 
6.82143106487618, 6.54912995115038, 6.01978990929582, 5.22055525535581, 
4.06696321221901, 3.86051871063138, 3.88271497149732, 3.68835428182232, 
3.46588752023038, 3.40017938587594, 3.33409054122572, 3.27437721033468, 
3.1800812868836, 3.0166679038114, 2.86024361059806, 2.48161051246762, 
18.4205570667446, 17.0873962395277, 13.8471635765988, 13.1281876559662, 
12.8868025478135, 9.93268505520465, 7.70094772378251, 7.77461891298966, 
7.06295188841488, 6.42277739775086, 5.58906805889066, 4.32860899052608, 
4.01314299605697, 4.00777530874005, 3.90085125181232, 3.50564889166584, 
3.53188648000107, 3.38729600427556, 3.37138325569636, 3.19594324924348, 
3.10125774648195, 2.95828413659186, 2.50311305299687, 22.7183080989336, 
19.9814897195756, 18.5628999193917, 15.8581372782429, 15.5100727558972, 
10.4640129694759, 8.91690959596007, 8.26190488706468, 7.67372446427145, 
6.89108438347427, 6.02037960424028, 4.63112066914527, 4.18148886174137, 
4.14393368788975, 4.14335789865521, 3.62726109041515, 3.59647296817019, 
3.56218579581726, 3.36520948461786, 3.21312765341411, 3.19271157623189, 
3.06540975344927, 2.52600843854534, 29.7243216692731, 27.8276432532366, 
24.514504442988, 21.2379765928566, 18.5534222223628, 11.0615850694332, 
10.6984857467849, 8.76033401364452, 8.40813885910582, 7.43891180869636, 
6.52930328523149, 4.98283578949519, 4.36644623596659, 4.35953159404631, 
4.35156335816902, 3.78420541730513, 3.70803143990967, 3.68988968006999, 
3.35891717146858, 3.24848924542533, 3.27255888494239, 3.18174377208628, 
2.54951002175061, 58.1989217924128, 43.9221100968895, 31.7843690421675, 
31.4901841403192, 23.1409478278656, 13.19482999695, 11.925022990115, 
9.32880000729427, 9.30806145486793, 8.0884637798103, 7.13892440760706, 
5.39686690801061, 4.61009716365765, 4.63592294537131, 4.51345681501507, 
3.95712446816019, 3.92617829398743, 3.73622650533739, 3.3525126595048, 
3.35235577272713, 3.29057280117792, 3.30854463469622, 2.57365038634712, 
78.3994280703312, 40.2938920008789, 36.8732540769446, 28.3405827691679, 
22.6504985586419, 14.746170594877, 11.142586680758, 9.25312478347536, 
9.1532682896371, 8.12528629344052, 7.30138340021254, 5.5737465026873, 
4.75425415800497, 4.6583179825636, 4.50057482704693, 4.00493884897168, 
4.03091130594095, 3.66983156620339, 3.32896761261257, 3.30282910300606, 
3.31705341974837, 3.26891362700399, 2.55286960341576, 37.6712382155759, 
35.7457014791361, 34.1464036664781, 24.0316961589654, 20.9065115341602, 
15.990014190891, 10.1820818794844, 9.12503690998079, 8.66516577218275, 
8.0037684219851, 7.34092525148487, 5.6875568580998, 4.85501622121448, 
4.63424197796023, 4.44583025689822, 4.0514754748331, 4.07572951983906, 
3.58097800691833, 3.32851342163202, 3.30557023732398, 3.23705236466597, 
3.18956636409415, 2.52215138395413, 39.9308800523089, 28.7989613401363, 
23.4646702882904, 20.8728311494796, 19.4181576551116, 17.4551966969221, 
9.37740693193392, 9.00089994659873, 8.2283334585154, 7.88626899591346, 
7.38060453871976, 5.80545964443865, 4.95956577542994, 4.61046061210207, 
4.39258876368832, 4.12896525558214, 4.0916500253485, 3.49666608246319, 
3.32801475643795, 3.33701011486042, 3.13564017955291, 3.11428417353218, 
2.50712397247042, 40.8483318402732, 24.660025933412, 18.0000422681451, 
18.1652837034404, 17.8554687886568, 17.9361361197718, 8.68294638919202, 
8.76811470531463, 7.78803322738736, 7.72705134947085, 7.37971301100791, 
5.90261769674456, 5.05040797778261, 4.57287591581792, 4.32847041037438, 
4.19769387469256, 4.09697614738224, 3.40959864173929, 3.3210203429889, 
3.36232916664546, 3.03567795300488, 3.03773301239625, 2.49387168687705, 
30.1462909933748, 20.5409147335714, 13.7354445126915, 11.7547884563709, 
10.8993991443694, 7.99349308078362, 7.48047503801594, 6.62734982132179, 
6.63338647326677, 6.49204630463644, 6.26914351168065, 6.00127896177237, 
5.21313541556329, 4.28301875960657, 4.28604812533386, 4.02542718178527, 
3.99783089689284, 3.29923301661269, 3.22214332482113, 3.12195063234774, 
2.68867784945751, 2.68651318239183, 2.40915366332848, 24.0722076690155, 
24.3408108118026, 9.5631205774713, 8.91243961640328, 7.81271135699668, 
6.49960908806224, 6.17035953638092, 5.81577592377926, 5.84027839953848, 
5.53832636547457, 5.2321378547673, 5.25878943924986, 5.33139174760173, 
4.37985075533402, 4.09623115580598, 3.85723540219841, 3.78164115790203, 
3.32619260833466, 3.13489365807321, 2.82888324641464, 2.44611370571754, 
2.39637749525597, 2.3347011268872, 22.937865915141, 20.5153151380697, 
7.39977718843242, 7.23099840426992, 6.13966797266852, 5.84308526845936, 
5.8675475823089, 5.85559194097889, 5.34384133619004, 5.33078894588476, 
4.4381055134539, 4.41518443512565, 4.42683128904282, 4.39441049043529, 
3.88633488244428, 3.78059446312856, 3.53488492624045, 3.35575721388362, 
3.05776992378397, 2.59599502838014, 2.30125090400795, 2.28090450050021, 
2.26908823593751, 17.6401043030015, 17.7800179887829, 6.08794933288072, 
6.13151009967121, 5.6977077662796, 5.52762501102557, 5.42126227945987, 
5.36002906494716, 5.42904421949546, 4.98055757657446, 4.52424178992708, 
3.9662769391896, 3.81108128930113, 3.66034007605087, 3.66724329648285, 
3.6449398588576, 3.3475145783768, 3.3783159164983, 2.99386939699227, 
2.41056097995389, 2.3818602852839, 2.18667972592362, 2.15052206320253, 
15.4906034843442, 14.6818152690652, 5.6218244726155, 5.34876806415341, 
5.37408529874299, 5.38280815735708, 5.39176138127794, 5.08166826736183, 
4.66831095851281, 4.63421867612929, 4.68529499485843, 3.55778237195008, 
3.50915116960842, 3.50841061067486, 3.45712011246863, 3.3371532295074, 
3.15954191790475, 3.15757090845151, 2.93447293049159, 2.43289859638956, 
2.28462433302964, 2.13680998992516, 2.13167906131011, 13.9598801329233, 
12.4933936828763, 5.72873636704721, 5.39872661187065, 5.02962030371785, 
4.91888518373503, 4.86428358796829, 4.73000595543503, 4.74506379824897, 
4.31216950703026, 4.2375259073016, 3.40671695241089, 3.32555735676092, 
3.33020192918282, 3.32465234636052, 3.14694469192561, 3.00811262427461, 
2.8411124282808, 2.77964854591103, 2.51648972818177, 2.15093401597979, 
2.10625925849882, 2.11701750718534, 12.7632947318645, 10.9390834787862, 
5.84334969993377, 5.59917915801125, 4.86258697386329, 4.71444863014914, 
4.5258515829203, 4.44023340784953, 4.49021089461522, 4.0601002676063, 
3.80679536473264, 3.36223568778668, 3.26833864188926, 3.25649295550359, 
3.11084446590995, 2.86868707757046, 2.87914793117105, 2.7939746366475, 
2.57222595062283, 2.50336217394579, 2.08351142138273, 2.09604154052357, 
2.09111459705992)), row.names = c(NA, -506L), class = c("tbl_df", 
"tbl", "data.frame"))
density_vs_power <- ggplot(
  data = data_long,
  aes(power1, value, color = factor(group), group = group)
) +
  geom_line() +
  geom_point(shape = factor(data_long$group)) +
  scale_y_continuous(trans = "log10")

Also, is there a way to make the colors of the lines more discrete so that each adjacent line is more discernible?

Currently, the cleanest version looks like this best version, but it returns the warning: The shape palette can deal with a maximum of 6 discrete values because more than 6 becomes difficult to discriminate ℹ you have requested 23 values. Consider specifying shapes manually if you need that many have them.

Any help is much appreciated!

3

There are 3 answers

0
asd-tm On BEST ANSWER

I would consider using some other aes / faceting / etc. to distinguish such a long list of parameters (grouping them somehow and using 6 shapes x 4 ltys or 4 colours x 3 shapes x 2 ltys for instance). Note the multiple comparison statistical limitations when interpreting such a plot.

However here is a solution that bruitforses ggplot to implement your idea:

data_long %>% 
  mutate(fgr = factor(group, levels = (as.integer(.$group)) %>% unique %>%  sort)) %>% 
  
ggplot(
  aes(power1, value, color = fgr, group = group)
) +
  geom_line() +
  geom_point(aes(shape = fgr)) + 
  scale_y_continuous(trans = "log10") +
  scale_color_discrete(name = "group")+
  scale_shape_manual(breaks = 1:23, values = 1:23, name = "group")

enter image description here

0
ReelSaemon On

Like the warning message says, you need to specify the shapes manually. As a minimum working example, you have to append + scale_shape_manual(values=c(1:23)) for your 23 different shapes.

For more discernible colors you could make use of the different color schemes. Similar to specifying the shapes manually, you could append + scale_color_manual(values=) with values being a color palette you find the most fitting one. You could make use of packages like RColorBrewer or pals for that (for a short tutorial and examples look here: http://www.sthda.com/english/wiki/colors-in-r). Although you need to find a palette with a sufficient amount of colors for your problem. You could also specify the values manually (with hexadecimal values, possibly use a color picker tool for that) so that they are more discernible.

For the topic of palettes with many colors see also: R color palettes for many data classes

0
Edward On

For the groups that are wrongly ordered, you need to manage them first:

data_long <- mutate(data_long,
                    group=factor(group, levels=1:23))

Then plot with shapes and colours specified manually.

library(RColorBrewer)

ggplot(data = data_long,
  aes(x=power1, y=value, color=group, group=group)) +
  geom_line() +
  geom_point(aes(shape = group)) +
  scale_y_continuous(trans = "log10") +
  scale_shape_manual(values=1:23) +
  scale_color_manual(values=c(brewer.pal(12, "Paired"), brewer.pal(11, "Set3")))