Search code examples
rggplot2categorical-data

how to manually set linecolors and linetype to factor variables using ggplot


This is my sample data:

x <- structure(list(Zeit = c(104, 104.01, 104.02, 104.03, 104.04, 
104.05, 104.06, 104.07, 104.08, 104.09, 104.1, 104.11, 104.12, 
104.13, 104.14, 104.15, 104.16, 104.17, 104.18, 104.19, 104.2, 
104.21, 104.22, 104.23, 104.24, 104.25, 104.26, 104.27, 104.28, 
104.29, 104.3, 104.31, 104.32, 104.33, 104.34, 104.35, 104.36, 
104.37, 104.38, 104.39, 104.4, 104.41, 104.42, 104.43, 104.44, 
104.45, 104.46, 104.47, 104.48, 104.49, 104.5, 104.51, 104.52, 
104.53, 104.54, 104.55, 104.56, 104.57, 104.58, 104.59, 104.6, 
104.61, 104.62, 104.63, 104.64, 104.65, 104.66, 104.67, 104.68, 
104.69, 104.7, 104.71, 104.72, 104.73, 104.74, 104.75, 104.76, 
104.77, 104.78, 104.79, 104.8, 104.81, 104.82, 104.83, 104.84, 
104.85, 104.86, 104.87, 104.88, 104.89, 104.9, 104.91, 104.92, 
104.93, 104.94, 104.95, 104.96, 104.97, 104.98, 104.99, 105, 
105.01, 105.02, 105.03, 105.04, 105.05, 105.06, 105.07, 105.08, 
105.09, 105.1, 105.11, 105.12, 105.13, 105.14, 105.15, 105.16, 
105.17, 105.18, 105.19, 105.2, 105.21, 105.22, 105.23, 105.24, 
105.25, 105.26, 105.27, 105.28, 105.29, 105.3, 105.31, 105.32, 
105.33, 105.34, 105.35, 105.36, 105.37, 105.38, 105.39, 105.4, 
105.41, 105.42, 105.43, 105.44, 105.45, 105.46, 105.47, 105.48, 
105.49, 105.5, 105.51, 105.52, 105.53, 105.54, 105.55, 105.56, 
105.57, 105.58, 105.59, 105.6, 105.61, 105.62, 105.63, 105.64, 
105.65, 105.66, 105.67, 105.68, 105.69, 105.7, 105.71, 105.72, 
105.73, 105.74, 105.75, 105.76, 105.77, 105.78, 105.79, 105.8, 
105.81, 105.82, 105.83, 105.84, 105.85, 105.86, 105.87, 105.88, 
105.89, 105.9, 105.91, 105.92, 105.93, 105.94, 105.95, 105.96, 
105.97, 105.98, 105.99, 106, 106.01, 106.02, 106.03, 106.04, 
106.05, 106.06, 106.07, 106.08, 106.09, 106.1, 106.11, 106.12, 
106.13, 106.14, 106.15, 106.16, 106.17, 106.18, 106.19, 106.2, 
106.21, 106.22, 106.23, 106.24, 106.25, 106.26, 106.27, 106.28, 
106.29, 106.3, 106.31, 106.32, 106.33, 106.34, 106.35, 106.36, 
106.37, 106.38, 106.39, 106.4, 106.41, 106.42, 106.43, 106.44, 
106.45, 106.46, 106.47, 106.48, 106.49, 106.5, 106.51, 106.52, 
106.53, 106.54, 106.55, 106.56, 106.57, 106.58, 106.59, 106.6, 
106.61, 106.62, 106.63, 106.64, 106.65, 106.66, 106.67, 106.68, 
106.69, 106.7, 106.71, 106.72, 106.73, 106.74, 106.75, 106.76, 
106.77, 106.78, 106.79, 106.8, 106.81, 106.82, 106.83, 106.84, 
106.85, 106.86, 106.87, 106.88, 106.89, 106.9, 106.91, 106.92, 
106.93, 106.94, 106.95, 106.96, 106.97, 106.98, 106.99, 107), 
    FSB_Farbe_links = 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, 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, 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, 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, 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, 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, 
    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, 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, 1, 1, 
    1, 1, 1), FSB_Typ_links = c(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, 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, 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, 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, 2, 2, 2, 2, 2, 
    2, 2, 2, 2, 2, 2, 2, 2, 2, 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, 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, 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, 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, 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), Fahrspurabweichung = c(-0.310505085597022, 
    -0.309542196127488, -0.308607585269534, -0.309529936571746, 
    -0.310452287873959, -0.31137463917617, -0.312296990478383, 
    -0.305541752737464, -0.296619779070938, -0.287697805404398, 
    -0.279268412031491, -0.278781032433747, -0.278293652836003, 
    -0.277806273238259, -0.27734375, -0.27734375, -0.27734375, 
    -0.27734375, -0.277307028841091, -0.276817413388985, -0.276327797936878, 
    -0.27583818248477, -0.275471723942599, -0.276415832703928, 
    -0.277359941465258, -0.278304050226587, -0.279248158987916, 
    -0.276909218709719, -0.274391656193606, -0.271874093677493, 
    -0.269594394915214, -0.270504264010528, -0.271414133105843, 
    -0.272324002201155, -0.27323387129647, -0.273848567143439, 
    -0.274378156351682, -0.274907745559925, -0.275263407198508, 
    -0.273821028496787, -0.272378649795066, -0.270936271093347, 
    -0.269582007243196, -0.271541746362726, -0.273501485482253, 
    -0.275461224601782, -0.277324467368697, -0.27683506048286, 
    -0.276345653597023, -0.275856246711185, -0.275342947128182, 
    -0.274361920959113, -0.273380894790045, -0.272399868620975, 
    -0.271129261363634, -0.265813188962985, -0.260497116562329, 
    -0.255181044161673, -0.249941348984442, -0.247632253883712, 
    -0.245323158782982, -0.243014063682255, -0.240704968581525, 
    -0.243957329572663, -0.248633233331343, -0.253309137090024, 
    -0.257757983878592, -0.256280582214447, -0.254803180550301, 
    -0.253325778886158, -0.251711166051994, -0.248298486954593, 
    -0.244885807857196, -0.241473128759795, -0.238344631661443, 
    -0.239324255485894, -0.240303879310346, -0.241283503134797, 
    -0.24192691616877, -0.238551477940446, -0.235176039712127, 
    -0.231800601483803, -0.228475938574099, -0.226995101786797, 
    -0.225514264999493, -0.224033428212189, -0.222560651396139, 
    -0.221194957055284, -0.219829262714432, -0.218463568373577, 
    -0.217097874032723, -0.21515240460553, -0.213043027496151, 
    -0.210933650386773, -0.208837716721903, -0.206905460093489, 
    -0.204973203465074, -0.203040946836663, -0.201026177776153, 
    -0.196570605181605, -0.192115032587063, -0.187659459992515, 
    -0.182792637221818, -0.173637062614099, -0.16448148800638, 
    -0.155325913398674, -0.146670502011139, -0.152096945484571, 
    -0.157523388957995, -0.162949832431427, -0.167823533370045, 
    -0.165889889828726, -0.163956246287403, -0.162022602746084, 
    -0.159963594812924, -0.154427526218816, -0.148891457624715, 
    -0.143355389030607, -0.137819320436499, -0.13671875, -0.13671875, 
    -0.13671875, -0.136388331368362, -0.131514900223315, -0.126641469078268, 
    -0.121768037933228, -0.117069961448919, -0.115114245124791, 
    -0.113158528800661, -0.11120281247653, -0.109087597656251, 
    -0.104693066406248, -0.100298535156245, -0.095904003906248, 
    -0.0916052351095185, -0.0886749615548059, -0.0857446880000976, 
    -0.0828144144453848, -0.0799165604538399, -0.0774760083970585, 
    -0.0750354563402769, -0.072594904283499, -0.0706295692129058, 
    -0.075522612621854, -0.0804156560307954, -0.0853086994397436, 
    -0.0902017428486919, -0.0903344045097887, -0.0881645669872215, 
    -0.0859947294646541, -0.0837002630065289, -0.0798347937076307, 
    -0.0759693244087383, -0.0721038551098403, -0.0683284255948609, 
    -0.0673396267086446, -0.066350827822427, -0.0653620289362091, 
    -0.0641811014836224, -0.0608144738057093, -0.0574478461277956, 
    -0.0540812184498869, -0.0507053688589885, -0.0468729880000489, 
    -0.0430406071411036, -0.0392082262821583, -0.0353758454232185, 
    -0.0309318896264039, -0.0264507605571378, -0.0219696314878779, 
    -0.0175406360967141, -0.015666190932482, -0.0137917457682524, 
    -0.0119173006040199, -0.0100428554397878, -0.00626247747066144, 
    -0.00215128450112778, 0.00195990846840011, 0.00603664571741724, 
    0.00947879557013387, 0.0129209454228458, 0.0163630952755625, 
    0.0196466142805918, 0.0210959142880107, 0.0225452142954319, 
    0.0239945143028528, 0.025643890937711, 0.0325448701669515, 
    0.0394458493961822, 0.0463468286254227, 0.0528784270446119, 
    0.0548146104399012, 0.0567507938351932, 0.0586869772304852, 
    0.0605855254897036, 0.0615665023857366, 0.0625474792817693, 
    0.0635284561778009, 0.0647462074738052, 0.0698521739162818, 
    0.0749581403587509, 0.0800641068012273, 0.0851700732436964, 
    0.088986019218912, 0.0925737786856788, 0.0961615381524406, 
    0.0996283662176822, 0.100115320517216, 0.10060227481675, 
    0.101089229116284, 0.101589879118756, 0.102564224981917, 
    0.103538570845078, 0.104512916708239, 0.105515676372029, 
    0.107985485426153, 0.110455294480273, 0.112925103534396, 
    0.115266227152696, 0.115756260271094, 0.116246293389493, 
    0.116736326507891, 0.117416481094767, 0.120304010663071, 
    0.123191540231376, 0.126079069799677, 0.128935432379541, 
    0.130331718481913, 0.131728004584287, 0.133124290686661, 
    0.134520576789033, 0.132640421681579, 0.130062855497926, 
    0.127485289314277, 0.1250530260181, 0.126534199708033, 0.128015373397963, 
    0.129496547087895, 0.130898017701937, 0.131381656024044, 
    0.131865294346152, 0.13234893266826, 0.1326290307242, 0.12820808431943, 
    0.123787137914665, 0.119366191509894, 0.115297914956724, 
    0.116269473928021, 0.117241032899319, 0.118212591870618, 
    0.119184534170744, 0.120164649589137, 0.121144765007527, 
    0.122124880425919, 0.123101959330116, 0.124030868775564, 
    0.124959778221013, 0.125888687666462, 0.126817597111909, 
    0.128719499483801, 0.130787611512604, 0.132855723541404, 
    0.134580766090116, 0.132164309751811, 0.129747853413503, 
    0.127331397075195, 0.125052047846167, 0.126530679839505, 
    0.128009311832843, 0.129487943826179, 0.130999529467622, 
    0.132932694538269, 0.134865859608913, 0.136799024679559, 
    0.138656476632385, 0.138162939208824, 0.137669401785262, 
    0.137175864361702, 0.136860652119074, 0.138783445060425, 
    0.140706238001773, 0.142629030943124, 0.144536249506591, 
    0.145003493113203, 0.145470736719816, 0.145937980326428, 
    0.14640522393304, 0.147777497774738, 0.149334351584919, 0.150891205395101, 
    0.152311024520469, 0.151822584527471, 0.151334144534473, 
    0.150845704541475, 0.150456748940469, 0.151424888625212, 
    0.152393028309955, 0.153361167994696, 0.154329985514398, 
    0.155318359078489, 0.15630673264258), Querabstand_links = c(1.64228559917915, 
    1.63554537289242, 1.62890625, 1.62890625, 1.62890625, 1.62890625, 
    1.62890625, 1.62317592872497, 1.6158284209996, 1.60848091327421, 
    1.60144864812596, 1.59949912973499, 1.59754961134401, 1.59560009295304, 
    1.59364978936117, 1.59168487487424, 1.58971996038732, 1.5877550459004, 
    1.58579061536437, 1.58383215355594, 1.58187369174751, 1.57991522993908, 
    1.5782060989426, 1.57915020770393, 1.58009431646526, 1.58103842522659, 
    1.58198253398792, 1.57630087490333, 1.57025872486465, 1.56421657482598, 
    1.55916205423693, 1.56735087609476, 1.57553969795258, 1.5837285198104, 
    1.59191734166823, 1.59457213428688, 1.59563131270336, 1.59669049111985, 
    1.59740181439702, 1.59451705699357, 1.59163229959013, 1.58874754218669, 
    1.58603901448639, 1.58995849272545, 1.59387797096451, 1.59779744920356, 
    1.6015625, 1.6015625, 1.6015625, 1.6015625, 1.60146714425636, 
    1.59950509191823, 1.59754303958009, 1.59558098724195, 1.59316890495867, 
    1.58446987739398, 1.57577084982927, 1.56707182226456, 1.55849990837511, 
    1.55480535621394, 1.55111080405277, 1.54741625189161, 1.54372169973044, 
    1.55041465914533, 1.55976646666269, 1.56911827418005, 1.57805231183812, 
    1.5760824429526, 1.57411257406707, 1.57214270518154, 1.56996684435999, 
    1.56509158850656, 1.56021633265314, 1.55534107679971, 1.55090801332289, 
    1.55286726097179, 1.55482650862069, 1.55678575626959, 1.55822148738396, 
    1.55339943277207, 1.54857737816018, 1.54375532354829, 1.53900958476547, 
    1.53703513571573, 1.53506068666599, 1.53308623761625, 1.53125, 
    1.53125, 1.53125, 1.53125, 1.53125, 1.52878329440829, 1.52561922874423, 
    1.52245516308016, 1.51931126258285, 1.51641287764023, 1.51351449269761, 
    1.51061610775499, 1.50765061419573, 1.50269997797956, 1.4977493417634, 
    1.49279870554724, 1.48684767976536, 1.47046401994102, 1.45408036011668, 
    1.43769670029236, 1.42224725402228, 1.43310014096914, 1.44395302791599, 
    1.45480591486285, 1.46462592505507, 1.46172545974309, 1.45882499443111, 
    1.45592452911913, 1.45280390802154, 1.44357712703136, 1.43435034604119, 
    1.42512356505101, 1.41589678406083, 1.4140625, 1.4140625, 
    1.4140625, 1.41353383018938, 1.4057363403573, 1.39793885052523, 
    1.39014136069317, 1.38263619217338, 1.37970261768719, 1.37676904320099, 
    1.3738354687148, 1.3707744140625, 1.3658916015625, 1.36100878906249, 
    1.3561259765625, 1.35124310018253, 1.34635931092468, 1.34147552166683, 
    1.33659173240897, 1.33177274672614, 1.32786786343529, 1.32396298014444, 
    1.3200580968536, 1.31704038842581, 1.32682647524371, 1.33661256206159, 
    1.34639864887949, 1.35618473569738, 1.35718129426468, 1.35392653798083, 
    1.35067178169698, 1.34730111000816, 1.34246927338454, 1.33763743676092, 
    1.3328056001373, 1.32806310118972, 1.32608550341729, 1.32410790564485, 
    1.32213030787242, 1.3199238949766, 1.3151144268653, 1.31030495875399, 
    1.3054954906427, 1.30066742828848, 1.29491885700007, 1.28917028571166, 
    1.28342171442324, 1.27767314313483, 1.2698337760025, 1.2618673243238, 
    1.25390087264512, 1.24603751664507, 1.24322584889872, 1.24041418115238, 
    1.23760251340603, 1.23479084565968, 1.23087185247066, 1.22676065950113, 
    1.2226494665316, 1.21844610734157, 1.21254527902263, 1.20664445070369, 
    1.20074362238475, 1.19515868095921, 1.19322628094932, 1.19129388093942, 
    1.18936148092953, 1.18710201066931, 1.17625761473765, 1.165413218806, 
    1.15456882287434, 1.14431517193308, 1.14141089684015, 1.13850662174721, 
    1.13560234665427, 1.1327738495103, 1.13179287261426, 1.13081189571823, 
    1.1298309188222, 1.12847994821992, 1.12105308793995, 1.11362622766, 
    1.10619936738003, 1.09877250710008, 1.09330122254441, 1.0881758518776, 
    1.0830504812108, 1.07816298243536, 1.07913689103443, 1.0801107996335, 
    1.08108470823257, 1.08200387088124, 1.08102952501808, 1.08005517915492, 
    1.07908083329176, 1.07806868835357, 1.07510491748862, 1.07214114662367, 
    1.06917737575872, 1.06653365861078, 1.06849379108438, 1.07045392355797, 
    1.07241405603156, 1.07391344187364, 1.07006340244924, 1.06621336302483, 
    1.06236332360043, 1.05855484016061, 1.05669312535745, 1.05483141055428, 
    1.05296969575112, 1.05110798094796, 1.05843198194632, 1.06771122020747, 
    1.0769904584686, 1.08590214932127, 1.08491470019464, 1.08392725106802, 
    1.0829398019414, 1.08203125, 1.08203125, 1.08203125, 1.08203125, 
    1.08256127235231, 1.09533289529943, 1.10810451824652, 1.12087614119364, 
    1.13274896004328, 1.13177740107198, 1.13080584210068, 1.12983428312938, 
    1.12890625, 1.12890625, 1.12890625, 1.12890625, 1.12885116566988, 
    1.12792225622444, 1.12699334677899, 1.12606443733354, 1.12513552788809, 
    1.1223504382743, 1.11924827023109, 1.11614610218789, 1.11379885494768, 
    1.12056493269493, 1.12733101044219, 1.13409708818945, 1.14055560287178, 
    1.13858409354733, 1.13661258422288, 1.13464107489843, 1.13260226829857, 
    1.1297025206926, 1.12680277308663, 1.12390302548066, 1.12112454673523, 
    1.12211162158235, 1.12309869642947, 1.1240857712766, 1.12478714682139, 
    1.12190295740936, 1.11901876799734, 1.11613457858531, 1.11328125, 
    1.11328125, 1.11328125, 1.11328125, 1.11328125, 1.11241916815017, 
    1.11138126561005, 1.11034336306993, 1.10944045095906, 1.11041733094506, 
    1.11139421093105, 1.11237109091705, 1.11321512605953, 1.11224698637479, 
    1.11127884669005, 1.1103107070053, 1.10927566845681, 1.10631054776453, 
    1.10334542707226)), row.names = c(NA, -301L), class = c("data.table", 
"data.frame"))

