Search code examples
rggplot2plotlinelegend

Add horizontal lines with according color and legend to ggplot using R


I want to add horizontal lines so that the lines have y-intercept of period and colors according to their obj_name, ie the same colors as the line plot for each object. I succeeded in putting in these lines but they do not have the according colors. I also need a legend on the upper left corner of the graph that shows the according horizontal line colors and their according obj_name, but since the colors are still not showing, the legend does not appear. My current graph

My data:

structure(list(0 = 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 ), 0.25 = c(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 ), 0.33 = c(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 ), period = c(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 ), 0.35 = c(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 ), 0.375 = c(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 ), 0.4 = c(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 ), 0.425 = c(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 ), 0.45 = c(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 ), 0.475 = c(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 ), 0.5 = c(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 ), 0.525 = c(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 ), 0.55 = c(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 ), 0.575 = c(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 ), 0.6 = c(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 ), 0.7 = c(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 ), 0.8 = c(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 ), 0.9 = c(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 ), 1 = c(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 ), 1.1 = c(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 ), 1.2 = c(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 ), 1.3 = c(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 ), obj_num = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24), ct = c(58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80)), class = "data.frame", row.names = c(NA, -23L)) dput(graph_data_long) structure(list(obj_name = c("Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_6", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_7", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_8", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_9", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_10", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_11", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12", "Obj_12"), period = c(7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.53449207137587, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 7.15475172462848, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.55663296004952, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 6.17413890450544, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.44402864811583, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 5.25741905081155, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199, 4.65530164472199), power = c("0", "0.25", "0.33", "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", "0", "0.25", "0.33", "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", "0", "0.25", "0.33", "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", "0", "0.25", "0.33", "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", "0", "0.25", "0.33", "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", "0", "0.25", "0.33", "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", "0", "0.25", "0.33", "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"), value = c(3.72410744566162, 5.51168003923104, 7.4060839154296, 8.20393019709473, 9.24723797685855, 10.7562379662041, 12.8868025478135, 15.5100727558972, 18.5534222223628, 23.1409478278656, 22.6504985586419, 20.9065115341602, 19.4181576551116, 17.8554687886568, 10.8993991443694, 7.81271135699668, 6.13966797266852, 5.6977077662796, 5.37408529874299, 5.02962030371785, 4.86258697386329, 3.19737465216476, 5.59774993516746, 7.50121965706743, 8.25167100160226, 9.03798643979528, 9.46022583168103, 9.93268505520465, 10.4640129694759, 11.0615850694332, 13.19482999695, 14.746170594877, 15.990014190891, 17.4551966969221, 17.9361361197718, 7.99349308078362, 6.49960908806224, 5.84308526845936, 5.52762501102557, 5.38280815735708, 4.91888518373503, 4.71444863014914, 3.07771847969777, 5.47433228431448, 6.44213757389488, 6.66742430744288, 6.97535025689521, 7.31701680907091, 7.70094772378251, 8.91690959596007, 10.6984857467849, 11.925022990115, 11.142586680758, 10.1820818794844, 9.37740693193392, 8.68294638919202, 7.48047503801594, 6.17035953638092, 5.8675475823089, 5.42126227945987, 5.39176138127794, 4.86428358796829, 4.5258515829203, 3.04646629915112, 4.54796886657372, 5.38135683525506, 5.64564288700107, 6.08552141033587, 6.82143106487618, 7.77461891298966, 8.26190488706468, 8.76033401364452, 9.32880000729427, 9.25312478347536, 9.12503690998079, 9.00089994659873, 8.76811470531463, 6.62734982132179, 5.81577592377926, 5.85559194097889, 5.36002906494716, 5.08166826736183, 4.73000595543503, 4.44023340784953, 3.09333501144575, 4.35881755510305, 5.18741750910549, 5.58348875895986, 6.11140873800865, 6.54912995115038, 7.06295188841488, 7.67372446427145, 8.40813885910582, 9.30806145486793, 9.1532682896371, 8.66516577218275, 8.2283334585154, 7.78803322738736, 6.63338647326677, 5.84027839953848, 5.34384133619004, 5.42904421949546, 4.66831095851281, 4.74506379824897, 4.49021089461522, 3.07796931479018, 4.1217081751311, 5.14012654850439, 5.36151701687943, 5.66980178677372, 6.01978990929582, 6.42277739775086, 6.89108438347427, 7.43891180869636, 8.0884637798103, 8.12528629344052, 8.0037684219851, 7.88626899591346, 7.72705134947085, 6.49204630463644, 5.53832636547457, 5.33078894588476, 4.98055757657446, 4.63421867612929, 4.31216950703026, 4.0601002676063, 2.77184102938775, 3.78612559323399, 4.42463386554498, 4.62383257394448, 4.90246206056175, 5.22055525535581, 5.58906805889066, 6.02037960424028, 6.52930328523149, 7.13892440760706, 7.30138340021254, 7.34092525148487, 7.38060453871976, 7.37971301100791, 6.26914351168065, 5.2321378547673, 4.4381055134539, 4.52424178992708, 4.68529499485843, 4.2375259073016, 3.80679536473264)), row.names = c(NA, -147L), class = c("tbl_df", "tbl", "data.frame"))

My current code:

graph_data_long %>%
  mutate(obj_name = reorder(obj_name, as.numeric(gsub("^.*?(\\d+)$", "\\1", obj_name)))) %>%
  ggplot(aes(x = as.numeric(power), y = value, group = obj_name, color = obj_name)) +
  geom_line() +
  geom_point(aes(shape = obj_name)) +
  geom_hline(yintercept = period, aes(colour = obj_name)) +
  scale_x_continuous(breaks = as.numeric(unique(graph_data_long$power))) +
  scale_y_continuous(trans = "log10") +
  theme_linedraw() +
  scale_shape_manual(values = 6:12) +
  labs(color = "Object", shape = "Object") +
  theme(axis.text.x = element_text(angle = 90, vjust = .5))

Any help is much appreciated!


Solution

  • Sorry, I am having some issues replicating your dataset. Instead, I used the well-known iris dataset as a demo.

    library(tidyverse)
    
    # Start from the build-in `iris` dataset:
    iris |> 
      
      # Use `Sepal.Length` and `Species` as the example columns:
      select(Sepal.Length, Species) |> 
      
      # Data transformation for ggplot:
      reframe(
        Sepal.Length = Sepal.Length,       # Keep it as it is
        Species      = Species,            # Keep it as it is
        mean         = mean(Sepal.Length), # Calculate the mean for each `Species`, it will act as the horizontal lines
        id           = seq_along(Species), # Dummy column for x-axis
        .by = Species) |>                  # Gruoped by `Species`
      
      # Create ggplot:
      ggplot(aes(x = id, y = Sepal.Length, colour = Species)) +
      
      # 1st layer: basic line of `Sepal.Length` for each `Species`
      geom_line() +
      
      # 2nd layer: The horizontal line per `Species` with the same colour:
      geom_line(aes(x = id, y = mean, colour = Species)) +
      
      # Make the plot simple:
      theme_minimal()
    

    The final output looks like this:

    final output

    The key idea is to use another geom_line() to map and match the horizontal lines to the existing lines. Hope this simple demo is helpful.