gpt4 book ai didi

r - ggplot2:多密度图与父直方图分布不匹配

转载 作者:行者123 更新时间:2023-12-01 13:37:54 24 4
gpt4 key购买 nike

我有兴趣创建一个由叠加了两个密度图的直方图组成的图。这些密度图由来自父直方图分布的点组成。我想看到的就像在 this 问题中看到的情节 - 查看产生红色和绿色分布的答案。

这是我的数据:

structure(list(Cutoff = c(1.19535655856797, 1.00687219356421, 
1.33315797195675, 1.09477069869784, 1.65253746761874, 0.954295996038076,
1.20732336423235, 1.15074753036442, 1.08361695876052, 1.11161869571304,
1.04278294535548, 1.67244432858406, 1.45497623339576, 1.20426170494466,
1.28386921586645, 1.16696600933587, 0.982131539833289, 1.20243855019263,
1.41783096383078, 0.762576315135001, 1.36716677751145, 1.25535520499449,
1.33547059166591, 1.02642973816899, 1.09673898885085, 1.61789437523638,
1.04479862671494, 1.30082080966193, 1.12376095826976, 1.0606346337333,
1.48799361737922, 1.33183005619348, 1.30725022210951, 1.02105502999452,
1.26144371469816, 0.953175430354252, 0.997642362859814, 1.44972549997821,
1.02142972166334, 1.42432267398241, 1.26007802349474, 1.14307519593422,
1.42838714211787, 0.957789929337148, 1.19674944681346, 1.12844014419085,
1.08722817312552, 1.06370370988222, 1.33678275268601, 1.48941784405378,
1.07824988532688, 1.19654972603173, 1.35628770305355, 0.873506707948171,
1.45829648655307, 1.59393562467138, 1.17178479831943, 1.52583464236262,
1.22707754030596, 0.857041343722077, 1.0116909720029, 1.07819360046119,
1.58836385239501, 1.2033227986932, 1.42804789634878, 1.00950377336327,
1.73512435659666, 1.19990369220469, 1.09843891337962, 1.47131825867473,
1.02563609187756, 1.43837286870019, 1.29040577192678, 1.43443639830045,
1.29700257725667, 1.17149068176944, 0.842864888836796, 1.16025783792664,
1.0357954574795, 1.06738427111227, 1.34602465415023, 1.78569651770027,
1.22592288811681, 1.17464703525909, 1.47544536605496, 1.26316471872366,
0.895129191534453, 1.3905731504035, 1.3510408440799, 0.818461796489174,
1.05641603527927, 1.4963366725854, 1.38768560701499, 1.06599089491115,
1.02566832416116, 0.880641188572421, 1.43003711331319, 1.02809817760591,
1.32612981295039, 1.0828539014644, 1.35520274916791, 1.55366768856036,
1.22413725280648, 1.23065734576842, 1.43891509505049, 1.67269959674531,
1.19533802455926, 1.14562328414776, 0.998403140642873, 1.34633449531565,
1.28744624431712, 1.08980241180118, 1.13521770923318, 0.914249007411708,
0.971944955550378, 1.11522016789312, 1.33545052851018, 1.18873110892389,
1.12817128413039, 1.08718464862075, 1.23984572424846, 1.3014113522081,
1.81240325219092, 1.64205580435599, 1.35452641838475, 1.13596316388751,
1.05699906219164, 1.0450613859957, 1.10639705801065, 1.40806160359608,
1.34778981457641, 1.1870669528721, 1.34956984666503, 0.977210537601071,
1.40456554178993, 0.895356831317802, 1.03124252809311, 0.900177917583693,
1.39123517489037, 1.31277132256094, 1.25238316908682, 1.30314562115018,
0.890627844546382, 1.11998676739035, 1.23716550630255, 1.10463292675312,
0.988415982531512, 1.13171283424437, 0.433692499657705, 0.899627386784149,
1.08275798209318, 1.01547057638643, 1.16542649908968, 1.2354422362405,
1.06727668300262, 0.945252018169863, 0.769385436605055, 1.1163288014311,
1.59742426902313, 0.894366917285648, 1.21611592490999, 1.16919007185351,
1.15447010072718, 1.39705959460693, 1.10817648083716, 1.25539355379958,
1.2750873541554, 1.0412949829686, 1.50422296248127, 1.03910065604786,
1.31873226600994, 1.06674533114536, 1.12919726204513, 1.16225072088168,
0.606597376636368, 1.32932864182284, 1.24836555763799, 1.24862630378576,
1.09288928297293, 1.07696616659194, 1.14843525965918, 1.34635987148075,
1.13559940972391, 1.58082222419271, 1.03248326744981, 1.20136159133835,
1.12808896572004, 1.29559870608019, 1.28796685978649, 1.03586624174504,
1.77440556826208, 1.8387236023348, 1.18361428417798, 1.66284889965013,
1.21655122342407, 1.23810573912618, 1.48160384952174, 1.28486988972092,
1.22290283065424, 1.21946787030264, 1.33025834241741, 1.33906073480191,
1.09315392512254, 1.37158212870866, 1.35208376419882, 0.741987763693633,
1.01974779071812, 0.878429785750235, 1.18929126577041, 1.16889236255407,
1.04556791718023, 1.51384422486689, 1.1303170878895, 1.75610565406816,
0.961285474862535, 0.977796308551145, 2.06221359310871, 0.860394359505867,
1.64203613284105, 1.29265918374296, 1.27457054186775, 1.4396100833089,
1.28280815153747, 1.09131935174484, 1.32350842964275, 0.771592315433869,
1.31825209680836, 1.29293812205461, 1.05362879319826, 1.34927677643637,
1.52362075783261, 1.21497329939847, 0.908154037600659, 1.33460388977043,
1.16177201880576, 1.26239428165702, 1.15203564980178, 1.36524900743014,
1.10141234727673, 1.13914225116249, 1.15634021518068, 1.18512640724095,
1.22987478362393, 1.06567783688687, 1.18785333032282, 1.3139844985697,
1.40183506827889, 1.32673731682262, 1.1366996368783, 1.00923621012365,
1.22904890507408, 1.57168986105796, 1.097530751097, 1.15071476199135,
1.18958888935323, 1.51454304293632, 1.01407017447938, 0.889568445361318,
1.37237980764125, 1.39175363551287, 0.996712864605389, 1.3214778815044,
1.45963301615109, 1.10019357014712, 0.940214917197717, 1.23484333220055,
0.967935648110094, 1.19482006261635, 1.11482331868415, 1.20270953137537,
1.28831527636632, 1.6928973819907, 1.15873369791146, 1.43515132490529,
1.13004278496052, 1.23401442636964, 1.60524691038898, 1.49526106646539,
1.05787715695484, 1.40321993946032, 1.23361927645798, 1.25176899701962,
1.09660663326742, 1.25214655505837, 0.920213347718148, 1.24179302074118,
1.07910308100457, 1.07047343454682, 1.0579167635207, 0.898592755603007,
1.22651493972518, 1.37112876605963, 1.07284222040129, 1.21528410684225,
1.01777816043295, 1.46650309177544, 1.00445577334459, 1.16290840349811,
1.07539235143726, 1.75485338623817, 1.46264780170267, 0.984087333807222,
0.918218649523582, 1.11689439771942, 1.17846274029157, 1.52196882426436,
1.36551315219604, 2.18030270529274, 0.901236790849296, 1.12449117590663,
0.915478259782492, 1.28416702478417, 1.26238689071989, 1.31656423922975,
1.62926579771164, 1.16677201923425, 1.13467233773369, 1.59889473552855,
1.0487525184388, 1.16638890980671, 1.19715055583738, 1.26523860088208,
2.09557583610316, 1.18849057268596, 0.943828335561374, 1.47396201256549,
1.08602808440518, 1.42955591113502, 1.81045234899294, 1.22410533121894,
1.13695503773132, 1.28740474619474, 1.06001595907444, 0.765983057086113,
0.951691653861695, 1.12525818635194, 1.13116740276077, 1.25164036178403,
1.16812408094997, 1.24324774267072, 1.27045084692517, 0.866442824843758,
1.36227925719918, 1.18303727981349, 1.16754223498179, 1.19972027626558,
1.40752650809281, 1.00584004223674, 1.15140576409637, 0.944201131060717,
1.52451557578741, 1.38029958061432, 1.90452307357901, 1.49489253001723,
1.24797593373183, 1.35658950279238, 1.04922285648539, 1.32804028774287,
1.30686236048952, 1.45608232397025, 0.907065154352906, 2.38028874016209,
1.29278665187292, 0.81338084288152, 2.02270898963902, 1.00717277776781,
1.05249212420597, 1.36265164827765, 1.51181646354118, 1.20399309917326,
1.36000891331203, 1.11215511603238, 0.909635538783894, 1.30376444976774,
0.871517690858904, 1.2252786427561, 1.22687199889338, 1.06970837726306,
1.29930098629761, 1.62959326421609, 1.18057471430353, 1.43176617578033,
1.1428251839243, 1.34261024814042, 0.819275264656779, 1.18896234061877,
1.0581924220734, 1.26250350980617, 1.18754356380991, 1.43395497295304,
0.934939901536231, 0.978913998840743, 1.29216584241203, 1.36735246896605,
1.16408284335089, 1.28962226640836, 1.03395523422684, 0.976269288920915,
1.38261513019981, 1.28581794965273, 1.49927100515505, 1.01426081336005,
0.75548089050502, 1.10264349411622, 1.07717323667075, 1.53601881908475,
1.51635293228152, 1.97662874137008, 0.974123050122801, 1.06906141137815,
1.07428305947065, 1.47627718824775, 1.01454199851032, 1.32114284009611,
1.03092295244659, 1.66665860897455, 1.31064988526366, 1.2586054752041,
1.04672862731146, 1.40466139162607, 1.17289539732947, 1.61838033183169,
1.41849403804458, 1.0035230908756, 0.786696795879351, 1.11042450576261,
1.25784901444192, 1.12615488605529, 1.01844119925981, 1.17024965616828,
1.23165686153788, 1.4633163557926, 0.96380440159532, 1.36351614591882,
1.01759119278186, 0.940867765480442, 0.870923083838016, 1.00330815490142,
1.21591967261849, 0.984887146650252, 1.05109328917909, 1.26946941155758,
1.73447358284825, 0.94148801973368, 0.697543009150257, 1.0592000749516,
1.15661895909039, 1.02679764406415, 1.24551004533085, 1.05850316838337,
1.17343246454205, 0.798106420617163, 1.39685960673458, 1.03920220262853,
1.04140369320848, 1.2010882058708, 1.11703280178688, 0.822460902560955,
1.11532931726096, 1.49552170029888, 1.12377692779216, 1.44668189202669,
1.06900490584055, 1.06464160861989, 0.925167117104425, 1.29856067508455,
1.42626180004344, 1.27435742320208, 1.22517913148894, 1.02519043058666,
0.74603309984024, 1.38546484253891, 1.23042641376434, 1.10530079968686,
1.15796597134372, 0.972380469849424, 1.40894228000384, 1.18545703914345,
1.40844320207071, 1.39178539783452, 1.73382589101331, 1.23485159808068,
1.21702972993969, 1.20458466827079, 1.33782943393987, 1.11610879802248,
1.03160028231654, 1.67139833756055, 1.02691760884264, 1.15343386978225,
0.818326095619363, 1.10222819939143, 1.19317070915881, 1.14177872420085,
1.03012214451353, 1.33178976164308, 1.3368275392932, 1.48384671038519,
1.08458451579517, 0.792989623258065), Filter = c("Signal", "Background",
"Signal", "Background", "Signal", "Background", "Signal", "Signal",
"Background", "Signal", "Background", "Signal", "Signal", "Signal",
"Signal", "Signal", "Background", "Signal", "Signal", "Background",
"Signal", "Signal", "Signal", "Background", "Background", "Signal",
"Background", "Signal", "Signal", "Background", "Signal", "Signal",
"Signal", "Background", "Signal", "Background", "Background",
"Signal", "Background", "Signal", "Signal", "Signal", "Signal",
"Background", "Signal", "Signal", "Background", "Background",
"Signal", "Signal", "Background", "Signal", "Signal", "Background",
"Signal", "Signal", "Signal", "Signal", "Signal", "Background",
"Background", "Background", "Signal", "Signal", "Signal", "Background",
"Signal", "Signal", "Background", "Signal", "Background", "Signal",
"Signal", "Signal", "Signal", "Signal", "Background", "Signal",
"Background", "Background", "Signal", "Signal", "Signal", "Signal",
"Signal", "Signal", "Background", "Signal", "Signal", "Background",
"Background", "Signal", "Signal", "Background", "Background",
"Background", "Signal", "Background", "Signal", "Background",
"Signal", "Signal", "Signal", "Signal", "Signal", "Signal", "Signal",
"Signal", "Background", "Signal", "Signal", "Background", "Signal",
"Background", "Background", "Signal", "Signal", "Signal", "Signal",
"Background", "Signal", "Signal", "Signal", "Signal", "Signal",
"Signal", "Background", "Background", "Background", "Signal",
"Signal", "Signal", "Signal", "Background", "Signal", "Background",
"Background", "Background", "Signal", "Signal", "Signal", "Signal",
"Background", "Signal", "Signal", "Background", "Background",
"Signal", "Background", "Background", "Background", "Background",
"Signal", "Signal", "Background", "Background", "Background",
"Signal", "Signal", "Background", "Signal", "Signal", "Signal",
"Signal", "Background", "Signal", "Signal", "Background", "Signal",
"Background", "Signal", "Background", "Signal", "Signal", "Background",
"Signal", "Signal", "Signal", "Background", "Background", "Signal",
"Signal", "Signal", "Signal", "Background", "Signal", "Signal",
"Signal", "Signal", "Background", "Signal", "Signal", "Signal",
"Signal", "Signal", "Signal", "Signal", "Signal", "Signal", "Signal",
"Signal", "Signal", "Background", "Signal", "Signal", "Background",
"Background", "Background", "Signal", "Signal", "Background",
"Signal", "Signal", "Signal", "Background", "Background", "Signal",
"Background", "Signal", "Signal", "Signal", "Signal", "Signal",
"Background", "Signal", "Background", "Signal", "Signal", "Background",
"Signal", "Signal", "Signal", "Background", "Signal", "Signal",
"Signal", "Signal", "Signal", "Background", "Signal", "Signal",
"Signal", "Signal", "Background", "Signal", "Signal", "Signal",
"Signal", "Signal", "Background", "Signal", "Signal", "Background",
"Signal", "Signal", "Signal", "Background", "Background", "Signal",
"Signal", "Background", "Signal", "Signal", "Background", "Background",
"Signal", "Background", "Signal", "Signal", "Signal", "Signal",
"Signal", "Signal", "Signal", "Signal", "Signal", "Signal", "Signal",
"Background", "Signal", "Signal", "Signal", "Background", "Signal",
"Background", "Signal", "Background", "Background", "Background",
"Background", "Signal", "Signal", "Background", "Signal", "Background",
"Signal", "Background", "Signal", "Background", "Signal", "Signal",
"Background", "Background", "Signal", "Signal", "Signal", "Signal",
"Signal", "Background", "Signal", "Background", "Signal", "Signal",
"Signal", "Signal", "Signal", "Signal", "Signal", "Background",
"Signal", "Signal", "Signal", "Signal", "Signal", "Background",
"Signal", "Background", "Signal", "Signal", "Signal", "Signal",
"Signal", "Background", "Background", "Background", "Signal",
"Signal", "Signal", "Signal", "Signal", "Signal", "Background",
"Signal", "Signal", "Signal", "Signal", "Signal", "Background",
"Signal", "Background", "Signal", "Signal", "Signal", "Signal",
"Signal", "Signal", "Background", "Signal", "Signal", "Signal",
"Background", "Signal", "Signal", "Background", "Signal", "Background",
"Background", "Signal", "Signal", "Signal", "Signal", "Signal",
"Background", "Signal", "Background", "Signal", "Signal", "Background",
"Signal", "Signal", "Signal", "Signal", "Signal", "Signal", "Background",
"Signal", "Background", "Signal", "Signal", "Signal", "Background",
"Background", "Signal", "Signal", "Signal", "Signal", "Background",
"Background", "Signal", "Signal", "Signal", "Background", "Background",
"Background", "Background", "Signal", "Signal", "Signal", "Background",
"Background", "Background", "Signal", "Background", "Signal",
"Background", "Signal", "Signal", "Signal", "Background", "Signal",
"Signal", "Signal", "Signal", "Background", "Background", "Signal",
"Signal", "Signal", "Background", "Signal", "Signal", "Signal",
"Background", "Signal", "Background", "Background", "Background",
"Background", "Signal", "Background", "Background", "Signal",
"Signal", "Background", "Background", "Background", "Signal",
"Background", "Signal", "Background", "Signal", "Background",
"Signal", "Background", "Background", "Signal", "Signal", "Background",
"Signal", "Signal", "Signal", "Signal", "Background", "Background",
"Background", "Signal", "Signal", "Signal", "Signal", "Background",
"Background", "Signal", "Signal", "Background", "Signal", "Background",
"Signal", "Signal", "Signal", "Signal", "Signal", "Signal", "Signal",
"Signal", "Signal", "Signal", "Background", "Signal", "Background",
"Signal", "Background", "Background", "Signal", "Signal", "Background",
"Signal", "Signal", "Signal", "Background", "Background")), .Names = c("Cutoff",
"Filter"), row.names = c(1525L, 4059L, 2632L, 2984L, 890L, 710L,
975L, 1385L, 2972L, 609L, 2627L, 3087L, 3998L, 1892L, 2700L,
1293L, 3215L, 4011L, 470L, 2544L, 3420L, 1162L, 1085L, 2063L,
26L, 3224L, 2938L, 2860L, 2967L, 498L, 127L, 2240L, 1812L, 2575L,
3925L, 2638L, 2202L, 2379L, 1383L, 2279L, 2373L, 1706L, 3635L,
2789L, 1972L, 3609L, 3781L, 2563L, 1611L, 1505L, 841L, 2360L,
2528L, 3459L, 3587L, 1582L, 3742L, 977L, 703L, 1847L, 1780L,
3073L, 2348L, 502L, 3555L, 2832L, 3947L, 93L, 1713L, 1732L, 676L,
3573L, 1020L, 2511L, 1631L, 2460L, 3487L, 3488L, 1087L, 2561L,
3743L, 1523L, 966L, 43L, 2285L, 2954L, 666L, 2005L, 846L, 1770L,
3479L, 48L, 3402L, 4044L, 264L, 1454L, 2494L, 2553L, 1147L, 1762L,
4115L, 3021L, 963L, 259L, 2773L, 764L, 1639L, 2043L, 3773L, 2779L,
2223L, 2930L, 3204L, 3805L, 3700L, 3254L, 2375L, 3828L, 481L,
2729L, 4060L, 3783L, 3970L, 1004L, 2598L, 2229L, 3120L, 1046L,
313L, 2486L, 2854L, 3699L, 1448L, 70L, 1418L, 2041L, 1406L, 1253L,
2182L, 2472L, 2421L, 3991L, 3539L, 2306L, 2582L, 2474L, 3441L,
2419L, 17L, 3490L, 3512L, 2248L, 2200L, 2717L, 4047L, 3216L,
1096L, 1578L, 78L, 2154L, 757L, 2919L, 1267L, 3719L, 2153L, 2126L,
1880L, 2799L, 1102L, 402L, 8L, 2971L, 3354L, 3564L, 919L, 1616L,
847L, 463L, 1163L, 903L, 611L, 1544L, 2783L, 855L, 2431L, 2958L,
2552L, 2587L, 3727L, 3614L, 1216L, 3481L, 3770L, 1783L, 3997L,
1490L, 3280L, 4065L, 1855L, 3864L, 1702L, 1441L, 2046L, 3446L,
579L, 672L, 2605L, 1346L, 994L, 3250L, 483L, 2556L, 1254L, 3186L,
383L, 1999L, 283L, 3787L, 3815L, 2168L, 1173L, 2303L, 2760L,
557L, 1258L, 3834L, 2610L, 1245L, 1218L, 1289L, 1402L, 3993L,
2435L, 471L, 3652L, 599L, 1330L, 2160L, 260L, 2221L, 1009L, 1546L,
189L, 2551L, 2742L, 2415L, 327L, 2034L, 321L, 1360L, 1842L, 280L,
425L, 181L, 3425L, 1039L, 2548L, 2666L, 3235L, 2615L, 4062L,
2073L, 382L, 1865L, 1936L, 2710L, 3278L, 4107L, 2230L, 802L,
538L, 466L, 2399L, 2914L, 2328L, 1049L, 3561L, 2737L, 1335L,
3707L, 1249L, 3246L, 583L, 3568L, 1038L, 2491L, 2606L, 2016L,
1874L, 2859L, 1164L, 1002L, 988L, 2829L, 3227L, 742L, 1676L,
1461L, 1549L, 1547L, 2414L, 3681L, 2861L, 1325L, 3867L, 2622L,
2608L, 323L, 3307L, 1796L, 75L, 1310L, 444L, 1684L, 1941L, 2048L,
720L, 3413L, 2307L, 3311L, 3757L, 3547L, 1911L, 1075L, 3928L,
3868L, 3349L, 1188L, 1169L, 2216L, 1327L, 4017L, 3200L, 2559L,
2595L, 552L, 2278L, 307L, 2708L, 1030L, 2099L, 1434L, 71L, 2301L,
3605L, 3642L, 2271L, 2464L, 3675L, 1063L, 608L, 1429L, 140L,
2029L, 3181L, 2267L, 913L, 759L, 1278L, 2077L, 3105L, 238L, 3139L,
3400L, 3840L, 3667L, 2269L, 1499L, 3782L, 2946L, 1238L, 2719L,
3172L, 3996L, 1729L, 40L, 1036L, 1593L, 4008L, 2957L, 125L, 3326L,
1430L, 3189L, 2538L, 2733L, 1947L, 3371L, 1867L, 1248L, 2994L,
2418L, 490L, 65L, 578L, 3945L, 3688L, 1018L, 4079L, 1474L, 876L,
3916L, 639L, 1624L, 1056L, 3571L, 3394L, 2604L, 92L, 414L, 1357L,
4058L, 3403L, 1479L, 3148L, 1112L, 3249L, 3161L, 213L, 3116L,
3259L, 1898L, 2257L, 1519L, 3949L, 1205L, 1722L, 2788L, 1177L,
545L, 2347L, 1410L, 3020L, 3999L, 2452L, 1918L, 1507L, 3357L,
3135L, 3029L, 740L, 3201L, 2355L, 1828L, 2851L, 1666L, 935L,
1885L, 1072L, 1669L, 1781L, 3390L, 3267L, 1881L, 3995L, 1900L,
897L, 597L, 1138L, 3366L, 572L, 3855L, 524L, 3793L, 705L, 68L,
1551L, 1592L, 2344L, 1548L, 1650L, 1026L, 225L, 3969L, 3551L,
2298L, 837L, 456L, 4085L, 2261L, 372L, 844L, 3621L, 1719L, 2007L,
3691L, 1252L, 3383L, 3590L, 406L, 2805L, 195L, 2212L, 2018L,
212L, 1143L, 2294L, 941L, 982L, 1691L), class = "data.frame")

