Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201977

How to pipeline data into stat_function() in tidyverse in this case scenario?

$
0
0

For the data set below I want to plot histograms overlaid with the normal distribution curve using the dnorm function inside stat_function().

MWE

structure(list(Carrot = c(-7.52760119913518, 4.56424405383314, 
-7.52760119913518, -7.52760119913518, -4.0912801441016, -6.78983814224366, 
-7.52760119913518, -7.52760119913518, -3.4546620719379, -7.52760119913518, 
-7.52760119913518, -4.30103579706251, -7.52760119913518, -7.52760119913518, 
-4.71106439279563, -5.43938628845601, -7.52760119913518, -6.1040980011181, 
-5.93584956223258, -7.52760119913518, -2.08700423412679, -7.52760119913518, 
-4.27695150720716, -4.22177947050573, -7.52760119913518, -1.7347789183334, 
-4.28027453706957, -3.71229727877158, -7.52760119913518, -1.56468923964992, 
-4.08430012985491, -7.52760119913518, -7.52760119913518, -7.52760119913518, 
-7.52760119913518, -7.52760119913518, -4.55353225506829, -4.24812063513693, 
-1.98899412309377, -7.52760119913518, -7.52760119913518, -4.58356437340426, 
-7.52760119913518, -7.52760119913518, -7.52760119913518, -7.52760119913518, 
4.0485404120493, -7.52760119913518, -7.52760119913518, -7.52760119913518, 
-7.52760119913518, -7.52760119913518, -7.52760119913518, -7.52760119913518, 
-5.76446254948523, -7.52760119913518, -3.54589044273301, -4.7147064122592, 
-3.68329861302373, -7.52760119913518, -5.20162173014144, -6.33667864605677, 
-7.52760119913518, -7.52760119913518, -5.07230557339384, -7.52760119913518, 
-4.66693321202131, -7.52760119913518, -7.52760119913518, -7.52760119913518, 
-7.52760119913518, -3.76825910218226, -4.08946973987872, -7.52760119913518, 
-2.94393372196423, -2.91802581773336, -1.54265892095247, -7.52760119913518, 
-5.75542060191163, -3.48623748207293, -5.94755075529161, -2.72382940954136, 
-7.52760119913518, -7.52760119913518, -2.54771378153105, -3.8996365699877, 
-4.75552595468454, -7.52760119913518, -7.52760119913518, -7.52760119913518, 
-6.80513805823345, -7.52760119913518, -7.52760119913518, -5.69680133329422, 
-5.71521472950068, -7.52760119913518, -7.52760119913518, -7.52760119913518, 
-7.52760119913518, -6.43302482073175, -7.52760119913518, -7.52760119913518, 
-7.52760119913518, -5.28138617794455, -4.64890669460169, -3.13806676760085, 
-4.23783381783333, -5.48742372264127, -7.52760119913518, -7.52760119913518, 
-7.52760119913518, -7.52760119913518, -7.52760119913518, -7.52760119913518, 
-7.52760119913518, -2.14617604246799, -4.41222999210188, -4.72059654287256, 
-3.08263251597298, 2.43472403690043, -7.52760119913518, -5.28138617794455, 
-3.50881474259708, -4.02786710910707, -7.52760119913518, -7.52760119913518, 
-6.06313302919705, -7.52760119913518, -5.02392990298921, -4.80982001431465, 
-6.34849935157877, -2.98503420115175, -7.52760119913518, -0.134203875863517, 
-3.49187721232018, -3.98817873189236, -5.14288830856046, -3.82073232384231, 
-7.52760119913518, -3.71725344786812, -3.86321549043531, -4.46412854318096, 
-3.48986785050164, -3.81223059025732, -7.52760119913518, -7.52760119913518, 
-4.43827856452116, -5.80038186497676, -6.19508199787652, -4.37059457550608, 
-4.10903668444827, -7.52760119913518, -4.43484225548874, -2.66348556157148, 
-4.03187676837248, -3.94246421444972, -5.85771679245483, -7.52760119913518, 
-4.32842409792483, -7.52760119913518, -7.52760119913518, -7.52760119913518, 
-3.26113631746466, -7.52760119913518, -6.83445401857523, -6.08109850046092, 
-5.59582909758531, -7.52760119913518, -4.09298750134701, -7.52760119913518, 
-4.58909138928273, -5.73065420651607, -2.99372513718649, -7.52760119913518, 
-5.57008097221721, -5.11017914315803, -7.52760119913518, -4.79519011075832, 
-7.52760119913518, -4.46341382733502, -7.52760119913518, -6.18648262346606, 
-3.86590912605395, -1.29511594224073, -7.52760119913518, -7.52760119913518, 
-7.52760119913518, -7.52760119913518, -7.52760119913518, -4.42666083479947, 
-7.52760119913518, -4.54722382420846, -7.52760119913518, -4.61335161963246, 
-5.13704115572175, -4.06200696155157, -5.33874781042572, -5.17942459032443, 
-2.49975285209988, -4.66115125693836, -7.52760119913518, -3.20791146136088, 
-1.23618936024408, -3.83366648366234, -4.24411457182387, -4.37947107119576, 
-7.52760119913518, -3.51126233044479, -7.52760119913518, -7.52760119913518, 
-7.52760119913518, -7.52760119913518, -3.4524741948821, -7.52760119913518, 
-6.06572497543392, -3.08714576797159, -5.14395620358846, -3.69837498307326
), Nuts = c(5.65576434923884, 7.35167389391492, 6.68657787810641, 
5.73423842078703, -0.479427571646657, 0.0613393825389472, 0.587620129792534, 
-3.01724072264205, 0.607441103997701, 1.9941146822818, 1.18783276440063, 
-1.05236526984378, -3.01724072264205, 0.231736557646909, 0.209375903483964, 
0.914233792489486, 1.25529060826894, 0.877867913632736, 0.465890296411343, 
0.33310584903785, 2.99513435324387, 1.94893694717663, 0.645386204364047, 
0.759121031784731, 0.221846602573805, 1.20176763645889, 0.732671327943716, 
0.301502261233483, 1.54550135960469, -3.01724072264205, -0.824047734128466, 
0.297360971251077, -0.113138559763449, -1.44607382219098, 0.230046877658278, 
2.68559719432438, 0.737887622611701, 1.0768102334136, 2.3673296254138, 
1.12093155771974, 0.948113770975723, 1.02953950948616, 6.24682720164151, 
6.03537003660215, 5.94207562541915, -3.01724072264205, 7.31616828838919, 
-3.01724072264205, 1.49888489751396, 1.97498795216498, -3.01724072264205, 
-3.01724072264205, -3.01724072264205, -3.01724072264205, 1.28600511372049, 
-1.49091852984507, 0.681090666312092, 0.616334731880247, 2.41215260177277, 
0.524030294856583, -0.163185255769828, 0.90540613995473, -1.31669232157827, 
0.752329924684452, -0.191835758132297, 0.0935632439105466, -0.00905527265636819, 
-0.21282321060567, 0.898124136585602, 1.51429650997999, -0.190661543820068, 
1.06052644630682, 0.825989680842408, 1.0781399673977, 1.13777338157618, 
-0.155673725038307, 0.263084490855381, -3.01724072264205, 0.729222484362373, 
-0.531282528716159, 1.00226054555619, 0.566630156785764, -0.62602702430332, 
1.39753608815446, -0.473334476989499, 0.84868487891048, -0.166898485521557, 
0.00324238167084799, -0.892783983767064, 0.248472810191159, -0.0474319457089696, 
-3.01724072264205, -0.0979285691096191, -0.299202615243776, -1.7063323981552, 
0.248961815596015, 0.424731406408101, 0.128392272594085, -0.415327351692794, 
0.0688947885197892, -0.994983966843498, -0.861711473302118, 0.480547225989883, 
0.548058260025257, 0.301170532122585, 0.00279521005951438, 0.651194991913207, 
0.0443135278382803, -3.01724072264205, -3.01724072264205, -3.01724072264205, 
-3.01724072264205, -3.01724072264205, 7.72643350953862, -0.0827321701006242, 
0.706671271168806, 0.266731358164268, 0.177839504662661, -0.282411222013033, 
0.934406007128539, 0.393397331869625, 0.548058260025257, 0.486739353398842, 
0.191568912134863, 0.499761431659164, -0.0935881335943071, -0.701872285731864, 
0.575288634873803, -0.55460916080538, 0.111917927307962, 0.0549929774152611, 
-3.01724072264205, -0.386106291502412, 2.64379125121619, -1.18836581124767, 
-0.37891497474629, -1.22406897026704, 1.38480199159989, -3.01724072264205, 
-3.01724072264205, 0.100040103916914, 0.769970385897281, -0.643758339276723, 
-0.0789195029835939, 0.480547225989883, 1.12442515152683, 0.589942564594036, 
0.293233681972298, 0.214378383645912, 0.755484525355873, 1.05614155298585, 
0.251253701471273, 0.585663250275085, 1.41116771128408, 0.0928345771965641, 
-0.309043045335539, -0.495200040156213, -3.01724072264205, 0.226117184744152, 
1.15601965366131, -0.18749833051604, 0.462776218679592, -0.0185686202368568, 
1.29088814299633, 0.329949276886572, 0.694262519921017, 0.57601968574152, 
0.961876441906253, 0.0166302400572378, 0.250444528584274, -0.936666077721652, 
0.355812511325459, 0.588073878825457, 0.178710824582344, 0.369938749177655, 
-0.409325014330487, -0.725069594189143, 2.07163465329115, 2.20841062255817, 
0.701409186981476, -0.169335962972329, 0.0131238219389784, 1.55769958192262, 
1.52923099718672, -0.691125429107339, 0.941935980578087, -0.0824861551730955, 
0.760273610841706, 1.49001805061233, -0.321651005366015, 0.813836082706038, 
-0.74097653255375, 1.21539943440279, -3.01724072264205, 0.60671413567934, 
0.449589092123127, -0.036407936448049, 0.0969748855975764, 0.789321205595386, 
0.0660773862788256, -0.0207788906884193, 0.358085497996286, 2.28914190331105, 
0.314640266751527, -0.0144278026771797, -1.25327836671171, 1.69974518384642, 
0.80289402476793, 0.0118718057829515, 0.476507952591445, 1.34042678880009, 
2.91910850751098, -2.32409354208211, 0.773209761466386, 0.0585122929522497, 
2.4507808508557, 0.312641236630292, -0.117235468045331), Potato = c(-3.1708422272242, 
-3.1708422272242, -3.1708422272242, -3.1708422272242, -0.367332008361822, 
-0.709634713966704, -1.42911044808342, -1.22959627479347, -0.475257379541536, 
0.762651049608282, -0.492598660962332, -0.884576387827383, -1.30315002183612, 
-0.58100213663245, -1.17993892659788, -0.191305329263686, -0.0625374348011994, 
-0.370471703345433, -1.07789195070017, -1.57921228941591, -0.399512739375086, 
-0.864789639905342, -1.41018901959703, -1.51955900799395, -1.80670710574816, 
-1.61470601012883, -0.70713551892171, -1.75707413753896, 1.54708821691983, 
2.04039532013771, -0.628720420898955, -0.974806407839173, -0.863826160381492, 
-0.815816395462377, -0.505234033075453, -0.555290394594686, -2.18050213228068, 
-1.11126289747094, 0.284403233122271, -0.15423046454922, -1.0503099279674, 
-0.874145991314564, -3.1708422272242, -3.1708422272242, -3.1708422272242, 
-3.1708422272242, -3.1708422272242, -3.1708422272242, 0.628887774891211, 
-0.159560013847139, -3.1708422272242, -3.1708422272242, -3.1708422272242, 
-3.1708422272242, 0.00154552370080311, -1.27870520492029, -0.729440134315154, 
0.0430138416985559, 0.938247076220556, -0.906074455016323, -0.746118207559694, 
-0.29246799398921, -0.650892014919122, -1.29792184203333, 0.162294367776984, 
-0.843934263405266, 0.0955001107319588, -1.22659938421134, -1.31427635909154, 
-2.09394997685457, -0.685134215358616, 0.136613521661383, -0.843641902755793, 
-1.45042164347036, -1.77701077096549, -3.1708422272242, -0.122110369714882, 
-1.5644949310007, -1.5027662863948, -1.38108402731151, -0.505448651700276, 
-1.41823557514275, -1.24774172077029, -1.54814468489389, -1.60592122024026, 
-0.338755516295112, -1.75257188981171, -1.30703134495147, -0.609079547798931, 
-1.59728141405138, -1.08913621853404, -3.1708422272242, -0.16957030530346, 
-1.81598595807122, -1.37574012457005, -0.771225780177657, -1.30869771612106, 
-1.45351656620365, -0.705150885777432, -1.84131660570736, -1.03067634270974, 
-1.0141183778047, -0.828160744508896, -0.698290258526702, -0.453124951694789, 
-0.259246969252595, -0.616367096348836, -0.928061547673852, -3.1708422272242, 
-3.1708422272242, -3.1708422272242, -3.1708422272242, -3.1708422272242, 
-3.1708422272242, -0.828160744508896, -0.698290258526702, -0.506194571894111, 
-2.15785451961907, -0.751075335687576, -0.611678307722049, -1.04899744192642, 
-0.625696809880686, -1.32040063625371, -1.63098233021641, -0.800998909498723, 
-0.398562896693006, -1.23392606501527, -1.53512510916287, -0.704508043797989, 
-1.01414300836076, -0.986021663597192, 0.706209185839092, -3.1708422272242, 
-2.47769504666425, -1.80852289038422, -1.10128957920994, -1.43427246492711, 
-2.25779500602102, -2.16302719145671, -2.00720062744483, 0.1847732549988, 
-0.493969203072244, -0.0991613467665297, 0.316868441667461, -1.23449952885672, 
-0.190972887254133, 0.639241879603586, -0.676835663494047, -0.274794025600947, 
0.204513288716005, -0.36235531359344, -0.964218220676797, -0.713614591319477, 
-0.161537503386466, -0.0478837954650077, -0.691891956888363, 
-1.32151358549116, -1.6277848716615, -0.433153128827565, -0.92133405981375, 
-1.49929909256359, -1.55054886801462, -0.00554644007269476, 0.413605736592961, 
-0.835523528417107, -0.229313416723591, -0.378300256806044, -1.50754486405803, 
-0.573865720634569, -0.850765375372113, -0.326470016572277, -0.862826770893392, 
-0.0980772101371879, 0.119360858826688, -0.124625820877411, -0.255482099313502, 
-0.70903026392601, -1.44835073415514, 1.97634471865197, -0.381826003839657, 
-1.54696475382757, -2.20503598178499, 0.667774984890487, -0.404166207340852, 
-1.06240363990298, -0.579308661386525, -1.18078917816641, -3.1708422272242, 
0.355673219418595, -1.89011980295266, -0.193896781535299, -1.23214992475251, 
-1.13123267638976, -2.34123789390784, 0.125682714556542, -0.366209740229349, 
-1.09578882963727, -1.0076713127075, -1.01104258150985, -1.03501914769806, 
-0.761055213134675, 0.207269865160058, 1.85078705490983, -0.153765221340387, 
-0.391784869930536, -0.362760531426073, -0.994431283470251, -1.30515817648136, 
-0.572669544730064, -1.84516815993323, -0.529522317137476, -3.1708422272242, 
-0.695965230737232, -0.947685146754683, -1.30993793721344, 1.48658890049447, 
-0.783929544967561, -0.464022954546944), Cabbage = c(-4.00654026954256, 
-4.00654026954256, -4.00654026954256, -4.00654026954256, -0.336140367863183, 
0.17499138225926, -1.40506519212731, -4.00654026954256, -0.170223857264644, 
0.336283707743854, -1.3027371046219, -1.36237506547705, -1.21466351968271, 
-1.17340536051263, -1.46627723702816, -0.486520962304586, 0.222033483088514, 
-0.467174301620758, -1.21540183882558, -1.31147688315062, -4.00654026954256, 
-0.49242554687212, -0.88863952006614, -0.505404927343125, -1.61387702328746, 
-1.39769345481555, -0.886306806438069, -1.92565716054688, 0.580052970954802, 
-4.00654026954256, -0.901463618505326, -0.238146893558858, -1.02815671030316, 
-1.30431558347687, -1.78037356662115, -4.00654026954256, -0.823276010657086, 
-0.516687195100446, -0.0367634734939036, 0.0938432858620029, 
-0.892736173327766, -2.40305177122932, -4.00654026954256, -4.00654026954256, 
-4.00654026954256, -4.00654026954256, -4.00654026954256, -4.00654026954256, 
0.16499420905677, 0.0468624610807325, -4.00654026954256, -4.00654026954256, 
-4.00654026954256, -4.00654026954256, -0.635795945289265, -4.00654026954256, 
-0.67032161934254, -0.588707369729067, -0.531059205899887, -1.41880533781375, 
-1.57049552033478, -0.741939175216776, -1.0982874224314, -1.01258794288688, 
-0.717221628398942, -0.617328608955866, -1.10834289900203, -0.810971295541482, 
-1.34471736828238, -0.404948626675315, -0.961288686376481, 0.186685966765904, 
-1.92414524428759, -0.41235900072085, -0.847894898688648, -0.442919120598568, 
0.784176975732925, -0.209159939304066, -0.64580572226815, 0.157174262133194, 
0.0937333021562137, 0.134425015255732, -0.385375745040473, 0.107149658528186, 
0.223282237117529, 0.406037333722281, -0.566183965672892, 0.178559170593693, 
-0.915201048677693, -0.838247451756703, -3.31339308898262, -4.00654026954256, 
-0.0403925869189981, -0.847953630952622, -2.25479522295478, -1.51492242892485, 
-0.56844791497117, -1.00002913197713, -1.30144980797049, 0.198552126952905, 
-2.531208985031, -1.86403178537828, -0.99349940615255, -0.605299870925472, 
-0.673986511597792, -0.424974844720397, -0.774548922538073, -1.23827043878079, 
-4.00654026954256, -4.00654026954256, -4.00654026954256, -4.00654026954256, 
-4.00654026954256, -4.00654026954256, -0.163199671601271, 0.636662984541416, 
-0.0998283698889988, -0.0901962454579833, -0.0833713572251478, 
0.130856852575324, -0.257261454361207, 0.781268129887966, 0.188349618322601, 
-0.411480727522284, -0.451157474464974, -0.289473810164464, -1.61384522197885, 
-1.07648025754191, -1.08683366758667, -1.00806118810575, -1.96103491804365, 
-4.00654026954256, 0.0213791245613211, 2.52869026577227, -0.604681702818922, 
0.142711931394348, -1.88501035846813, 0.790036414060718, -4.00654026954256, 
-0.104275169168235, -0.685768260039533, 0.380328419010124, -0.945871357510961, 
-0.577973901507273, -0.99349940615255, -0.605299870925472, -0.177860837858082, 
0.315610377279169, -1.23378697795298, -0.584227716636569, -1.38709176909219, 
-1.47097512481175, -1.26829326875794, 0.324795321274064, -0.825541248166343, 
-1.41098729684803, -0.186522559173058, -1.79514545176311, -0.153762646771485, 
1.72477747423386, -0.646847099824719, -4.00654026954256, -2.19041294177219, 
0.361907714803453, -1.06413237751191, 0.153299426245554, -1.08784753452048, 
-1.31007738176985, -0.979129327388845, -0.773826249565108, -1.36239979168797, 
-0.521851034767972, -0.531686127246093, -0.773635539601728, -1.2461950014608, 
-1.81279653003212, -1.27751707732076, -0.355737228358993, 0.81406668354316, 
-0.223750301006649, -0.425476061402947, -0.236234048814492, 1.71988538759175, 
2.78897727385525, -1.1063009370049, -1.28238725888469, -0.0604231015948721, 
-0.971107771695997, 0.229371740028129, -1.01332291672405, -0.0661293467445485, 
-0.44297255252896, -0.336008667606201, -0.85231732101295, -0.0742519833703415, 
-0.193719473678246, -0.349414524391744, -4.00654026954256, 0.122502623752731, 
-0.1388904885138, -0.794110687208534, 0.642472620652816, 0.849526344895428, 
-0.169137853616827, -0.568626972840837, -0.142840930422038, -0.505350852565564, 
-0.413932605655479, -1.24205839266656, -2.35540284019113, 0.381229921460069, 
-4.00654026954256, -1.08705046472179, -0.934984680113084, -0.796697155239635, 
1.30050102424142, -0.546206094377298, -0.386648415040902)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -218L))

