multiple columns using geom_lines ggplot

Asked

Viewed 252 times

2

I have this data frame:

structure(list(`Exchange Rate` = c(-0.145442175, 0.291096141, 
0.489923112, -2.038363166, 1.180430664, 0.188114666, 0.850922634, 
1.172142766, -3.980837975, 0.285762444, 2.497040646, 0.658010994, 
-0.925171981, 0.37076995, -1.108211119, -0.409242669, -1.234583525, 
-0.385841816, 0.016744771, -0.584406288, 1.17224811, -0.746804388, 
-0.625028046, 0.257871468, -2.735845346, 2.619304857, -0.406825232, 
0.323665151, 2.218951363, -0.821029648), `i_CAN - i_T+1` = c(0.009476137, 
-0.0236354, 0.0094081, 0.11715252, 0.032324021, 0.0461193, 0.050794971, 
0.032372819, 0.202121874, 0.390821859, -0.124492596, -0.127305193, 
-0.22233597, -0.081113713, 0.09952616, 0.22494711, 0.226621495, 
0.411607624, 0.089200478, -0.013454832, -0.013547165, -0.232366214, 
0.03140992, -0.026798837, -0.084556341, -0.091993172, -0.303730207, 
-0.236679148, -0.284235285, -0.355253166), Fitted_OLS = c(-0.036795117836493, 
0.0120555676565338, -0.0366906491623935, -0.22323992930528, -0.0728300398338213, 
-0.0955073599141197, -0.103350601084975, -0.0729090354522075, 
-0.400153521158964, -0.887015257107641, 0.1362666683468, 0.13919994231771, 
0.221388292373518, 0.087380368104602, -0.189831042487278, -0.452154909992189, 
-0.456044210600938, -0.948567833126862, -0.170785020294756, -0.00253939338337472, 
-0.00240533038312774, 0.228145471304061, -0.0713518661553421, 
0.0165138860659871, 0.0915102566139487, 0.100284493544177, 0.265652059802101, 
0.230938443729295, 0.257246215885006, 0.281209408151878), Fitted_Tau_0.05 = c(-2.97674658085357, 
-2.95792547866683, -2.97674412477729, -2.7937460366665, -2.96913739819288, 
-2.95639989365184, -2.95069150171007, -2.96910314906723, -2.3856485268894, 
-0.647452287114872, -2.68293610049662, -2.670570393744, -2.10297963546522, 
-2.84137496711892, -2.84927190111917, -2.23638642750757, -2.22477621905134, 
-0.385841816000001, -2.87715002139054, -2.96747293407547, -2.96740133507642, 
-2.02609643038743, -2.9697648045679, -2.95427875550959, -2.8310157181346, 
-2.80733412921436, -1.38551048535346, -1.99204069101103, -1.57679230211392, 
-0.821029648), Fitted_Tau_0.1 = c(-2.14615029819501, -2.1274826763545, 
-2.14613692884822, -2.038363166, -2.14482079785526, -2.13839956793073, 
-2.1352633011825, -2.14480554064275, -1.77137087834078, -0.604458131512312, 
-1.92044345866761, -1.91142894340333, -1.5035051350835, -2.03720410348948, 
-2.07364942604987, -1.67230210256299, -1.66457879312031, -0.427523081653794, 
-2.09111249534671, -2.1358169999572, -2.13575175544593, -1.44873737433719, 
-2.14509683128765, -2.12442374236989, -2.02946586195686, -2.01185030632841, 
-0.994510606111227, -1.42450007218492, -1.12983335353955, -0.596198212559954
), Fitted_Tau_0.15 = c(-1.71933635014076, -1.64468300363171, 
-1.71919398018537, -1.88775477782576, -1.7645786532068, -1.78941620461232, 
-1.79741089743845, -1.76466980128876, -1.94037609877127, -1.80396551522613, 
-1.35102279399198, -1.34140346983592, -0.970787962667353, -1.48955509746975, 
-1.86796968110935, -1.94244465526037, -1.94239520736829, -1.76758405969913, 
-1.8549638584265, -1.66878107985916, -1.66856708589882, -0.926502246479135, 
-1.76286692476135, -1.63698790713986, -1.47923520568469, -1.45654517211675, 
-0.582926679795556, -0.907156313132261, -0.681742524482543, -0.303821553575753
), Fitted_Tau_0.2 = c(-1.31822170355372, -1.2612698633568, -1.31811270945255, 
-1.44928350811005, -1.35295376640536, -1.37211918880717, -1.37830653315406, 
-1.3530239562106, -1.49424565728752, -1.40966278052636, -1.03953905477796, 
-1.03231429428585, -0.754999493782102, -1.14381078705322, -1.4334978140728, 
-1.49753646228861, -1.49763135643463, -1.38479451698007, -1.42321960777862, 
-1.27961419752, -1.27945114676471, -0.721966101785503, -1.35163580735597, 
-1.25541891323434, -1.13602661180633, -1.11892213009466, -0.466192270983381, 
-0.707541046713255, -0.539674906980139, -0.258917521321538), 
    Fitted_Tau_0.25 = c(-1.09339481162949, -1.05110052573891, 
    -1.09331514534173, -1.18211991727581, -1.11846216978887, 
    -1.13196984549597, -1.13626995170423, -1.11851211064678, 
    -1.19942718266663, -1.07158047530742, -0.87876580399353, 
    -0.873021040504597, -0.648980045982374, -0.960915759565046, 
    -1.17270671135338, -1.19615393564632, -1.19578173174037, 
    -1.04347715538338, -1.1662630186065, -1.06485614745889, -1.06473438705463, 
    -0.621939972338152, -1.11752382999568, -1.04669027747912, 
    -0.954838882721072, -0.941450979021475, -0.410849637100717, 
    -0.610113745806298, -0.471770384878017, -0.238060762840202
    ), Fitted_Tau_0.3 = c(-0.856313887340608, -0.806646313685092, 
    -0.856213949181322, -1.00354880403255, -0.889381479589737, 
    -0.908871103666313, -0.915395418356029, -0.8894510521589, 
    -1.10431465051829, -1.27944844557565, -0.642631449020096, 
    -0.637782904178151, -0.46520344327348, -0.715523344546948, 
    -0.980942028103982, -1.12906551138706, -1.13084251044205, 
    -1.29463801971958, -0.967426885637462, -0.822137162540082, 
    -0.821997544464316, -0.445995456941508, -0.888077394134122, 
    -0.801793032336597, -0.709868022713106, -0.697575071938967, 
    -0.303861113376514, -0.43767788549624, -0.343641339735199, 
    -0.195279413471425), Fitted_Tau_0.35 = c(-0.614545665342069, 
    -0.558065880924393, -0.614427605848361, -0.811738878205299, 
    -0.65465898397137, -0.67932992265918, -0.687768761632182, 
    -0.65474565386996, -0.981951536153628, -1.40603299487419, 
    -0.398084181817311, -0.393882925756105, -0.260228293446381, 
    -0.46466813560417, -0.778042726509029, -1.02987073594871, 
    -1.03342253129503, -1.45663141569822, -0.758560716145644, 
    -0.57522303861583, -0.57506660089703, -0.24706156769216, 
    -0.653036237713024, -0.55277224983645, -0.459261249939471, 
    -0.447653375368195, -0.15856416870681, -0.241455158578784, 
    -0.181837373028309, -0.100320130529075), Fitted_Tau_0.4 = c(-0.479438374939914, 
    -0.436143954097646, -0.47935590325476, -0.576475230026411, 
    -0.505622561922884, -0.519973273488984, -0.524587935337115, 
    -0.505675260948231, -0.60580447124208, -0.521891105849949, 
    -0.265272270406803, -0.25966561974868, -0.0433942987857298, 
    -0.34596083504526, -0.565171677020277, -0.606581828394693, 
    -0.606520454451007, -0.500080373099197, -0.557716927497197, 
    -0.450129167743093, -0.450005014447828, -0.0175259875419499, 
    -0.504632848548831, -0.431676452446974, -0.339954052271896, 
    -0.326744569380808, 0.183288817589302, -0.00622425586947906, 
    0.12551257167753, 0.346545934018588), Fitted_Tau_0.45 = c(-0.249472084798654, 
    -0.208831341163206, -0.249392971491474, -0.352001167948825, 
    -0.275016191953433, -0.28945133050485, -0.294175059127898, 
    -0.275068564667666, -0.400912881741685, -0.408594609398056, 
    -0.0586294915515659, -0.0538708806456925, 0.125085624494199, 
    -0.128105253859496, -0.338319951212337, -0.409242669, -0.409773530726585, 
    -0.400929549369045, -0.329741210091177, -0.221783177595283, 
    -0.221667529600591, 0.146033804315185, -0.274033409994674, 
    -0.204724267363426, -0.122860266116657, -0.111371796435336, 
    0.306431474173815, 0.155162288845756, 0.260638178370155, 
    0.434610381968483), Fitted_Tau_0.5 = c(-0.0589293804847586, 
    -0.00490356049467172, -0.0588184219019292, -0.234262085861125, 
    -0.0961787243437016, -0.118657579789866, -0.126274373701589, 
    -0.0962582544635617, -0.372236441107652, -0.677439106252066, 
    0.159975010586446, 0.164579803649482, 0.320382520728142, 
    0.0890019429762622, -0.205597768591054, -0.409242669, -0.411956363535649, 
    -0.710955829219981, -0.188799183390529, -0.0215199676427287, 
    -0.0213692864069794, 0.33685178879669, -0.0946889180432597, 
    0.000260692992560596, 0.0946312725986971, 0.106793755426848, 
    0.454164868626372, 0.343934908071133, 0.422094054568919, 
    0.539010337917609), Fitted_Tau_0.55 = c(0.150264403850921, 
    0.191313870829747, 0.150349467987768, 0.0119437600494874, 
    0.121531708244201, 0.104022168851425, 0.0980600933118858, 
    0.121469985581222, -0.10242359726447, -0.372866511856322, 
    0.312044286480321, 0.315318172287265, 0.422971870619275, 
    0.260912410446757, 0.0350922979409396, -0.133930022922939, 
    -0.136254303668132, -0.404043989867706, 0.04856095062998, 
    0.178767074552214, 0.178881164697057, 0.433998634288923, 
    0.122687638394564, 0.195199121205693, 0.265014140639825, 
    0.273848995212453, 0.510601799492232, 0.438720352657596, 
    0.489997881300486, 0.563889736871806), Fitted_Tau_0.6 = c(0.240996408041307, 
    0.289143070784699, 0.24109755072136, 0.0695133246797788, 
    0.206516061825866, 0.185199941004965, 0.177890248666098, 
    0.20644132036637, -0.0819103901962612, -0.46899564270693, 
    0.422504562654689, 0.425936754491707, 0.532754890314971, 
    0.367598400900301, 0.099146181657134, -0.125007707190126, 
    -0.128209542488649, -0.515917691292874, 0.116221439518898, 
    0.27456942234523, 0.274702514087874, 0.542992569986604, 0.207915279924101, 
    0.293630053973377, 0.372091108953187, 0.381716748501882, 
    0.610118093084203, 0.547333843695623, 0.59277579301086, 0.652352231616311
    ), Fitted_Tau_0.65 = c(0.489839418597448, 0.527992879314328, 
    0.489923112, 0.330054823015843, 0.460500499698239, 0.441595166532832, 
    0.434984079004537, 0.46043520634883, 0.165404541592783, -0.321905666699846, 
    0.612377335164369, 0.614043661527979, 0.64844060721105, 0.581957135168593, 
    0.359950359490514, 0.115378494839238, 0.111612108614408, 
    -0.385841816, 0.376783510189501, 0.516812060999561, 0.516915658205245, 
    0.649588638383461, 0.461721526534987, 0.531367682588737, 
    0.58469522006527, 0.590419484988943, 0.644073050347688, 0.649936556323412, 
    0.647961881459264, 0.625175441611744), Fitted_Tau_0.7 = c(0.701131750084692, 
    0.741749887208679, 0.701222837483147, 0.517624011815766, 
    0.668767308288848, 0.647511401992231, 0.640014139041589, 
    0.668694395423455, 0.317291963770026, -0.302770741511918, 
    0.819640978667811, 0.820824090465584, 0.829259757162326, 
    0.794597172292875, 0.553047668350595, 0.255131273325021, 
    0.250432222344626, -0.385841816, 0.572820173289519, 0.730053172207601, 
    0.730162412693577, 0.826575717172501, 0.6701301557038, 0.745241255475913, 
    0.797051037602352, 0.802077490276806, 0.787776563499923, 
    0.825211772767144, 0.801805442129363, 0.738288430875664), 
    Fitted_Tau_0.75 = c(0.954941355980596, 0.98904997341344, 
    0.95501537278888, 0.817516404166308, 0.929169933410643, 0.912725594509409, 
    0.907001065410233, 0.929112937916906, 0.680451091404978, 
    0.285762444, 1.06931931040647, 1.07104795079765, 1.11319690892047, 
    1.03915486985594, 0.842787812529983, 0.63932934503629, 0.63624106126005, 
    0.234673083132889, 0.857087382167451, 0.978971023988016, 
    0.979064062587486, 1.11580309033995, 0.930236055756527, 0.992108025141667, 
    1.04178916514467, 1.04733835532856, 1.12418952674708, 1.11681556898551, 
    1.12366658756661, 1.11917382049069), Fitted_Tau_0.8 = c(1.2768560345932, 
    1.30531417870574, 1.27692394578744, 1.12068770385648, 1.25185303998575, 
    1.23463493694692, 1.22843669129479, 1.25179495011444, 0.928751981762999, 
    0.285762444, 1.33528866297297, 1.33490079305126, 1.28277146731658, 
    1.33286112856777, 1.15291376923336, 0.866866038205999, 0.862154117308085, 
    0.196660520001577, 1.17058074318566, 1.29754414716174, 1.29761852367672, 
    1.27284661230694, 1.25293751031056, 1.30755141401993, 1.33363080586885, 
    1.33495390959105, 1.17785375767357, 1.26831936886257, 1.20804744330672, 
    1.0826981142404), Fitted_Tau_0.85 = c(1.80711607765919, 1.83868724587655, 
    1.8071974182933, 1.59340325869935, 1.77596551317471, 1.75345429532218, 
    1.7451918823297, 1.77589079858761, 1.3048536707266, 0.285762444, 
    1.83587741770137, 1.83366318571112, 1.69073971820275, 1.85535001639481, 
    1.64001436863116, 1.20931784527881, 1.20200915562344, 0.141611070647244, 
    1.66520537947909, 1.83069034168612, 1.83076968705223, 1.66793538488457, 
    1.7773586537384, 1.84086292892205, 1.85481173135193, 1.85305625989599, 
    1.46313642683032, 1.65767655005897, 1.52648961517962, 1.26889779361196
    ), Fitted_Tau_0.9 = c(2.28529379415122, 2.31614756892496, 
    2.28538058021639, 2.02725692811573, 2.25070320674272, 2.22455914100304, 
    2.21479963300349, 2.2506177064781, 1.65335112206353, 0.285762444, 
    2.26956716613532, 2.26523489767097, 2.02213104238312, 2.31553942164976, 
    2.0860089151872, 1.52731357667569, 1.51764112510294, 0.0898217973034567, 
    2.11742385875433, 2.30908973496706, 2.30916342771216, 1.98550965921942, 
    2.25229568297375, 2.31790153091248, 2.31332121696138, 2.30768773958533, 
    1.66452670029506, 1.96911920224887, 1.76273070531268, 1.36691957157089
    ), Fitted_Tau_0.95 = c(3.02065990607808, 3.0002881765813, 
    3.02063370363616, 2.98133505995452, 3.02581301016629, 3.02540402448931, 
    3.02466396197783, 3.02581623552021, 2.83630813483043, 2.1545971825556, 
    2.8441395264415, 2.83775442155135, 2.5572644313895, 2.92866324156822, 
    2.99882620992089, 2.78021500247187, 2.77581451135967, 2.04918119460137, 
    3.00706269035658, 3.00817749736773, 3.00811242607593, 2.52032078935125, 
    3.02574647018696, 2.99754272857815, 2.92291274136819, 2.90992695435473, 
    2.21701952361349, 2.5040044862339, 2.30691608054622, 1.9539507502728
    )), .Names = c("Exchange Rate", "i_CAN - i_T+1", "Fitted_OLS", 
"Fitted_Tau_0.05", "Fitted_Tau_0.1", "Fitted_Tau_0.15", "Fitted_Tau_0.2", 
"Fitted_Tau_0.25", "Fitted_Tau_0.3", "Fitted_Tau_0.35", "Fitted_Tau_0.4", 
"Fitted_Tau_0.45", "Fitted_Tau_0.5", "Fitted_Tau_0.55", "Fitted_Tau_0.6", 
"Fitted_Tau_0.65", "Fitted_Tau_0.7", "Fitted_Tau_0.75", "Fitted_Tau_0.8", 
"Fitted_Tau_0.85", "Fitted_Tau_0.9", "Fitted_Tau_0.95"), row.names = c(NA, 
30L), class = "data.frame")