I try to plot one of the two lines with different linetypes (red line). But instead of one red line with 2 different linetypes I get 2 red lines. There is also a chance that the linecolor might also change (not in this example). How can I achieve this? Can I change the color or linetype manually depending on the factor variable?

This is the code I use but that is not working like expected. If I try to set the group to 1 I get an error.

library(data.table)
    library(ggplot2)
    ggplot(x) +
      geom_line(aes(x = Zeit, y = Fahrspurabweichung), colour = "black", size = 1.5) +
      geom_line(aes(x = Zeit, y = Fahrspurabweichung + Querabstand_links, color = factor(FSB_Farbe_links), linetype = factor(FSB_Typ_links)), size = 1.5)

Solution

  • I think what you're looking for is for the red line to alternate between linetypes based on the running value of FSB_Farbe_links. In ggplot2 (among others), the linetype= cannot change within a line (similarly for color=). If you need it to vary within a line, then ... you need multiple lines.1

    Try this:

    x[, grp := rleid(FSB_Typ_links), by = .(FSB_Farbe_links)]
    # add "group=grp" to your geom_line:
    ggplot(x) +
          geom_line(aes(x = Zeit, y = Fahrspurabweichung), colour = "black", size = 1.5) +
          geom_line(aes(x = Zeit, y = Fahrspurabweichung + Querabstand_links,
                        color = factor(FSB_Farbe_links),
                        linetype = factor(FSB_Typ_links),
                        group = grp), size = 1.5)
    

    ggplot2 with alternating line type

    Explanation: data.table::rleid assigns a unique id (integer) based on the "run-length encoding" (rle) useful in grouping operations, assigning the group pairwise either the values. As a demonstration of that function, shown in a matrix solely for visualization (the return value is integer):

    vec <- c(99,99,99,105,105,105,99,102,105)
    cbind(vec, rleid(vec))
    #       vec  
    #  [1,]  99 1
    #  [2,]  99 1
    #  [3,]  99 1
    #  [4,] 105 2
    #  [5,] 105 2
    #  [6,] 105 2
    #  [7,]  99 3
    #  [8,] 102 4
    #  [9,] 105 5
    

    Where the first three 99s are unchanging, so they are group 1. 105 is repeated 3 times as well. The next is 99 again, though since it is not contiguous with the first batch of 99, it gets a new id. Hope that makes sense.

    This method of grouping is sensitive to the order of data. If not already sorted (by Zeit here), then its results will be different (and likely logically incorrect). This should be a "grouped by" operation (by=.(FSB_Farbe_links) here); while it's feasible to also sort by the groups and achieve the same effect, it is also possible that the RLE variable FSB_Typ_links does not change between two different groups, which would appear as one rle-group. Therefore, whenever talking rle-like operations, I tend to urge grouping functions (whether data.table's by=.(), dplyr::group_by, or base R's grouping functions such as aggregate/ave/by.)

    data.table::rleid is the data.table convenience function that mimics somewhat the base::rle function, which does the same basic thing, but with a key difference:

    str(rle(vec))
    # List of 2
    #  $ lengths: int [1:5] 3 3 1 1 1
    #  $ values : num [1:5] 99 105 99 102 105
    #  - attr(*, "class")= chr "rle"
    rleid(vec)
    # [1] 1 1 1 2 2 2 3 4 5
    

    rle does not assign groups, it just tells you what ($values) and how long ($lengths) for each contiguous value. You can see there that 99 has 3, 105 has 3, 99 has 1, etc.

    It is easy to mimic rleid without the data.table package, if you're curious:

    my_rleid <- function(z) {
      r <- rle(z)
      rep.int(seq_along(r$lengths), times = r$lengths)
    }
    my_rleid(vec)
    # [1] 1 1 1 2 2 2 3 4 5
    

    While my base-R variant is simple, data.table::rleid takes half the time with this small vector and is much faster with larger datasets.

    bench::mark(my_rleid(vec), data.table::rleid(vec))
    # # A tibble: 2 x 13
    #   expression                 min  median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time result  memory   time   gc     
    #   <bch:expr>             <bch:t> <bch:t>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm> <list>  <list>   <list> <list> 
    # 1 my_rleid(vec)           16.1us  19.9us    36076.    17.2KB     0    10000     0      277ms <int [~ <Rprofm~ <bch:~ <tibbl~
    # 2 data.table::rleid(vec)   8.3us   9.7us    93380.        0B     9.34  9999     1      107ms <int [~ <Rprofm~ <bch:~ <tibbl~
    vec <- sample(1e4)
    bench::mark(my_rleid(vec), data.table::rleid(vec))
    # # A tibble: 2 x 13
    #   expression                  min   median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time result  memory  time   gc    
    #   <bch:expr>             <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm> <list>  <list>  <list> <list>
    # 1 my_rleid(vec)           239.4us  265.6us     3135.  1016.9KB     47.7  1052    16      336ms <int [~ <Rprof~ <bch:~ <tibb~
    # 2 data.table::rleid(vec)   14.7us   16.3us    51087.    39.1KB     25.6  9995     5      196ms <int [~ <Rprof~ <bch:~ <tibb~
    

    (If you aren't familiar with bench::mark, the \itr/sec`` column is iterations per second, one metric for the relative speed of the functions.)


    Note:

    1. Technically, unfortunately, this method does not produce a perfectly contiguous line: at the transition from solid to dash (and vice versa), there will be a gap as big as the gap in Zeit With linetype this may be masked, since every line type except "solid" has gaps built in to the visual structure. However, if the gap in Zeit is not small, then you may notice a larger gap. To make this more evident, plot just the top (formerly red) line and use color= instead of linetype=:

      ggplot(x) +
        geom_line(aes(x = Zeit, y = Fahrspurabweichung + Querabstand_links,
                      color = factor(FSB_Typ_links),
                      group = grp), size = 1.5)
      

      top line only, with small gaps

      I don't know of an easy way to mitigate this; the only way I know of is to then "copy" the first point of each contiguous group into the previous group. It is not hard (really just a shift operation without grouping and then reassign a couple of values, by-group), but if it isn't a problem, then I suggest sticking with the simpler more-readable approach that allows the minute gaps.