我试图通过创建指示子分布成员资格的新数据框来模仿上面答案中的情节。将上面的数据分配给变量dat

library(ggplot2)

g1 <- dat[dat$Filter == "Signal", ]
g2 <- dat[dat$Filter == "Background", ]

ggplot(dat, aes(x = Cutoff)) +
geom_histogram(aes(y = ..density..), fill = "grey60", bins = 100) +
stat_function(fun = dnorm,
args = list(mean = mean(g1$Cutoff),
sd = sd(g1$Cutoff)),
geom = "density",
fill = "chartreuse3",
alpha = 0.5) +
stat_function(fun = dnorm,
args = list(mean = mean(g2$Cutoff),
sd = sd(g2$Cutoff)),
geom = "density",
fill = "firebrick2",
alpha = 0.5) +
theme_bw()

但是,我从中得到的情节是这样的:

Histogram with child densities

虽然密度图遵循直方图的一般形状,但红色峰值看起来比直方图高很多。那么,我是否误解了关于密度图和直方图有何不同的基本知识?它们完全在不同的 y 轴刻度上吗?我如何更改我的比例以使生成的图看起来更接近上面链接的 StackOverflow 答案中的示例?请注意,在我包含 5000 个观测值的实际数据集中,红色峰值看起来更加明显,远远超过直方图的限制。弄乱垃圾箱有一点帮助,但作用不大。