Actually my data frame is much bigger. I did several regressions and created a data frame with its fitted values (adjusted values).

Now on a Y versus X chart you plot these adjusted values. No ggplot can’t.

I do so:

for(i in 1:7)
print(ggplot(data, aes(x=X, y=Y)) + geom_point() +
  geom_line(data=data, aes(x=X, y=data[,i+2], colour="red")))

but only the curve of column three comes out.

Some help?

  • 1

    One suggestion I give you is not to share data the way you shared the above set. It’s hard to read something like this on R. If possible, use the output of the command dput(data). Although it is not friendly to humans, the R can read it very well.

  • Value,@Marcusnunes was behind that command. It’s right now @Marcusnunes?

  • Yes, but this was not the original post’s dataset. Therefore, the answer I put below will no longer achieve the same result.

1 answer

2


The ggplot2 requires the data to be in long format or Narrow, depending on the source. So the trick in your problem was just to convert the data set to long, make a little manipulation in the result and everything was set.

library(reshape2)
library(ggplot2)
names(data)

data.long <- melt(data[, -1], id.vars="X")

ggplot(data, aes(x=X, y=Y)) + geom_point() + 
geom_line(data=data.long, aes(x=X, y=value, colour=variable))

ols

Note that the object data.long was created without the first column (y) of data frame data, because it only contains information about the original points and is not necessary when creating the regression curves.

  • Thank you, @Marcusnunes! A question: in this command ... + geom_line(data=data.long, aes(x=X, y=value, colour=variable)) column X is plotted? Or only column value?

  • 1

    Good turkey. The spine X is not plotted. It only serves as a reference to know where to place each of the values of value. Note that the argument aes(x=X, y=value, colour=variable) tells you where the abscissa values are (X), of wages (value) and how to color each of the generated lines (colour).

  • again, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.