Home > OS >  Multiple linear regression lines in R
Multiple linear regression lines in R

Time:06-25

I've made a plot between VO2 and percentage of power (see attached) enter image description here

With this code:

dput %>% 
  filter(percent_power < 100) %>% 
  ggscatter(x = "percent_power", y = "relVO2", color = "group")  
  geom_smooth(aes(colour=group), method = "lm", formula = y ~ x)  
  xlab("Percentage of power (%)")   
  ylab(expression(paste("V", O[2]," (mL/min/kg)")))

At the moment, there is one regression line for each group.

I would like to create 2 regression lines for each group.

  • For the CHD group, the threshold should be at 82% of percent_power. So one linear regression line before 82% and one after.

  • For the "sains" group, the threshold should be at 60% of percent power. So one linear regression line before 60% and one after.

Thank you for your precious help.

Here is the console output.

structure(list(id = c("AC12-PRD-C1", "AC12-PRD-C1", "AC12-PRD-C1", 
"AC12-PRD-C1", "AC12-PRD-C1", "AC12-PRD-C1", "AC12-PRD-C1", "AL13-PRD-C1", 
"AL13-PRD-C1", "AL13-PRD-C1", "AL13-PRD-C1", "AL13-PRD-C1", "AL13-PRD-C1", 
"AL13-PRD-C1", "AL13-PRD-C1", "BM06-PRD-S1", "BM06-PRD-S1", "BM06-PRD-S1", 
"BM06-PRD-S1", "BM06-PRD-S1", "BM06-PRD-S1", "CB19-PRD-S1", "CB19-PRD-S1", 
"CB19-PRD-S1", "CB19-PRD-S1", "CB19-PRD-S1", "CB19-PRD-S1", "CB19-PRD-S1", 
"CB19-PRD-S1", "CB19-PRD-S1", "CB19-PRD-S1", "CB19-PRD-S1", "CB19-PRD-S1", 
"CC14-PRD-S1", "CC14-PRD-S1", "CC14-PRD-S1", "CC14-PRD-S1", "CC14-PRD-S1", 
"CC14-PRD-S1", "CC14-PRD-S1", "CC14-PRD-S1", "DA03-PRD-C1", "DA03-PRD-C1", 
"DA03-PRD-C1", "DA03-PRD-C1", "DA03-PRD-C1", "DA03-PRD-C1", "DA03-PRD-C1", 
"DA03-PRD-C1", "DA03-PRD-C1", "DA03-PRD-C1", "DA03-PRD-C1", "DA03-PRD-C1", 
"DA24-PRD-S1", "DA24-PRD-S1", "DA24-PRD-S1", "DA24-PRD-S1", "DA24-PRD-S1", 
"DA24-PRD-S1", "DA24-PRD-S1", "DB22-PRD-S1", "DB22-PRD-S1", "DB22-PRD-S1", 
"DB22-PRD-S1", "DB22-PRD-S1", "DB42-PRD-C1", "DB42-PRD-C1", "DB42-PRD-C1", 
"DB42-PRD-C1", "DB42-PRD-C1", "DB42-PRD-C1", "DB42-PRD-C1", "DB42-PRD-C1", 
"DB42-PRD-C1", "DB42-PRD-C1", "DB42-PRD-C1", "DL18-PRD-S1", "DL18-PRD-S1", 
"DL18-PRD-S1", "DL18-PRD-S1", "DL18-PRD-S1", "DL18-PRD-S1", "DL18-PRD-S1", 
"DL18-PRD-S1", "DL18-PRD-S1", "DL18-PRD-S1", "DL18-PRD-S1", "DL18-PRD-S1", 
"DL18-PRD-S1", "DR15-PRD-C1", "DR15-PRD-C1", "DR15-PRD-C1", "DR15-PRD-C1", 
"DR15-PRD-C1", "DR15-PRD-C1", "DR15-PRD-C1", "DR15-PRD-C1", "DT08-PRD-S1", 
"DT08-PRD-S1", "DT08-PRD-S1", "DT08-PRD-S1", "DT08-PRD-S1", "DT08-PRD-S1", 
"DT08-PRD-S1", "DT08-PRD-S1", "FB44-PRD-C1", "FB44-PRD-C1", "FB44-PRD-C1", 
"FB44-PRD-C1", "FB44-PRD-C1", "FB44-PRD-C1", "FB44-PRD-C1", "FG33-PRD-C1", 
"FG33-PRD-C1", "FG33-PRD-C1", "FG33-PRD-C1", "FG33-PRD-C1", "FG33-PRD-C1", 
"FG33-PRD-C1", "FG33-PRD-C1", "FG33-PRD-C1", "FG33-PRD-C1", "FG33-PRD-C1", 
"FG33-PRD-C1", "FG40-PRD-C1", "FG40-PRD-C1", "FG40-PRD-C1", "FG40-PRD-C1", 
"FG40-PRD-C1", "GC03-PRD-S1", "GC03-PRD-S1", "GC03-PRD-S1", "GC03-PRD-S1", 
"GC03-PRD-S1", "GC03-PRD-S1", "GC03-PRD-S1", "GC03-PRD-S1", "GG30-PRD-C1", 
"GG30-PRD-C1", "GG30-PRD-C1", "GG30-PRD-C1", "GG30-PRD-C1", "GG30-PRD-C1", 
"GG30-PRD-C1", "GG30-PRD-C1", "GH05-PRD-C1", "GH05-PRD-C1", "GH05-PRD-C1", 
"GH05-PRD-C1", "GH05-PRD-C1", "GH05-PRD-C1", "GH05-PRD-C1", "GH05-PRD-C1", 
"GH05-PRD-C1", "GH05-PRD-C1", "GL05-PRD-S1", "GL05-PRD-S1", "GL05-PRD-S1", 
"GL05-PRD-S1", "GL05-PRD-S1", "GL05-PRD-S1", "GL05-PRD-S1", "GL05-PRD-S1", 
"GL05-PRD-S1", "GL05-PRD-S1", "GL05-PRD-S1", "GL05-PRD-S1", "GP22-PRD-C1", 
"GP22-PRD-C1", "GP22-PRD-C1", "GP22-PRD-C1", "GP22-PRD-C1", "GP22-PRD-C1", 
"GP22-PRD-C1", "GP22-PRD-C1", "GP22-PRD-C1", "GP28-PRD-C1", "GP28-PRD-C1", 
"GP28-PRD-C1", "GP28-PRD-C1", "GP28-PRD-C1", "GP28-PRD-C1", "GP28-PRD-C1", 
"GP28-PRD-C1", "GP28-PRD-C1", "GP28-PRD-C1", "GP28-PRD-C1", "GT04-PRD-S1", 
"GT04-PRD-S1", "GT04-PRD-S1", "GT04-PRD-S1", "GT04-PRD-S1", "GT04-PRD-S1", 
"GT04-PRD-S1", "GT21-PRD-S1", "GT21-PRD-S1", "GT21-PRD-S1", "GT21-PRD-S1", 
"GT21-PRD-S1", "GT21-PRD-S1", "GT21-PRD-S1", "GT21-PRD-S1"), 
    power = c(25L, 40L, 55L, 70L, 85L, 100L, 115L, 25L, 40L, 
    55L, 70L, 85L, 100L, 115L, 130L, 25L, 40L, 55L, 70L, 85L, 
    100L, 25L, 40L, 55L, 70L, 85L, 100L, 115L, 130L, 145L, 160L, 
    175L, 190L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 20L, 
    30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L, 110L, 120L, 130L, 
    25L, 40L, 55L, 70L, 85L, 100L, 115L, 25L, 40L, 55L, 70L, 
    85L, 25L, 40L, 55L, 70L, 85L, 100L, 115L, 130L, 145L, 160L, 
    175L, 25L, 40L, 55L, 70L, 85L, 100L, 115L, 130L, 145L, 160L, 
    175L, 190L, 205L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 
    50L, 60L, 70L, 80L, 90L, 100L, 110L, 120L, 20L, 30L, 40L, 
    50L, 60L, 70L, 80L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 
    100L, 110L, 120L, 130L, 20L, 30L, 40L, 50L, 60L, 25L, 40L, 
    55L, 70L, 85L, 100L, 115L, 130L, 25L, 40L, 55L, 70L, 85L, 
    100L, 115L, 130L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 
    100L, 110L, 40L, 60L, 80L, 100L, 120L, 140L, 160L, 180L, 
    200L, 220L, 240L, 260L, 25L, 40L, 55L, 70L, 85L, 100L, 115L, 
    130L, 145L, 25L, 40L, 55L, 70L, 85L, 100L, 115L, 130L, 145L, 
    160L, 175L, 60L, 80L, 100L, 120L, 140L, 160L, 180L, 25L, 
    40L, 55L, 70L, 85L, 100L, 115L, 130L), VE = c(22.75, 23.75, 
    26.75, 32.5, 39.5, 48.25, 55.75, 24, 26.5, 29, 36.5, 44.75, 
    55.25, 63.75, 78, 19, 20.75, 25, 29, 39.25, 51, 25, 25.5, 
    28.5, 34.25, 35.5, 40.25, 47, 52.5, 59.5, 64.5, 83, 92.25, 
    18.25, 19.75, 20.75, 23.5, 26.25, 28, 32.75, 37.25, 24.25, 
    26.75, 29.5, 31, 32, 39, 43.25, 46.25, 51.75, 52.75, 64.5, 
    72.75, 15.75, 16.25, 20.25, 28, 34.75, 45.75, 64.75, 18.25, 
    21.25, 22.25, 24.75, 29.5, 21.25, 20.75, 26, 30.75, 34, 38.5, 
    46.75, 53.75, 70.5, 82.75, 98.25, 17.25, 22, 25.75, 29.75, 
    33.75, 38.75, 44.5, 50.75, 56.25, 65, 71.75, 81, 99, 30, 
    29, 29.75, 38.25, 40.75, 47.25, 52.75, 65.5, 22.75, 24.75, 
    28.75, 34, 48, 53.75, 61.5, 73.75, 22, 21.75, 24.25, 28, 
    34.5, 40, 48.5, 23.75, 23.75, 25.75, 29, 31.25, 33.5, 35.25, 
    38.5, 40.5, 50, 62.5, 77.25, 19.5, 21.25, 24.25, 30.75, 38.5, 
    17, 17.25, 20.75, 24.25, 28.5, 32.25, 38.75, 54.25, 21, 23.25, 
    28.25, 32.75, 37, 46.5, 56.25, 65.5, 24.5, 25.75, 26.75, 
    29.5, 33.25, 37.75, 40, 45.75, 50, 56.5, 23.75, 26.75, 29.75, 
    38.25, 43.75, 50.5, 58.5, 69.75, 78.25, 92.75, 109, 123.25, 
    22.75, 23.75, 28.5, 32.75, 39.75, 46.5, 56.5, 65.75, 76, 
    26.75, 26.5, 30.5, 31, 40.25, 44.75, 52.5, 60, 65, 70.75, 
    78, 40.5, 43.75, 50.25, 63, 75, 82, 95.25, 25.75, 27.5, 33, 
    37.75, 44, 53.5, 58.75, 69), absVO2 = c(0.739, 0.81975, 0.95125, 
    1.07525, 1.199, 1.34575, 1.49775, 0.66125, 0.7485, 0.855, 
    0.9845, 1.1495, 1.3465, 1.451, 1.5985, 0.61675, 0.717, 0.86275, 
    0.98575, 1.13, 1.262, 0.8835, 0.94575, 1.08125, 1.244, 1.30475, 
    1.4735, 1.679, 1.79075, 1.96, 2.0405, 2.34425, 2.4435, 0.5925, 
    0.661, 0.7435, 0.87875, 0.9435, 0.99675, 1.11425, 1.20275, 
    0.9255, 1.02925, 1.11675, 1.19725, 1.24775, 1.42625, 1.54225, 
    1.59425, 1.69675, 1.776, 1.94525, 2.0395, 0.535, 0.5845, 
    0.76875, 0.982, 1.09975, 1.259, 1.3265, 0.7205, 0.86825, 
    0.9235, 1.01075, 1.17275, 0.7435, 0.76575, 0.96075, 1.0975, 
    1.21975, 1.34525, 1.50625, 1.6755, 1.86325, 2.0465, 2.1395, 
    0.613, 0.85525, 0.98, 1.076, 1.287, 1.4615, 1.59325, 1.6965, 
    1.884, 1.998, 2.1425, 2.31275, 2.474, 0.81075, 0.8035, 0.8645, 
    1.076, 1.119, 1.255, 1.33825, 1.499, 0.8105, 0.8865, 1.03725, 
    1.134, 1.26675, 1.36275, 1.4455, 1.52875, 0.64675, 0.6645, 
    0.742, 0.81675, 0.983, 1.06875, 1.155, 1.02475, 1.0335, 1.09525, 
    1.21675, 1.283, 1.39025, 1.438, 1.575, 1.63075, 1.8005, 1.95575, 
    2.047, 0.708, 0.7245, 0.82675, 0.924, 0.99275, 0.6775, 0.69625, 
    0.806, 0.95825, 1.07425, 1.2515, 1.37325, 1.62675, 0.79325, 
    0.841, 0.97275, 1.04925, 1.16925, 1.41225, 1.53125, 1.65475, 
    0.7105, 0.74875, 0.85225, 0.92375, 1.05575, 1.155, 1.228, 
    1.33625, 1.4605, 1.5715, 1.02625, 1.23, 1.326, 1.57525, 1.7225, 
    1.907, 2.13825, 2.35175, 2.52325, 2.75525, 2.95425, 3.117, 
    0.727, 0.76075, 0.9335, 1.0545, 1.229, 1.37075, 1.44675, 
    1.59225, 1.7365, 0.99625, 0.991, 1.199, 1.20625, 1.4295, 
    1.52425, 1.587, 1.71075, 1.88425, 1.9755, 2.10025, 1.279, 
    1.41725, 1.547, 1.8155, 1.976, 2.1635, 2.3375, 0.80825, 0.87425, 
    1.01375, 1.1475, 1.286, 1.432, 1.56, 1.748), VCO2 = c(0.667, 
    0.715, 0.854, 1.047, 1.25125, 1.5185, 1.76025, 0.5805, 0.66425, 
    0.79425, 0.99925, 1.23475, 1.48375, 1.65775, 1.89075, 0.58225, 
    0.6595, 0.8275, 1.0455, 1.30525, 1.6115, 0.80475, 0.829, 
    0.958, 1.14275, 1.19925, 1.42225, 1.68475, 1.8795, 2.13525, 
    2.2835, 2.7605, 2.9685, 0.54825, 0.60325, 0.66025, 0.77325, 
    0.89375, 0.98525, 1.16275, 1.327, 0.781, 0.8775, 0.972, 1.05425, 
    1.1175, 1.353, 1.5075, 1.61425, 1.76925, 1.8675, 2.17975, 
    2.3575, 0.45075, 0.47775, 0.6545, 0.95525, 1.18325, 1.497, 
    1.725, 0.58275, 0.746, 0.7955, 0.91, 1.13175, 0.6205, 0.60475, 
    0.791, 0.963, 1.10725, 1.30025, 1.56775, 1.804, 2.1645, 2.434, 
    2.67125, 0.542, 0.76325, 0.89, 1.081, 1.2545, 1.4735, 1.68575, 
    1.83225, 2.1025, 2.31325, 2.52175, 2.755, 3.117, 0.72925, 
    0.7275, 0.77375, 1.02925, 1.1305, 1.33425, 1.48125, 1.77475, 
    0.70875, 0.839, 0.9905, 1.206, 1.4965, 1.649, 1.7875, 1.9635, 
    0.6755, 0.67525, 0.751, 0.86925, 1.06025, 1.23525, 1.4025, 
    0.871, 0.888, 0.97825, 1.11275, 1.23175, 1.3465, 1.4595, 
    1.60625, 1.74075, 2.029, 2.37675, 2.64675, 0.6175, 0.64475, 
    0.76425, 0.9405, 1.12525, 0.5545, 0.566, 0.69775, 0.8575, 
    1.014, 1.2, 1.42375, 1.878, 0.61675, 0.6825, 0.82925, 0.98575, 
    1.14725, 1.45, 1.6995, 1.92425, 0.6365, 0.67, 0.7485, 0.81925, 
    0.95025, 1.08175, 1.1795, 1.3525, 1.53, 1.67925, 0.73375, 
    0.90225, 1.0115, 1.3055, 1.498, 1.78, 2.0795, 2.436, 2.701, 
    3.06675, 3.38925, 3.68425, 0.6335, 0.681, 0.8515, 1.0125, 
    1.2365, 1.44175, 1.613, 1.8165, 1.99725, 0.82575, 0.83625, 
    1.03175, 1.06925, 1.3575, 1.549, 1.7125, 1.91125, 2.06925, 
    2.17725, 2.33825, 1.3015, 1.4475, 1.61525, 2.0795, 2.39475, 
    2.6085, 2.9635, 0.73725, 0.812, 0.9935, 1.1605, 1.361, 1.60875, 
    1.78375, 2.08475), percent_absVO2 = c(49.3406776831915, 54.7320981472208, 
    63.5119345685195, 71.791019863128, 80.0534134535136, 89.8514438324153, 
    100, 41.3669064748201, 46.8251485767907, 53.4876446668752, 
    61.588989677823, 71.9111667187989, 84.2352205192368, 90.7725993118549, 
    100, 48.8708399366086, 56.8145800316957, 68.3637083993661, 
    78.1101426307448, 89.540412044374, 100, 36.1571516267649, 
    38.7047268262738, 44.2500511561285, 50.9105790873747, 53.3967669326785, 
    60.3028442807448, 68.7129118068345, 73.2862696951095, 80.2128094945774, 
    83.5072641702476, 95.9382033967669, 100, 49.2621076699231, 
    54.9573893161505, 61.8166701309499, 73.0617335273332, 78.4452296819788, 
    82.8725836624402, 92.6418623986697, 100, 45.3787693062025, 
    50.4658004412846, 54.7560676636431, 58.7031135082128, 61.1792105908311, 
    69.9313557244423, 75.6190242706546, 78.1686687913704, 83.1944103947046, 
    87.0801667075264, 95.3787693062025, 100, 40.3316999623068, 
    44.0633245382586, 57.9532604598568, 74.0294006784772, 82.9061439879382, 
    94.9114210327931, 100, 61.4367938605841, 74.0353869111064, 
    78.7465359198465, 86.1863142187167, 100, 34.7511100724468, 
    35.7910726805328, 44.905351717691, 51.2970320168264, 57.0109838747371, 
    62.8768403832671, 70.4019630754849, 78.3126898808133, 87.0881046973592, 
    95.653189997663, 100, 24.7776879547292, 34.569523039612, 
    39.6119644300728, 43.4923201293452, 52.021018593371, 59.0743734842361, 
    64.3997574777688, 68.57316087308, 76.1519805982215, 80.7599029911075, 
    86.6006467259499, 93.4822150363783, 100, 54.0860573715811, 
    53.6024016010674, 57.6717811874583, 71.7811874583056, 74.6497665110073, 
    83.7224816544363, 89.2761841227485, 100, 53.017170891251, 
    57.988552739166, 67.8495502861815, 74.1782502044154, 82.8618152085037, 
    89.1414554374489, 94.5543744889616, 100, 55.995670995671, 
    57.5324675324675, 64.2424242424242, 70.7142857142857, 85.1082251082251, 
    92.5324675324675, 100, 50.0610649731314, 50.4885197850513, 
    53.505129457743, 59.4406448461163, 62.6770884220811, 67.9164631167562, 
    70.2491450903761, 76.9418661455789, 79.6653639472399, 87.9579872984856, 
    95.5422569614069, 100, 71.3170486023672, 72.979098463863, 
    83.2787710904054, 93.0747922437673, 100, 41.6474565852159, 
    42.8000614722606, 49.5466420777624, 58.9057937605656, 66.0365759950822, 
    76.932534193945, 84.4167819271554, 100, 47.9377549478773, 
    50.8233872186131, 58.7853150022662, 63.4083698443874, 70.6602205771265, 
    85.3452183109231, 92.5366369542227, 100, 45.2115812917595, 
    47.6455615653834, 54.2316258351893, 58.7814190264079, 67.1810372255807, 
    73.4966592427617, 78.1419026407891, 85.0302258988228, 92.9366846961502, 
    100, 32.9242861726019, 39.4610202117421, 42.5409047160731, 
    50.5373756817453, 55.2614693615656, 61.1806223933269, 68.599615014437, 
    75.4491498235483, 80.9512351620148, 88.3942893808149, 94.7786333012512, 
    100, 41.8658220558595, 43.8093866973798, 53.7575583069392, 
    60.7255974661676, 70.7745465015837, 78.9375179959689, 83.3141376331702, 
    91.693060754391, 100, 47.4348291870015, 47.1848589453637, 
    57.0884418521605, 57.4336388525176, 68.0633257945483, 72.5746934888704, 
    75.5624330436853, 81.4545887394358, 89.7155100583264, 94.0602309248899, 
    100, 54.7165775401069, 60.6310160427808, 66.1818181818182, 
    77.668449197861, 84.5347593582888, 92.5561497326203, 100, 
    46.2385583524027, 50.0143020594966, 57.9948512585812, 65.6464530892448, 
    73.5697940503432, 81.9221967963387, 89.2448512585812, 100
    ), percent_power = c(21.7391304347826, 34.7826086956522, 
    47.8260869565217, 60.8695652173913, 73.9130434782609, 86.9565217391304, 
    100, 19.2307692307692, 30.7692307692308, 42.3076923076923, 
    53.8461538461538, 65.3846153846154, 76.9230769230769, 88.4615384615385, 
    100, 25, 40, 55, 70, 85, 100, 13.1578947368421, 21.0526315789474, 
    28.9473684210526, 36.8421052631579, 44.7368421052632, 52.6315789473684, 
    60.5263157894737, 68.4210526315789, 76.3157894736842, 84.2105263157895, 
    92.1052631578947, 100, 22.2222222222222, 33.3333333333333, 
    44.4444444444444, 55.5555555555556, 66.6666666666667, 77.7777777777778, 
    88.8888888888889, 100, 15.3846153846154, 23.0769230769231, 
    30.7692307692308, 38.4615384615385, 46.1538461538462, 53.8461538461538, 
    61.5384615384615, 69.2307692307692, 76.9230769230769, 84.6153846153846, 
    92.3076923076923, 100, 21.7391304347826, 34.7826086956522, 
    47.8260869565217, 60.8695652173913, 73.9130434782609, 86.9565217391304, 
    100, 29.4117647058824, 47.0588235294118, 64.7058823529412, 
    82.3529411764706, 100, 14.2857142857143, 22.8571428571429, 
    31.4285714285714, 40, 48.5714285714286, 57.1428571428571, 
    65.7142857142857, 74.2857142857143, 82.8571428571429, 91.4285714285714, 
    100, 12.1951219512195, 19.5121951219512, 26.8292682926829, 
    34.1463414634146, 41.4634146341463, 48.780487804878, 56.0975609756098, 
    63.4146341463415, 70.7317073170732, 78.0487804878049, 85.3658536585366, 
    92.6829268292683, 100, 22.2222222222222, 33.3333333333333, 
    44.4444444444444, 55.5555555555556, 66.6666666666667, 77.7777777777778, 
    88.8888888888889, 100, 41.6666666666667, 50, 58.3333333333333, 
    66.6666666666667, 75, 83.3333333333333, 91.6666666666667, 
    100, 25, 37.5, 50, 62.5, 75, 87.5, 100, 15.3846153846154, 
    23.0769230769231, 30.7692307692308, 38.4615384615385, 46.1538461538462, 
    53.8461538461538, 61.5384615384615, 69.2307692307692, 76.9230769230769, 
    84.6153846153846, 92.3076923076923, 100, 33.3333333333333, 
    50, 66.6666666666667, 83.3333333333333, 100, 19.2307692307692, 
    30.7692307692308, 42.3076923076923, 53.8461538461538, 65.3846153846154, 
    76.9230769230769, 88.4615384615385, 100, 19.2307692307692, 
    30.7692307692308, 42.3076923076923, 53.8461538461538, 65.3846153846154, 
    76.9230769230769, 88.4615384615385, 100, 18.1818181818182, 
    27.2727272727273, 36.3636363636364, 45.4545454545455, 54.5454545454545, 
    63.6363636363636, 72.7272727272727, 81.8181818181818, 90.9090909090909, 
    100, 15.3846153846154, 23.0769230769231, 30.7692307692308, 
    38.4615384615385, 46.1538461538462, 53.8461538461538, 61.5384615384615, 
    69.2307692307692, 76.9230769230769, 84.6153846153846, 92.3076923076923, 
    100, 17.2413793103448, 27.5862068965517, 37.9310344827586, 
    48.2758620689655, 58.6206896551724, 68.9655172413793, 79.3103448275862, 
    89.6551724137931, 100, 14.2857142857143, 22.8571428571429, 
    31.4285714285714, 40, 48.5714285714286, 57.1428571428571, 
    65.7142857142857, 74.2857142857143, 82.8571428571429, 91.4285714285714, 
    100, 33.3333333333333, 44.4444444444444, 55.5555555555556, 
    66.6666666666667, 77.7777777777778, 88.8888888888889, 100, 
    19.2307692307692, 30.7692307692308, 42.3076923076923, 53.8461538461538, 
    65.3846153846154, 76.9230769230769, 88.4615384615385, 100
    ), relVO2 = c(8.79761904761905, 9.75892857142857, 11.3244047619048, 
    12.8005952380952, 14.2738095238095, 16.0208333333333, 17.8303571428571, 
    7.73391812865497, 8.75438596491228, 10, 11.5146198830409, 
    13.4444444444444, 15.7485380116959, 16.9707602339181, 18.6959064327485, 
    10.0941080196399, 11.7348608837971, 14.120294599018, 16.133387888707, 
    18.494271685761, 20.6546644844517, 12.6214285714286, 13.5107142857143, 
    15.4464285714286, 17.7714285714286, 18.6392857142857, 21.05, 
    23.9857142857143, 25.5821428571429, 28, 29.15, 33.4892857142857, 
    34.9071428571429, 9.63414634146342, 10.7479674796748, 12.0894308943089, 
    14.2886178861789, 15.3414634146341, 16.2073170731707, 18.1178861788618, 
    19.5569105691057, 8.04782608695652, 8.95, 9.71086956521739, 
    10.4108695652174, 10.85, 12.4021739130435, 13.4108695652174, 
    13.8630434782609, 14.754347826087, 15.4434782608696, 16.9152173913043, 
    17.7347826086956, 10.0187265917603, 10.9456928838951, 14.3960674157303, 
    18.3895131086142, 20.5945692883895, 23.5767790262172, 24.8408239700375, 
    10.7859281437126, 12.997754491018, 13.8248502994012, 15.1309880239521, 
    17.5561377245509, 9.56885456885457, 9.85521235521236, 12.3648648648649, 
    14.1248391248391, 15.6981981981982, 17.3133848133848, 19.3854568854569, 
    21.5637065637066, 23.9800514800515, 26.3384813384813, 27.5353925353925, 
    8.01307189542484, 11.1797385620915, 12.8104575163399, 14.0653594771242, 
    16.8235294117647, 19.1045751633987, 20.8267973856209, 22.1764705882353, 
    24.6274509803922, 26.1176470588235, 28.0065359477124, 30.2320261437909, 
    32.3398692810458, 10.1853015075377, 10.0942211055276, 10.8605527638191, 
    13.5175879396985, 14.0577889447236, 15.7663316582915, 16.8121859296482, 
    18.8316582914573, 10.6225425950197, 11.6186107470511, 13.5943643512451, 
    14.8623853211009, 16.6022280471822, 17.8604193971166, 18.9449541284404, 
    20.0360419397117, 9.98070987654321, 10.2546296296296, 11.4506172839506, 
    12.6041666666667, 15.1697530864198, 16.4930555555556, 17.8240740740741, 
    7.9437984496124, 8.01162790697674, 8.49031007751938, 9.43217054263566, 
    9.94573643410853, 10.7771317829457, 11.1472868217054, 12.2093023255814, 
    12.6414728682171, 13.9573643410853, 15.1608527131783, 15.8682170542636, 
    10.0854700854701, 10.3205128205128, 11.7770655270655, 13.1623931623932, 
    14.1417378917379, 10.2651515151515, 10.5492424242424, 12.2121212121212, 
    14.5189393939394, 16.2765151515152, 18.9621212121212, 20.8068181818182, 
    24.6477272727273, 11.109943977591, 11.7787114845938, 13.6239495798319, 
    14.6953781512605, 16.3760504201681, 19.7794117647059, 21.4460784313725, 
    23.1757703081232, 8.58091787439614, 9.04287439613527, 10.2928743961353, 
    11.1564009661836, 12.7506038647343, 13.9492753623188, 14.8309178743961, 
    16.1382850241546, 17.6388888888889, 18.9794685990338, 14.7027220630372, 
    17.621776504298, 18.9971346704871, 22.5680515759312, 24.6776504297994, 
    27.3209169054441, 30.6339541547278, 33.6926934097421, 36.1497134670487, 
    39.4734957020057, 42.3244985673352, 44.6561604584527, 9.34447300771208, 
    9.77827763496144, 11.9987146529563, 13.5539845758355, 15.7969151670951, 
    17.6188946015424, 18.5957583547558, 20.4659383033419, 22.3200514138817, 
    9.3107476635514, 9.26168224299065, 11.2056074766355, 11.2733644859813, 
    13.3598130841121, 14.2453271028037, 14.8317757009346, 15.9883177570093, 
    17.6098130841122, 18.4626168224299, 19.6285046728972, 21.0016420361248, 
    23.2717569786535, 25.4022988505747, 29.8111658456486, 32.4466338259442, 
    35.5254515599343, 38.3825944170772, 10.4560155239327, 11.3098318240621, 
    13.114489003881, 14.8447606727038, 16.6364812419146, 18.5252263906856, 
    20.1811125485123, 22.6131953428202), percent_relVO2 = c(49.3406776831915, 
    54.7320981472208, 63.5119345685195, 71.791019863128, 80.0534134535136, 
    89.8514438324153, 100, 41.3669064748201, 46.8251485767907, 
    53.4876446668752, 61.588989677823, 71.9111667187989, 84.2352205192368, 
    90.7725993118548, 100, 48.8708399366086, 56.8145800316957, 
    68.3637083993661, 78.1101426307448, 89.540412044374, 100, 
    36.1571516267649, 38.7047268262738, 44.2500511561285, 50.9105790873747, 
    53.3967669326785, 60.3028442807448, 68.7129118068345, 73.2862696951095, 
    80.2128094945775, 83.5072641702476, 95.9382033967669, 100, 
    49.2621076699231, 54.9573893161505, 61.8166701309499, 73.0617335273332, 
    78.4452296819788, 82.8725836624402, 92.6418623986697, 100, 
    45.3787693062025, 50.4658004412846, 54.7560676636431, 58.7031135082128, 
    61.1792105908311, 69.9313557244423, 75.6190242706546, 78.1686687913705, 
    83.1944103947046, 87.0801667075264, 95.3787693062025, 100, 
    40.3316999623068, 44.0633245382586, 57.9532604598568, 74.0294006784772, 
    82.9061439879382, 94.9114210327931, 100, 61.4367938605841, 
    74.0353869111064, 78.7465359198465, 86.1863142187167, 100, 
    34.7511100724468, 35.7910726805328, 44.905351717691, 51.2970320168264, 
    57.0109838747371, 62.8768403832671, 70.4019630754849, 78.3126898808133, 
    87.0881046973592, 95.653189997663, 100, 24.7776879547292, 
    34.569523039612, 39.6119644300727, 43.4923201293452, 52.021018593371, 
    59.074373484236, 64.3997574777688, 68.57316087308, 76.1519805982215, 
    80.7599029911075, 86.6006467259499, 93.4822150363783, 100, 
    54.086057371581, 53.6024016010674, 57.6717811874583, 71.7811874583055, 
    74.6497665110073, 83.7224816544363, 89.2761841227485, 100, 
    53.017170891251, 57.988552739166, 67.8495502861815, 74.1782502044154, 
    82.8618152085037, 89.1414554374489, 94.5543744889616, 100, 
    55.995670995671, 57.5324675324675, 64.2424242424242, 70.7142857142857, 
    85.1082251082251, 92.5324675324675, 100, 50.0610649731314, 
    50.4885197850513, 53.505129457743, 59.4406448461163, 62.6770884220811, 
    67.9164631167562, 70.2491450903761, 76.9418661455789, 79.6653639472399, 
    87.9579872984856, 95.5422569614069, 100, 71.3170486023672, 
    72.979098463863, 83.2787710904054, 93.0747922437673, 100, 
    41.6474565852159, 42.8000614722606, 49.5466420777624, 58.9057937605656, 
    66.0365759950822, 76.932534193945, 84.4167819271554, 100, 
    47.9377549478773, 50.8233872186131, 58.7853150022662, 63.4083698443874, 
    70.6602205771264, 85.3452183109231, 92.5366369542227, 100, 
    45.2115812917595, 47.6455615653834, 54.2316258351893, 58.7814190264079, 
    67.1810372255807, 73.4966592427617, 78.1419026407891, 85.0302258988228, 
    92.9366846961502, 100, 32.9242861726019, 39.4610202117421, 
    42.5409047160732, 50.5373756817453, 55.2614693615656, 61.1806223933269, 
    68.599615014437, 75.4491498235483, 80.9512351620148, 88.3942893808149, 
    94.7786333012512, 100, 41.8658220558595, 43.8093866973798, 
    53.7575583069392, 60.7255974661676, 70.7745465015836, 78.9375179959689, 
    83.3141376331702, 91.693060754391, 100, 47.4348291870016, 
    47.1848589453637, 57.0884418521605, 57.4336388525176, 68.0633257945483, 
    72.5746934888704, 75.5624330436853, 81.4545887394358, 89.7155100583264, 
    94.0602309248899, 100, 54.7165775401069, 60.6310160427808, 
    66.1818181818182, 77.668449197861, 84.5347593582888, 92.5561497326203, 
    100, 46.2385583524027, 50.0143020594966, 57.9948512585812, 
    65.6464530892449, 73.5697940503433, 81.9221967963387, 89.2448512585812, 
    100), group = c("CHD", "CHD", "CHD", "CHD", "CHD", "CHD", 
    "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", 
    "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", 
    "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", 
    "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", 
    "Sains", "Sains", "Sains", "Sains", "Sains", "CHD", "CHD", 
    "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", 
    "CHD", "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", 
    "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", "CHD", 
    "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", 
    "CHD", "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", 
    "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", 
    "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "Sains", 
    "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", 
    "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", 
    "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", 
    "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "Sains", "Sains", 
    "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", "CHD", 
    "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", 
    "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "Sains", 
    "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", 
    "Sains", "Sains", "Sains", "Sains", "CHD", "CHD", "CHD", 
    "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", 
    "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "CHD", "Sains", 
    "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", 
    "Sains", "Sains", "Sains", "Sains", "Sains", "Sains", "Sains"
    )), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -202L), groups = structure(list(id = c("AC12-PRD-C1", 
"AL13-PRD-C1", "BM06-PRD-S1", "CB19-PRD-S1", "CC14-PRD-S1", "DA03-PRD-C1", 
"DA24-PRD-S1", "DB22-PRD-S1", "DB42-PRD-C1", "DL18-PRD-S1", "DR15-PRD-C1", 
"DT08-PRD-S1", "FB44-PRD-C1", "FG33-PRD-C1", "FG40-PRD-C1", "GC03-PRD-S1", 
"GG30-PRD-C1", "GH05-PRD-C1", "GL05-PRD-S1", "GP22-PRD-C1", "GP28-PRD-C1", 
"GT04-PRD-S1", "GT21-PRD-S1"), .rows = structure(list(1:7, 8:15, 
    16:21, 22:33, 34:41, 42:53, 54:60, 61:65, 66:76, 77:89, 90:97, 
    98:105, 106:112, 113:124, 125:129, 130:137, 138:145, 146:155, 
    156:167, 168:176, 177:187, 188:194, 195:202), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -23L), .drop = TRUE))

