2

I am trying to learn how to use functions to make my code easier to read and to recall. I am practicing on a function that is expected to subset my data with filter() (according to one of the function argument), keeping only part of the columns of the data I feed to the function with select() and plot the trend of an index over time with ggplot() + geom_line().

I found several related questions, but I could not solve the problem with any of them. I think my issue relates to the way I include the arguments within the function itself. Since I am quite new to writing functions in R (and, to some extent, to R itself), I would like to understand what I am missing.

Below, a sample of my data obtained with dput():

structure(list(denominazione_regione = c("Abruzzo", "Abruzzo", 
"Abruzzo", "Abruzzo", "Abruzzo", "Abruzzo", "Abruzzo", "Abruzzo", 
"Abruzzo", "Abruzzo", "Basilicata", "Basilicata", "Basilicata", 
"Basilicata", "Basilicata", "Basilicata", "Basilicata", "Basilicata", 
"Basilicata", "Basilicata", "Calabria", "Calabria", "Calabria", 
"Calabria", "Calabria", "Calabria", "Calabria", "Calabria", "Calabria", 
"Calabria", "Campania", "Campania", "Campania", "Campania", "Campania", 
"Campania", "Campania", "Campania", "Campania", "Campania", "Emilia-Romagna", 
"Emilia-Romagna", "Emilia-Romagna", "Emilia-Romagna", "Emilia-Romagna", 
"Emilia-Romagna", "Emilia-Romagna", "Emilia-Romagna", "Emilia-Romagna", 
"Emilia-Romagna", "Friuli Venezia Giulia", "Friuli Venezia Giulia", 
"Friuli Venezia Giulia", "Friuli Venezia Giulia", "Friuli Venezia Giulia", 
"Friuli Venezia Giulia", "Friuli Venezia Giulia", "Friuli Venezia Giulia", 
"Friuli Venezia Giulia", "Friuli Venezia Giulia", "Lazio", "Lazio", 
"Lazio", "Lazio", "Lazio", "Lazio", "Lazio", "Lazio", "Lazio", 
"Lazio", "Liguria", "Liguria", "Liguria", "Liguria", "Liguria", 
"Liguria", "Liguria", "Liguria", "Liguria", "Liguria", "Lombardia", 
"Lombardia", "Lombardia", "Lombardia", "Lombardia", "Lombardia", 
"Lombardia", "Lombardia", "Lombardia", "Lombardia", "Marche", 
"Marche", "Marche", "Marche", "Marche", "Marche", "Marche", "Marche", 
"Marche", "Marche", "Molise", "Molise", "Molise", "Molise", "Molise", 
"Molise", "Molise", "Molise", "Molise", "Molise", "P.A. Bolzano", 
"P.A. Bolzano", "P.A. Bolzano", "P.A. Bolzano", "P.A. Bolzano", 
"P.A. Bolzano", "P.A. Bolzano", "P.A. Bolzano", "P.A. Bolzano", 
"P.A. Bolzano", "P.A. Trento", "P.A. Trento", "P.A. Trento", 
"P.A. Trento", "P.A. Trento", "P.A. Trento", "P.A. Trento", "P.A. Trento", 
"P.A. Trento", "P.A. Trento", "Piemonte", "Piemonte", "Piemonte", 
"Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", 
"Piemonte", "Puglia", "Puglia", "Puglia", "Puglia", "Puglia", 
"Puglia", "Puglia", "Puglia", "Puglia", "Puglia", "Sardegna", 
"Sardegna", "Sardegna", "Sardegna", "Sardegna", "Sardegna", "Sardegna", 
"Sardegna", "Sardegna", "Sardegna", "Sicilia", "Sicilia", "Sicilia", 
"Sicilia", "Sicilia", "Sicilia", "Sicilia", "Sicilia", "Sicilia", 
"Sicilia", "Toscana", "Toscana", "Toscana", "Toscana", "Toscana", 
"Toscana", "Toscana", "Toscana", "Toscana", "Toscana", "Umbria", 
"Umbria", "Umbria", "Umbria", "Umbria", "Umbria", "Umbria", "Umbria", 
"Umbria", "Umbria", "Valle d'Aosta", "Valle d'Aosta", "Valle d'Aosta", 
"Valle d'Aosta", "Valle d'Aosta", "Valle d'Aosta", "Valle d'Aosta", 
"Valle d'Aosta", "Valle d'Aosta", "Valle d'Aosta", "Veneto", 
"Veneto", "Veneto", "Veneto", "Veneto", "Veneto", "Veneto", "Veneto", 
"Veneto", "Veneto"), mese = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 4, 4, 4, 
4, 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 4, 4, 4, 4, 4, 4, 
4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 
5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5), giorno = c(13, 14, 15, 17, 18, 
19, 20, 21, 22, 24, 2, 3, 5, 7, 8, 9, 10, 11, 12, 15, 3, 4, 5, 
6, 7, 8, 9, 10, 11, 12, 4, 5, 6, 7, 8, 9, 10, 14, 15, 16, 14, 
15, 16, 17, 18, 19, 20, 21, 22, 23, 8, 9, 10, 11, 12, 13, 14, 
15, 16, 17, 22, 23, 24, 25, 26, 27, 28, 29, 4, 6, 4, 9, 10, 11, 
12, 13, 14, 15, 16, 17, 13, 14, 15, 16, 17, 19, 20, 21, 22, 23, 
30, 31, 1, 2, 3, 4, 5, 6, 7, 8, 27, 1, 2, 3, 5, 6, 7, 19, 16, 
17, 3, 4, 7, 8, 9, 10, 11, 5, 6, 17, 6, 9, 10, 11, 12, 13, 14, 
15, 16, 17, 20, 22, 23, 25, 27, 29, 30, 2, 3, 17, 3, 4, 5, 15, 
16, 17, 20, 21, 22, 23, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 
4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 6, 9, 10, 11, 12, 13, 14, 15, 
16, 17, 23, 24, 25, 26, 27, 28, 29, 31, 1, 2, 7, 8, 9, 10, 29, 
30, 1, 2, 3, 4, 5, 6, 7, 8, 12, 13, 14, 15, 16, 17), indice_covid = c(0.000722977172570487, 
0.000731747419143323, 0.000726619405602403, 0.000705958713917565, 
0.000715143109836991, 0.000715066255966087, 0.000719508897665411, 
0.000712522957044176, 0.000708704722548377, 0.000704215366199546, 
0.000194231162135417, 0.000204103477007972, 0.000195833844109375, 
0.000196535072992117, 0.000195721206888281, 0.000204300911935104, 
0.000204788965105557, 0.000204686490106934, 0.000202443676947922, 
0.00019709033540664, 0.000191329551016342, 0.00019013015559816, 
0.000195445031690215, 0.000195653677128041, 0.000196745165065935, 
0.00019636132340351, 0.000194665566928984, 0.000198970510972297, 
0.000198872792842392, 0.000192711132430227, 0.000247190709193111, 
0.000253340604775297, 0.000257958192196345, 0.00026256767163786, 
0.000259478621064338, 0.000251187148507711, 0.000250203862942052, 
0.000245759340550998, 0.000251713424290707, 0.000250221080333117, 
0.00230641037054345, 0.0023030137614792, 0.00230321555195822, 
0.00230574274068461, 0.0022955317181813, 0.0022916164249754, 
0.00230260915349491, 0.00229883442609974, 0.00229607273678057, 
0.00229384641965863, 0.00102605677984233, 0.00103412261154359, 
0.00103905893583055, 0.00104611267918566, 0.00105462027451819, 
0.00106128028669706, 0.00112457383848192, 0.0010657720577344, 
0.00108146179292638, 0.00109006764207304, 0.000451787709373674, 
0.000451301708327933, 0.000453740362866175, 0.000458679513230127, 
0.000452522184585961, 0.000450469304561494, 0.000452388493645777, 
0.000451646379485777, 0.00044759030746637, 0.000453176465305298, 
0.00223742214827426, 0.00224570252927823, 0.00224596373110458, 
0.00226551056338028, 0.00225429956663055, 0.00226734839808079, 
0.00227926554196977, 0.00227752561522984, 0.0022828723494815, 
0.00229510317675282, 0.00419441077616446, 0.0042509108406737, 
0.00429653658926419, 0.00424793326504034, 0.00419551867716494, 
0.00422845831162317, 0.0042308249300686, 0.00420821551831933, 
0.00421728969042919, 0.0041844763767952, 0.00172290368072297, 
0.00178077393459916, 0.00184618380602529, 0.00188901900711415, 
0.0020071335192238, 0.00180186353769265, 0.00183252911777645, 
0.00184365144948012, 0.0018222985489136, 0.00169241476432713, 
0.000250794294819987, 0.000266834076638407, 0.000265165223138765, 
0.000269318035318716, 0.000276757902865351, 0.000272940706832408, 
0.000274233141481004, 0.000254959475421852, 0.000256973597672904, 
0.000265933897656217, 0.00160390744722108, 0.00161006319915358, 
0.00178213145122727, 0.00166885475302064, 0.0016547681756398, 
0.00161459333782649, 0.00160615142193389, 0.00160337890499983, 
0.00161225818840389, 0.00160400366732056, 0.00258365467992859, 
0.00257701229721788, 0.00259178457506773, 0.00262120887528692, 
0.00263828178998998, 0.00266783466211296, 0.00267901572358427, 
0.00269823185448847, 0.00270825410554096, 0.00272187052992249, 
0.00195370953028712, 0.00196262152333827, 0.00194005431082411, 
0.00194421363849008, 0.00194744414088127, 0.00194429612391499, 
0.00196104010507744, 0.00195441352344111, 0.00194683393834275, 
0.00195880684904024, 0.000330009885697706, 0.00034406841508414, 
0.000337786303630158, 0.000324351196174386, 0.000324488151434096, 
0.000324569103459299, 0.000328843946704101, 0.000337401985032215, 
0.000336483436678545, 0.000329547072227643, 0.000230483882870789, 
0.000230103422133935, 0.000230301910659427, 0.000231314894995154, 
0.000228620918265592, 0.000229279094603471, 0.000234826014536552, 
0.000234719487969866, 0.00023808566892597, 0.000239411792331136, 
0.000223062270757503, 0.000226287613069965, 0.00023061526741283, 
0.000230070197530306, 0.000226959363714129, 0.000226174506604244, 
0.000226413235808541, 0.000223216642122798, 0.000224058198468727, 
0.000224589546052104, 0.000705847037288575, 0.000697185576306138, 
0.000727218324766378, 0.000710302680606525, 0.000713471918610933, 
0.000718929784394798, 0.000733704525448964, 0.000725081910028338, 
0.000726996512532976, 0.000697427167923133, 0.000471752986060328, 
0.000496763433728451, 0.000528989540994201, 0.000549981803030561, 
0.00048950494039217, 0.000527332301604848, 0.000487397674642721, 
0.000460909394964938, 0.000460726234814601, 0.000463354614150553, 
0.00313132661181226, 0.00325238521159263, 0.00321473031687171, 
0.00328710836662263, 0.0031429102541658, 0.00315969283656677, 
0.00312271481546321, 0.00310642552480385, 0.00313066183374978, 
0.00314310505626025, 0.00119011081454931, 0.00120503266098013, 
0.00121702131779706, 0.00123403843449071, 0.0011908796837411, 
0.00120040745199511, 0.0012181362510992, 0.00123036198182824, 
0.00124443699302914, 0.00125054297376155), indice_covid_mobile = c(103.912846159121, 
101.213073787884, 99.2992098903576, 100.491596517356, 101.300982017554, 
99.9892533578459, 100.621290917066, 99.0290682097327, 99.4641247053094, 
100.26158538902, 115.9226525617, 105.082765692187, 103.08766970886, 
101.462864409105, 99.5858926900704, 104.383635878416, 100.238889374419, 
99.9499606833941, 98.9042690810515, 102.34133298302, 107.567889175972, 
99.3731258910028, 102.795388283008, 100.106754024915, 100.557867326552, 
99.8049041447617, 99.1364101417053, 102.211456351129, 99.9508881344138, 
96.901707707676, 103.716272968089, 102.487915343687, 101.822679560248, 
101.786909499663, 98.8235221212675, 96.8045642748461, 99.6085446363396, 
101.58668799582, 102.42272937678, 99.4071257972055, 101.154089070817, 
99.8527317988322, 100.008762017943, 100.109724368796, 99.5571482315378, 
99.8294385054715, 100.479693215658, 99.8360673851472, 99.8798656707148, 
99.9030380402904, 100.114688083845, 100.78609993713, 100.477344198053, 
100.678858831956, 100.81325802677, 100.631508073549, 105.96388650371, 
94.7711943195391, 101.472147358163, 100.795760812166, 100.991174693634, 
99.8924271210442, 100.540360138958, 101.088541105921, 98.6575967605781, 
99.5463470975805, 100.426042144237, 99.835956446633, 100.8372802469, 
101.781618493555, 100.393749520538, 100.492053705944, 100.011631185473, 
100.870309346718, 99.5051448035183, 100.578841944673, 100.52559826708, 
99.9236628331413, 100.234760663762, 100.535764834775, 101.600857776282, 
101.347032217977, 101.07331699724, 98.868778998757, 98.7661155530205, 
101.614006396929, 100.055968825303, 99.4656027577841, 100.215629928419, 
99.2219336103835, 103.98400560808, 103.358879229506, 103.673114826945, 
102.320202406123, 106.252690505751, 89.7729782515648, 101.701881382375, 
100.606938880031, 98.8418146731291, 92.8725298791513, 106.587311983887, 
108.53430496759, 99.3745725730888, 101.566122484236, 111.232519873583, 
98.6207454264458, 100.473522129988, 102.906353573357, 103.411157299569, 
103.486856262455, 104.116548061971, 100.383797203708, 118.44082353111, 
93.6437518046932, 99.1559135176178, 97.5721772750567, 99.4771490941511, 
100.519560604868, 100.553785719418, 100.807425857845, 100.463909136803, 
100.006603916455, 100.573232726355, 101.13529112343, 100.651337436861, 
101.120156013475, 100.419106237358, 100.717283244553, 100.371437726369, 
100.502774992704, 102.942338632398, 101.58152104801, 98.850149545095, 
100.736612990071, 100.625871301234, 100.528946833558, 100.861184721633, 
100.893960560723, 99.6121810964031, 101.284060427294, 106.306911726141, 
104.260032803778, 98.1741679333031, 100.882266971771, 100.042224373249, 
100.024947605897, 102.379257946514, 102.602461871014, 99.7277584618886, 
97.9385718003325, 101.294692780857, 99.8349295698616, 100.086260570856, 
100.439850599948, 98.8353639182557, 100.287889814665, 102.419287263269, 
99.9546359602037, 101.434129302692, 100.556994216052, 105.32505410048, 
101.445938078864, 101.912457462498, 99.763645361108, 98.6478762353532, 
99.6541860635133, 100.105550889833, 98.5881595330206, 101.035932860823, 
100.237147128295, 103.205110035867, 100.125355196867, 104.307712247772, 
97.6739249295887, 100.446181338032, 100.764972753867, 102.055102094094, 
98.8247836667833, 100.264053271521, 95.9326703636007, 112.11597059997, 
105.301598168353, 106.487214049528, 103.968369960001, 89.0038429807777, 
107.727677106256, 92.4270470743795, 101.052848336138, 99.9602611375818, 
100.57048614499, 103.394635772503, 103.866048317148, 98.8422375496386, 
102.251450125414, 102.180770080942, 100.533982234419, 98.829695700936, 
99.4783612458395, 100.780199259645, 100.397463002114, 100.329554145308, 
101.253819917305, 100.994882313579, 101.398259541127, 101.362276408612, 
100.800061365064, 101.476898454323, 101.003642303397, 101.143973189093, 
100.490662103956), indice_covid_fisso = c(8966.958396573, 9075.73421845227, 
9012.13237067192, 8755.88145458328, 8869.79389778578, 8868.84069277578, 
8923.94199444909, 8837.29660467997, 8789.93971545696, 8734.25902022251, 
14274.0922562703, 14999.6109203431, 14391.8737188443, 14443.4071888342, 
14383.5959838623, 15014.1204580173, 15049.9875964539, 15042.4566855114, 
14877.6318366388, 14484.2135498949, 16593.1915765469, 16489.1731546972, 
16950.1095690285, 16968.204493221, 17062.8645619911, 17029.5756204458, 
16882.509932477, 17255.8592706088, 17247.384596198, 16713.0102086266, 
7839.21256089349, 8034.24553302023, 8180.68408417076, 8326.8655052082, 
8228.90177454762, 7965.9525074559, 7934.76935909882, 7793.81925677153, 
7982.64240547845, 7935.31537795503, 1227.93003521849, 1226.12168474402, 
1226.22911774605, 1227.57458990548, 1222.13825412489, 1220.05375685345, 
1225.90627195246, 1223.8966117452, 1222.42629175954, 1221.24100327104, 
8859.45292261082, 8929.0970764655, 8971.71960329855, 9032.62491420386, 
9106.08346134662, 9163.58911358911, 9710.0951679899, 9202.37316026789, 
9337.84565363512, 9412.15257004731, 3476.06853467991, 3472.32922768268, 
3491.09231072248, 3529.09428557033, 3481.71960955858, 3465.92468749571, 
3480.69098735194, 3474.98113817457, 3443.77359527659, 3486.75366554139, 
6238.09607545619, 6261.18238140163, 6261.91063113719, 6316.40862464312, 
6285.15154833305, 6321.53264178107, 6354.75850779916, 6349.90746519359, 
6364.81455010715, 6398.91498826526, 1323.27161169467, 1341.09650663555, 
1355.49072339066, 1340.15712765777, 1323.62113729451, 1334.01308159951, 
1334.75971305066, 1327.62679395391, 1330.48955465938, 1320.13746261722, 
1414.1362338993, 1461.63536213666, 1515.32290733917, 1550.48146589578, 
1647.42827330727, 1478.94542550627, 1504.11532235845, 1513.24438305035, 
1495.71820864616, 1389.11134023281, 771.75264686781, 821.110803449613, 
815.975351279508, 828.75452472172, 851.648735595149, 839.902331458836, 
843.879454868576, 784.569880834311, 790.767805629534, 818.340742381602, 
7780.1187710493, 7809.97864933774, 8644.63493837713, 8095.16048611568, 
8026.83033072523, 7831.95311986324, 7791.00368203037, 7777.55495649506, 
7820.62594516405, 7780.58550858044, 11646.7962800509, 11616.85323896, 
11683.4448434984, 11816.0859557175, 11893.0485471188, 12026.2692456048, 
12076.6720901345, 12163.2960355366, 12208.4751057826, 12269.856265605, 
3745.51871273691, 3762.60417821855, 3719.33985695903, 3727.31383638831, 
3733.50713532563, 3727.47197182132, 3759.57239094578, 3746.86836041536, 
3732.33729662078, 3755.29094472873, 7279.55022764552, 7589.66145531074, 
7451.08698270595, 7154.72755901373, 7157.74859788319, 7159.53428479451, 
7253.83126638358, 7442.60945927894, 7422.34758481137, 7269.34121862073, 
14716.6217521341, 14692.328961306, 14705.0026481401, 14769.6826905103, 
14597.6696367375, 14639.6948408, 14993.871113465, 14987.0692878061, 
15202.0032400773, 15286.6775188485, 13562.5240475235, 13758.6297471818, 
14021.7576885192, 13988.6166137682, 13799.4732041881, 13751.7527026863, 
13766.2678000316, 13571.910060438, 13623.0779614024, 13655.3846995732, 
3709.28719515099, 3663.770468982, 3821.5951582046, 3732.70198593756, 
3749.35660560317, 3778.03816208134, 3855.68070346596, 3810.36811408214, 
3820.42951574437, 3665.04005381276, 3335.56624585953, 3512.40456485421, 
3740.2617672617, 3888.6891916591, 3461.08282215474, 3728.54412703095, 
3446.18323547991, 3258.89579011162, 3257.60074199724, 3276.18490288943, 
32348.8642996309, 33599.4870235032, 33210.484779231, 33958.2022804436, 
32468.5316869857, 32641.9078779708, 32259.8982267784, 32091.6180955748, 
32341.9966623645, 32470.5441332424, 1721.88218494195, 1743.47148672928, 
1760.81697619303, 1785.43776756444, 1722.99460417864, 1736.77961832881, 
1762.4300896669, 1780.11858361459, 1800.48266294519, 1809.31694906057
)), row.names = c(NA, -210L), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), groups = structure(list(denominazione_regione = c("Abruzzo", 
"Basilicata", "Calabria", "Campania", "Emilia-Romagna", "Friuli Venezia Giulia", 
"Lazio", "Liguria", "Lombardia", "Marche", "Molise", "P.A. Bolzano", 
"P.A. Trento", "Piemonte", "Puglia", "Sardegna", "Sicilia", "Toscana", 
"Umbria", "Valle d'Aosta", "Veneto"), .rows = list(1:10, 11:20, 
    21:30, 31:40, 41:50, 51:60, 61:70, 71:80, 81:90, 91:100, 
    101:110, 111:120, 121:130, 131:140, 141:150, 151:160, 161:170, 
    171:180, 181:190, 191:200, 201:210)), row.names = c(NA, -21L
), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))