Code scenario 1: worked

When data was gathered using pivot_longer()separately from ggplot code, it worked. Both histograms and curves were drawn. However, as @G.Grothendieck suggested dnorm in stat_function() does not see the passed mean and sd arguments as it should, we always see the same curve in all food items without any variation.

p1 <- MWE %>%
  dplyr::select(Carrot, Nuts, Potato, Cabbage) %>%
  tidyr::pivot_longer(cols = colnames(.), names_to = "Food", values_to = "logAmt")

p2 <- ggplot(p1, aes(logAmt)) + 
  geom_histogram(aes(y = ..density..), fill= "white", colour = "black", binwidth = 1) +
  labs(x = "log Amt", y = "Density") + 
  stat_function(fun=dnorm, args=list(mean= mean(p$logAmt, na.rm = TRUE), sd = sd(p$logAmt, na.rm = TRUE)), colour = "black",size=1) +
  facet_wrap(vars(Food)) +
  theme_linedraw()
p2

Histogram + Distribution Curve

enter image description here

Code scenario 2: worked partially

When both code snippets for data gathering and ggplot were combined into one and the tilde ~ was used, it resulted into a plot with histograms, but without the curves.

p1 <- MWE %>%
  dplyr::select(Carrot, Nuts, Potato, Cabbage) %>%
  tidyr::pivot_longer(cols = colnames(.), names_to = "Food", values_to = "logAmt") %>%
  ggplot(aes(logAmt)) + 
  geom_histogram(aes(y = ..density..), fill= "white", colour = "black", binwidth = 1) +
  labs(x = "log Amt", y = "Density") + 
  stat_function(fun=dnorm, args=list(mean= ~ mean(logAmt, na.rm = TRUE), sd = ~ sd(logAmt, na.rm = TRUE)), colour = "black",size=1) +
  facet_wrap(vars(Food)) +
  theme_linedraw()
p1

Output

enter image description here

Code scenario 3: worked partially too

Curves didn't show up even when tilde was used and p1$logAmt was passed explicitly into the stat_function().

p1 <- MWE %>%
  dplyr::select(Carrot, Nuts, Potato, Cabbage) %>%
  tidyr::pivot_longer(cols = colnames(.), names_to = "Food", values_to = "logAmt") %>%
  ggplot(aes(logAmt)) + 
  geom_histogram(aes(y = ..density..), fill= "white", colour = "black", binwidth = 1) +
  labs(x = "log Amt", y = "Density") + 
  stat_function(fun=dnorm, args=list(mean= ~mean(p1$logAmt, na.rm = TRUE), sd = ~sd(p1$logAmt, na.rm = TRUE)), colour = "black",size=1) +
  facet_wrap(vars(Food)) +
  theme_linedraw()
p1

Question:

How to make stat_function() accept arguments from the pipe-lined data above?


Viewing all articles
Browse latest Browse all 201977

Trending Articles