CodePudding user response:

Here is a solution where you define new groups based on your criteria, which is wrapped in a case_when statement.

library(tidyverse)
library(ggpubr)

df %>% 
  filter(percent_power < 100) %>% 
  mutate(power_group = case_when(
    group == "CHD" & percent_power >= 82 ~ "CHD - Power >= 82",
    group == "CHD" & percent_power < 82 ~ "CHD - Power < 82",
    group == "Sains" & percent_power >= 60 ~ "Sains - Power >= 60",
    group == "Sains" & percent_power < 60 ~ "Sains - Power < 60"
  )) %>% 
  ggplot(aes(x = percent_power, y = relVO2, col = power_group))  
  geom_point()  
  geom_smooth(method = "lm", formula = y ~ x)  
  xlab("Percentage of power (%)")   
  ylab(expression(paste("V", O[2]," (mL/min/kg)")))

Created on 2022-06-24 by the reprex package (v2.0.1)

CodePudding user response:

Here is a way with package segmented.
First define a function to fit a segmented linear regression with a breakpoint depending on the group variable and to predict from that fit. Then create an extra variable, y, equal to the output of the function.

suppressPackageStartupMessages({
  library(segmented)
  library(dplyr)
  library(ggplot2)
})

segmfit <- function(x, y, group) {
  psi <- if(group[1] == "CHD") 82 else 60
  fit <- lm(y ~ x)
  sfit <- segmented(fit, seg.Z = ~x, psi = psi)
  predict(sfit)
}

df1 %>%
  filter(percent_power < 100) %>%
  ungroup() %>%
  group_by(group) %>%
  mutate(y = segmfit(percent_power, relVO2, group)) %>%
  ggplot(aes(x = percent_power, group = group))  
  geom_point(aes(y = relVO2, colour = group))  
  geom_line(aes(y = y), colour = 'blue')  
  xlab("Percentage of power (%)")   
  ylab(expression(paste("V", O[2]," (mL/min/kg)")))

Created on 2022-06-24 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related