The function:

The function I am practicing with has 3 arguments: the dataset (which I called data.moving) and 2 additional entries: one to subset the data according to the column denominazione_regione, the second to choose which index to be plotted over time (the data.frame I feed to the function has 3 possible indices)

plot_by_reg <- function(df, reg, dato) {

  #d <- 
    df %>% 
    dplyr::filter(denominazione_regione == reg) %>%
    dplyr::mutate(calendario = format(as.Date(paste(mese,giorno , sep = "-" )  , format = "%m-%d" ), "%m-%d")) %>%
    dplyr::select(c(denominazione_regione, calendario, all_of(dato))) %>%
    ggplot(aes(x=df$calendario, y=df$dato)) + #, group = 1)) +
    geom_line(aes(group = 1)) +
    theme_dark()
}

The Issues:

When I call the function, R returns me the following error>

plot_by_reg(df = data.moving, reg = "Toscana", dato = "indice_covid")
Errore: geom_line requires the following missing aesthetics: x and y
Run `rlang::last_error()` to see where the error occurred.

Conversely, if I change the ggplot() aesthetics to recall the function argument (included within quotes "" in the function call( I get a graph, where the line is horizontal and with no real value on the y axis

plot_by_reg <- function(df, reg, dato) {

  #d <- 
    df %>% 
    dplyr::filter(denominazione_regione == reg) %>%
    dplyr::mutate(calendario = format(as.Date(paste(mese,giorno , sep = "-" )  , format = "%m-%d" ), "%m-%d")) %>%
    dplyr::select(c(denominazione_regione, calendario, all_of(dato))) #%>%
    ggplot(aes(x = calendario, y = dato) + #, group = 1)) +
    geom_line(aes(group = 1)) +
    theme_dark()
}

enter image description here

Finally, if I modify the plot_by_reg removing the ggplot() part, the function works, printing a data.frame with 3 columns


plot_by_reg <- function(df, reg, dato) {

  #d <- 
    df %>% 
    dplyr::filter(denominazione_regione == reg) %>%
    dplyr::mutate(calendario = format(as.Date(paste(mese,giorno , sep = "-" )  , format = "%m-%d" ), "%m-%d")) %>%
    dplyr::select(c(denominazione_regione, calendario, all_of(dato))) #%>%
    #ggplot(aes(x=df$calendario, y=df$dato)) + #, group = 1)) +
    #geom_line(aes(group = 1)) +
    #theme_dark()
}

I think the problem lies in the way I call the function's argument in within it, but as far as I tried to change it, I always find an error. For instance, if I try to pass the argument dato, without "", R throws an error Error in plot_by_reg(df = data.moving, reg = Toscana, dato = indice_covid) : object "indice_covid" not found in the select() line, as it does not find the element df$dato. The same commands outside the function work perfectly.

EDIT

I forgot to mention the feature of the machine I am working with:

Sys.info()
          sysname           release           version          nodename           machine             login 
        "Windows"          "10 x64"     "build 18362" "DESKTOP-7D7TTKL"          "x86-64"         "Filippo" 
             user    effective_user 
        "Filippo"         "Filippo" 

R.version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          6.2                         
year           2019                        
month          12                          
day            12                          
svn rev        77560                       
language       R                           
version.string R version 3.6.2 (2019-12-12)
nickname       Dark and Stormy Night     

library(tidyverse)
-- Attaching packages --------------------------------------- tidyverse 1.3.0 --
v ggplot2 3.3.0     v purrr   0.3.3
v tibble  2.1.3     v dplyr   0.8.4
v tidyr   1.0.2     v stringr 1.4.0
v readr   1.3.1     v forcats 0.4.0
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
Warning messages:
1: package ‘tidyverse’ was built under R version 3.6.3 
2: package ‘ggplot2’ was built under R version 3.6.3 
Filippo Santi
  • 109
  • 1
  • 8

2 Answers2

3

There are two issues with your function. The first error arises because calendario is no column of the df passed to the function. Simply remove the df$ when specifying the aesthetics. Second. Even when removing the df$ you set the y-aesthetic equal the string in variable dato, i.e. "indice_covid" in your example. That is for every date you have the same value "indice_covid". That's why you get a flat line. To tell ggplot2 that you want a the column datoof the df you have to convert it to a symbol using sym and the bang-bang-operator !!, i.e. !!sym(dato). Try this:

library(ggplot2)
library(dplyr)

plot_by_reg <- function(df, reg, dato) {

  df %>% 
    dplyr::filter(denominazione_regione == reg) %>%
    dplyr::mutate(calendario = format(as.Date(paste(mese,giorno , sep = "-" )  , format = "%m-%d" ), "%m-%d")) %>%
    dplyr::select(c(denominazione_regione, calendario, all_of(dato))) %>%
    #ggplot(aes(x=df$calendario, y=df$dato)) +
    ggplot(aes(x = calendario, y = !!sym(dato))) +
    geom_line(aes(group = 1)) +
    theme_dark()
}

plot_by_reg(df = data.moving, reg = "Toscana", dato = "indice_covid")

Created on 2020-05-25 by the reprex package (v0.3.0)

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Hi @stefan, thank you very much. It worked! Sorry if I keep asking, but I have two more curiosities: first, you said that `calendario` is not part of the `df`, but I created it within the function. This menas that a function only consider the original columns as pert of the data? Second, concerning the `sys()` and the bang-bang `!!` operator: Why I don't have to use them, say, for the other argument of the function? Is it because they are required as strings in the command to which they are passed? – Filippo Santi May 26 '20 at 06:08
  • Hi @FilippoSanti. Asking is fine. (: 1. Creating the variable in the pipe does not add the variable to the original `df` (it's only stored in some kind of temporary df in your memory). If you want to add to the original df you have to assign the result to `df`, i.e. `df <- df %>% .....` and then passing the mutated `df` to ggplot. ... – stefan May 26 '20 at 06:38
  • ... 2. You are right. In case of `filter(denominazione_regione == reg)` we don't need the conversion because here `reg` as a string is fine. In case of `dato` however we want R to interpret the string as a variable name or symbol (that's what sym() is doing) and tell ggplot to look for this variable in our dataframe (that's what the !! is doing). For more on this see e.g. https://tidyeval.tidyverse.org/sec-up-to-speed.html or see the links provided by @MagnusNordmo – stefan May 26 '20 at 06:39
  • Thanks @stefan, now many things I did not get about function make sense! Best – Filippo Santi May 26 '20 at 10:07
1

It's great that you provide data with your question. However, i'm having a hard time understanding what your variables mean as I dont speak Italien. I can see a few things going wrong in the code however. For instance, you need a special {{ }} symbol to make functions with dplyr and ggplot2. See the links below. I have som sample code that you can look at or emulate. Good luck!

library(tidyverse)

Plot <- function(data,xvar,yvar){ 
  ggplot({{data}},aes({{xvar}},{{yvar}})) + 
    geom_point()
}


Plot(mtcars,mpg,hp)



Wrangle <- function(data,var1,var2){
  data %>% 
    select({{var1}},{{var2}})
}

Wrangle(mtcars,hp,mpg)

Combo <- function(data,var1,var2){
  a <- data %>% 
    select({{var1}},{{var2}}) %>% 
    ggplot(aes({{var1}},{{var2}})) + 
    geom_point()
  
  b <- data %>% 
    select({{var1}},{{var2}})
  
  print(a)
  print(b)
}

Combo(mtcars,hp,mpg)

SO Similar Question

Cookbook by Hadley Wickham

Community
  • 1
  • 1
Magnus Nordmo
  • 923
  • 7
  • 10