[R] Plotting more than one regression line in ggplot

r@i@1290 m@iii@g oii @im@com r@i@1290 m@iii@g oii @im@com
Wed Jun 5 16:21:57 CEST 2019


I am trying to plot, using ggplot, a series of scatter plots with regression lines for several datasets. I started with the following dataset, "onepectCO2MEDIAN". The data for this dataset is as follows:
    onepctCO2MEDIAN
                    x          y
    layer.1   0.000000000  0.0000000
    layer.2   0.006794447  4.9002490
    layer.3   0.014288058  0.1608000
    layer.4   0.022087920  6.6349133
    layer.5   0.030797357 -1.2429506
    layer.6   0.038451072  1.5643374
    layer.7   0.048087904 -2.2659035
    layer.8   0.058677729  2.2070045
    layer.9   0.069261406 -2.3677001
    layer.10  0.080524530 -1.0913506
    layer.11  0.092760246  0.4099940
    layer.12  0.103789609 -0.1259727
    layer.13  0.116953168 -2.4138253
    layer.14  0.129253298  7.0890257
    layer.15  0.141710050 -0.7593539
    layer.16  0.156002052  0.0454416
    layer.17  0.170648172 -1.5349683
    layer.18  0.185318425  6.5524201
    layer.19  0.199463055 -0.8312563
    layer.20  0.213513337 -2.5099183
    layer.21  0.228839271  0.1365968
    layer.22  0.246981293 -1.3719845
    layer.23  0.263012767 -0.8712988
    layer.24  0.278505564  0.6632584
    layer.25  0.293658361  0.7938036
    layer.26  0.310747266  3.4880637
    layer.27  0.325990349 -4.4612208
    layer.28  0.342517540  0.0871734
    layer.29  0.362751633 -1.4171578
    layer.30  0.380199537 -0.9956508
    layer.31  0.394992948  0.3215526
    layer.32  0.414373398  3.1403866
    layer.33  0.430690214 -0.7376099
    layer.34  0.449738145 -2.4860541
    layer.35  0.470167458 -3.4235858
    layer.36  0.489019871  0.4824748
    layer.37  0.507242471 -0.9785386
    layer.38  0.524314284  8.5359684
    layer.39  0.543750525  5.4844742
    layer.40  0.564234197  3.2149367
    layer.41  0.583679616  3.9168916 
    layer.42  0.601459444  4.4907020
    layer.43  0.619924664  6.5410410
    layer.44  0.639932007  4.8068650
    layer.45  0.661347181  8.1510170
    layer.46  0.684117317  0.2697413
    layer.47  0.704829752 -0.1807501
    layer.48  0.725045770  9.7181249
    layer.49  0.745165825  1.5406466
    layer.50  0.765016139 -1.6476041
    layer.51  0.783461511  4.8024603
    layer.52  0.806382924  4.0421516
    layer.53  0.829241335  9.3756512
    layer.54  0.849924415  5.3305050
    layer.55  0.871352434  7.5445803
    layer.56  0.893632233  6.4679547
    layer.57  0.916052133  2.8096065
    layer.58  0.938579470  5.3921661
    layer.59  0.959907651  7.2043689
    layer.60  0.981643587  3.3350806
    layer.61  1.004116774  8.8690707
    layer.62  1.028363466  1.7861299
    layer.63  1.054009140  6.2555038
    layer.64  1.072440803  7.6079236
    layer.65  1.094457805  7.6871483
    layer.66  1.123176277  4.7787764
    layer.67  1.149430871 12.7110502
    layer.68  1.170912921 -0.7156284
    layer.69  1.196743071  1.6490899
    layer.70  1.218625903  3.0363024
    layer.71  1.241868377  4.2974769
    layer.72  1.267941594  1.9543778
    layer.73  1.290708780  3.9986964
    layer.74  1.313222289  4.5179472
    layer.75  1.339045882  0.9337905
    layer.76  1.362803459  3.3050770
    layer.77  1.384450197  3.5422970
    layer.78  1.409720302  5.9973660
    layer.79  1.435851157  0.5081869
    layer.80  1.455592215  7.9661630
    layer.81  1.479495347  9.9460496
    layer.82  1.506051958  3.7908372
    layer.83  1.525728464  2.5735847
    layer.84  1.549362063 10.1404974
    layer.85  1.573440671 13.7408304
    layer.86  1.600278735  0.9335771
    layer.87  1.623879492  9.7588742
    layer.88  1.650029302  1.2769395
    layer.89  1.672362328 13.4970906
    layer.90  1.700221121 10.2087502
    layer.91  1.724793375  1.6811275
    layer.92  1.751070559  6.1178992
    layer.93  1.778022110 -0.1567626
    layer.94  1.803022087  3.8237479
    layer.95  1.830668867  4.4331468
    layer.96  1.855736911  5.9790707
    layer.97  1.882615030 11.3104333
    layer.98  1.909218490  8.2142607
    layer.99  1.938130021 15.3209674
    layer.100 1.963727593  5.8178217
    layer.101 1.993271947  9.6004907
    layer.102 2.022548139  3.4063646
    layer.103 2.050679922  4.7375010
    layer.104 2.078064442  3.0133019
    layer.105 2.104113460  5.5659522
    layer.106 2.133597612 12.0346333
    layer.107 2.164026260 -0.4028320
    layer.108 2.194852829 10.5996780
    layer.109 2.224257946  5.4479584
    layer.110 2.252194643  4.7052374
    layer.111 2.277335048 14.0962019
    layer.112 2.304058313  5.7149016
    layer.113 2.330930233  3.7780072
    layer.114 2.357022762  4.4120620
    layer.115 2.386489272  4.1866085
    layer.116 2.417503953  6.9078802
    layer.117 2.448524356  2.7825739
    layer.118 2.478698969  7.6171786
    layer.119 2.510175705 10.2410603
    layer.120 2.539697886  8.1820711
    layer.121 2.567915559  4.8275494
    layer.122 2.597463250 19.1624883
    layer.123 2.627518773 16.0677109
    layer.124 2.658759236 12.5897081
    layer.125 2.692401528  9.2907988
    layer.126 2.721903205  7.4262502
    layer.127 2.753021359  9.3902518
    layer.128 2.786313415 12.6193550
    layer.129 2.819564104 11.1121040
    layer.130 2.850823164 15.7907100
    layer.131 2.880394101 10.7425287
    layer.132 2.911391258  7.7971430
    layer.133 2.942965150  8.8060858
    layer.134 2.974468350 17.5606266
    layer.135 3.008983612 17.3088605
    layer.136 3.040015221 13.4500543
    layer.137 3.072668672 14.6377884
    layer.138 3.105982423  8.0798552dput(onepctCO2MEDIAN)    dput(onepctCO2MEDIAN)
    structure(list(x = c(0, 0.00679444684647024, 0.014288058038801, 
    0.0220879195258021, 0.0307973567396402,0.0384510718286037,0.0480879042297602, 
    0.0586777292191982, 0.0692614056169987, 0.080524530261755,0.0927602462470531, 
    0.103789608925581, 0.116953168064356, 0.129253298044205, 0.141710050404072, 
    0.156002052128315, 0.170648172497749, 0.185318425297737, 0.199463054537773, 
    0.21351333707571, 0.22883927077055, 0.246981292963028, 0.263012766838074, 
    0.278505563735962, 0.29365836083889, 0.310747265815735, 0.325990349054337, 
    0.342517539858818, 0.362751632928848, 0.380199536681175, 0.39499294757843, 
    0.414373397827148, 0.430690214037895, 0.449738144874573, 0.470167458057404, 
    0.489019870758057, 0.507242470979691, 0.524314284324646, 0.543750524520874, 
    0.56423419713974, 0.583679616451263, 0.601459443569183, 0.619924664497375, 
    0.639932006597519, 0.661347180604935, 0.684117317199707, 0.704829752445221, 
    0.725045770406723, 0.745165824890137, 0.765016138553619, 0.783461511135101, 
    0.806382924318314, 0.829241335391998, 0.84992441534996, 0.871352434158325, 
    0.893632233142853, 0.916052132844925, 0.938579469919205, 0.959907650947571, 
    0.981643587350845, 1.00411677360535, 1.02836346626282, 1.05400913953781, 
    1.07244080305099, 1.09445780515671, 1.12317627668381, 1.14943087100983, 
    1.17091292142868, 1.19674307107925, 1.21862590312958, 1.24186837673187, 
    1.26794159412384, 1.2907087802887, 1.31322228908539, 1.33904588222504, 
    1.36280345916748, 1.38445019721985, 1.40972030162811, 1.43585115671158, 
    1.45559221506119, 1.47949534654617, 1.50605195760727, 1.52572846412659, 
    1.5493620634079, 1.5734406709671, 1.60027873516083, 1.62387949228287, 
    1.65002930164337, 1.67236232757568, 1.70022112131119, 1.72479337453842, 
    1.75107055902481, 1.77802211046219, 1.80302208662033, 1.83066886663437, 
    1.85573691129684, 1.88261502981186, 1.90921849012375, 1.93813002109528, 
    1.96372759342194, 1.99327194690704, 2.02254813909531, 2.05067992210388, 
    2.07806444168091, 2.1041134595871, 2.13359761238098, 2.16402626037598, 
    2.19485282897949, 2.2242579460144, 2.25219464302063, 2.27733504772186, 
    2.30405831336975, 2.33093023300171, 2.35702276229858, 2.38648927211761, 
    2.41750395298004, 2.44852435588837, 2.47869896888733, 2.51017570495605, 
    2.53969788551331, 2.567915558815, 2.59746325016022, 2.62751877307892, 
    2.65875923633575, 2.69240152835846, 2.72190320491791, 2.75302135944366, 
    2.78631341457367, 2.8195641040802, 2.85082316398621, 2.88039410114288, 
    2.91139125823975, 2.94296514987946, 2.97446835041046, 3.00898361206055, 
    3.04001522064209, 3.07266867160797, 3.10598242282867), y = c(0, 
    4.90024901723162, 0.160799993152722, 6.63491326258641, -1.24295055804536, 
    1.56433744259162, -2.26590352245208, 2.20700446463354, -2.36770012911069, 
    -1.09135061899174, 0.409993989292701, -0.125972681525582, -2.41382533818026, 
    7.08902570153028, -0.759353880417294, 0.0454415959640926, -1.53496826259972, 
    6.55242014096194, -0.831256280861552, -2.50991825629084, 0.136596820654013, 
    -1.37198445498419, -0.871298832596736, 0.663258363762466, 0.793803634291308, 
    3.48806373666998, -4.46122081238949, 0.0871733966938564, -1.41715777257774, 
    -0.995650815648318, 0.32155262317503, 3.14038657369241, -0.737609879885404, 
    -2.48605406511292, -3.423585843908, 0.482474753780281, -0.978538630093809, 
    8.53596837794201, 5.48447420320695, 3.21493665820644, 3.91689160157513, 
    4.49070195980797, 6.54104103157039, 4.80686500146557, 8.15101701282067, 
    0.26974132191657, -0.180750068063062, 9.71812491230244, 1.54064657400204, 
    -1.64760408795688, 4.80246028991894, 4.04215159914344, 9.37565121768513, 
    5.33050496938428, 7.54458026088508, 6.46795470819342, 2.80960651433971, 
    5.39216613235986, 7.20436888038562, 3.3350806460997, 8.86907069895943, 
    1.78612988613659, 6.25550382050395, 7.60792364896564, 7.68714830528144, 
    4.77877638957615, 12.7110501777314, -0.715628443181046, 1.64908991824022, 
    3.03630240714679, 4.29747688442346, 1.95437780501881, 3.99869636910933, 
    4.51794724689848, 0.933790484492299, 3.30507700050003, 3.5422970157433, 
    5.99736597322524, 0.508186860060022, 7.96616300581067, 9.94604963036295, 
    3.79083717222623, 2.57358468532258, 10.1404974171776, 13.7408303595752, 
    0.933577123801399, 9.75887417074129, 1.27693947132921, 13.4970905965787, 
    10.2087501765735, 1.68112753028756, 6.1178991508927, -0.156762622680077, 
    3.82374791691426, 4.43314678736265, 5.97907067167507, 11.3104332518482, 
    8.21426074201525, 15.320967360602, 5.81782169471483, 9.6004907412354, 
    3.40636455909704, 4.73750103921864, 3.0133019468806, 5.56595224859066, 
    12.0346332527215, -0.40283199827104, 10.5996779538754, 5.44795836991128, 
    4.70523736412729, 14.096201892183, 5.71490161813391, 3.77800720810782, 
    4.41206200639436, 4.18660847858423, 6.90788020044911, 2.78257393345915, 
    7.61717857379431, 10.2410602647684, 8.18207106836167, 4.82754943871433, 
    19.1624882857155, 16.0677109398509, 12.589708067017, 9.29079879799404, 
    7.42625019725314, 9.39025179806185, 12.6193550331438, 11.1121039747257, 
    15.7907099734986, 10.7425286789233, 7.79714300307344, 8.80608578166101, 
    17.5606266346039, 17.3088604929222, 13.4500543478523, 14.6377884248645, 
    8.07985518296064)), class = "data.frame", row.names = c("layer.1", 
    "layer.2", "layer.3", "layer.4", "layer.5", "layer.6", "layer.7", 
    "layer.8", "layer.9", "layer.10", "layer.11", "layer.12", "layer.13", 
    "layer.14", "layer.15", "layer.16", "layer.17", "layer.18", "layer.19", 
    "layer.20", "layer.21", "layer.22", "layer.23", "layer.24", "layer.25", 
    "layer.26", "layer.27", "layer.28", "layer.29", "layer.30", "layer.31", 
    "layer.32", "layer.33", "layer.34", "layer.35", "layer.36", "layer.37", 
    "layer.38", "layer.39", "layer.40", "layer.41", "layer.42", "layer.43", 
    "layer.44", "layer.45", "layer.46", "layer.47", "layer.48", "layer.49", 
    "layer.50", "layer.51", "layer.52", "layer.53", "layer.54", "layer.55", 
    "layer.56", "layer.57", "layer.58", "layer.59", "layer.60", "layer.61", 
    "layer.62", "layer.63", "layer.64", "layer.65", "layer.66", "layer.67", 
    "layer.68", "layer.69", "layer.70", "layer.71", "layer.72", "layer.73", 
    "layer.74", "layer.75", "layer.76", "layer.77", "layer.78", "layer.79", 
    "layer.80", "layer.81", "layer.82", "layer.83", "layer.84", "layer.85", 
    "layer.86", "layer.87", "layer.88", "layer.89", "layer.90", "layer.91", 
    "layer.92", "layer.93", "layer.94", "layer.95", "layer.96", "layer.97", 
    "layer.98", "layer.99", "layer.100", "layer.101", "layer.102", 
    "layer.103", "layer.104", "layer.105", "layer.106", "layer.107", 
    "layer.108", "layer.109", "layer.110", "layer.111", "layer.112", 
    "layer.113", "layer.114", "layer.115", "layer.116", "layer.117", 
    "layer.118", "layer.119", "layer.120", "layer.121", "layer.122", 
    "layer.123", "layer.124", "layer.125", "layer.126", "layer.127", 
    "layer.128", "layer.129", "layer.130", "layer.131", "layer.132", 
    "layer.133", "layer.134", "layer.135", "layer.136", "layer.137", 
    "layer.138"))
I started with the following to generate the first regression line and scatter plot:    lm<-ggplot(onepctCO2MEDIAN) +  
    geom_jitter(aes(RCP1pctCO2cumulativeMedian[1:138], departurea),   
    colour="blue") + geom_smooth(aes(RCP1pctCO2cumulativeMedian[1:138], 
    departurea), method=lm) 
But I receive this error:    Warning message:
    Computation failed in `stat_smooth()`:
    'what' must be a function or character string
A blue scatter plot is successfully generated, but the problem is that the regression line does not appear, presumably related to the above warning.
Is there a reason for this? I would appreciate any assistance!
	[[alternative HTML version deleted]]



More information about the R-help mailing list