最佳答案

首先,我认为您是根据每个子集的均值和标准差绘制完美的正态分布,而不是实际的密度分布。其次,您正在绘制基于 500 个观测值的密度直方图,然后绘制基于 328 和 172 个观测值的两条密度曲线。尝试添加两个单独的 geom_histogramgeom_density 层来指定每个子集...

ggplot(dat, aes(x = Cutoff)) +
geom_histogram(data=dat[which(dat$Filter%in%"Signal"),], aes(y = ..density..), fill = "grey60", bins = 100) +
geom_histogram(data=dat[which(dat$Filter%in%"Background"),], aes(y = ..density..), fill = "grey60", bins = 100) +
geom_density(data=dat[which(dat$Filter%in%"Signal"),], aes(x=Cutoff, y=..density..), fill="chartreuse3", alpha=0.5) +
geom_density(data=dat[which(dat$Filter%in%"Background"),], aes(x=Cutoff, y=..density..), fill="firebrick2", alpha=0.5) +
theme_bw()

...会给你这个情节:

enter image description here

否则,如果您确实想要绘制单个父直方图但绘制多个子集密度,您应该期望峰值不匹配。即:

ggplot(dat, aes(x = Cutoff)) +
geom_histogram(aes(y = ..density..), fill = "grey60", bins = 100) +
geom_density(data=dat[which(dat$Filter%in%"Signal"),], aes(x=Cutoff, y=..density..), fill="chartreuse3", alpha=0.5) +
geom_density(data=dat[which(dat$Filter%in%"Background"),], aes(x=Cutoff, y=..density..), fill="firebrick2", alpha=0.5) +
theme_bw()

enter image description here

关于r - ggplot2:多密度图与父直方图分布不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42491473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com