在ggplot2中的绘图区域外添加文本

时间:2018-11-01 21:18:11

标签: r ggplot2

我正在尝试创建以下情节,我想在情节中添加一些其他说明,以使读者了解该情节的一些重要特征。

我正在尝试将4个文本描述(希望带有线段)添加到图形中。

ggplot是否可行?还是应该寻找其他软件包来做到这一点?

线的长度与点的散布有关,我不介意手动为这些线添加固定宽度的线,但是看看是否有办法使线的长度相关也很酷分散(但实际上并不重要!)。

我正在绘制一个模型与另一个模型的预测概率。

Image trying to create

我有一个名为x的数据框

ggplot代码:

ggplot(x, aes(x=mod1, y=mod2, colour = actual)) + 
  geom_point(alpha = 1) +
  geom_density_2d() +
  stat_density_2d(aes(fill = ..level..), geom="polygon", alpha = 1) +
  labs(x = "plot1 results", subtitle = "--------------- subtitle which can be quite long like this -------------------", y = "plot2 results", title = "title") +
  theme_bw(base_size = 11, base_family = "") +
  theme(aspect.ratio = 1) +
  scale_color_manual(values=c("yellow", "grey"))

数据:

structure(list(mod1 = c(0.0428680343284435, 0.846016555762155, 
0.326787531886571, 0.553755029639909, 0.687879627696911, 0.0960930400203601, 
0.744828994271728, 0.540002328947346, 0.220881375043177, 0.0702872626691926, 
0.326427351123072, 0.242150667845905, 0.0585994813808256, 0.0476546237354429, 
0.677024473452915, 0.141965306592508, 0.580238736830929, 0.209089243871524, 
0.202588588164632, 0.462602376730863, 0.170047796107216, 0.0599183763024999, 
0.144128948353236, 0.704983877871062, 0.148981617804389, 0.0259100317297817, 
0.070919748619838, 0.847120835339521, 0.280625159241402, 0.241346727659237, 
0.285528700584795, 0.522832128634093, 0.0495932506050149, 0.191222810970403, 
0.848539280298263, 0.460823513872965, 0.297519579850422, 0.299706327222228, 
0.29118588708967, 0.569263883004122, 0.814633900535549, 0.268597010973285, 
0.583651002250045, 0.0771418703083737, 0.272577428138581, 0.0652980769011686, 
0.0645563141023351, 0.185751367095499, 0.163771389063719, 0.922377554059713, 
0.118440921292355, 0.601015657502687, 0.458036991708823, 0.706976353965206, 
0.557104373519309, 0.336600458082119, 0.365573066188997, 0.349695386601579, 
0.885005310870269, 0.340463030723538, 0.646538075407289, 0.347697108751173, 
0.23887463827597, 0.222397529338268, 0.261741960415693, 0.255160177543014, 
0.394003003413919, 0.350059479442216, 0.443055385407801, 0.999532291288415, 
0.025735898423369, 0.369031728988488, 0.105252031223466, 0.233622390662318, 
0.258892195873903, 0.101629573908821, 0.260570520936073, 0.209063089308277, 
0.265239879267213, 0.137555975136797, 0.0855678812173928, 0.73880289082864, 
0.802094313494666, 0.973899882546715, 1, 0.943515501797875, 0.020488912431986, 
0.138004937708603, 0.111975864093794, 0.818441921329778, 0.0392858886896277, 
0.593233184537478, 0.186525732878499, 0.467999992773845, 0.653350287632996, 
0.550997098851144, 0.525885581162108, 0.98809473982989, 0.304496141867713, 
0.233695105089987, 0.168798462655651, 0.276530329800917, 0.0258753799208103, 
0.398677230034304, 0.601193870473126, 0.33018455671484, 0.546663783633736, 
0.0732052848477258, 0.299579613100531, 0.195704039249802, 0.41032712235619, 
0.358664485435842, 0.102182019177211, 0.111387254699232, 0.558663221258304, 
0.701343535849789, 0.565610725546385, 0.165796298650061, 0.844108924341358, 
0.65239500174214, 0.795485589918214, 0.575301458796068, 0.179957090666757, 
0.883763860288409, 0.491162742628921, 0.615729900118775, 0.561630408279962, 
0.284792242641951, 0.969432341514501, 0.477295676601633, 0.375561806375457, 
0.206089561651849, 0.465548611290696, 0.336325053961017, 0.373667379797929, 
0.570137795506596, 0.491918452884049, 0.190682849018188, 0.225091942414155, 
0.410820863660711, 0.227786666632663, 0.138933996506405, 0.0334324000181915, 
0.162488265240681, 0.805662813620657, 0.0933958810234599, 0.153939073268865, 
0.857625661330725, 0.0963304147676984, 0.380415923377483, 0.0519017480874503, 
0.0779283157397147, 0.107444894718232, 0.10441308985682, 0.712516878369368, 
0.284157191775836, 0.265396916220893, 0.0400873874170811, 0.0393202404365727, 
0.477044935156301, 0.124630185722413, 0.0650517287131615, 0.124980600269956, 
0.320411167606173, 0.248807440955571, 0.082630461317074, 0.756327325304615, 
0.225984532731912, 0.403768216845618, 0.630147108079651, 0.15594857762537, 
0.0839887294293966, 0.150716106034452, 0.121445695528259, 0.35994354107865, 
0.368300361452314, 0.24483459459127, 0.152527988563374, 0.551278085315308, 
0.150034610924398, 0.250987845804079, 0.0565821061235104, 0.712734202323831, 
0.335055664246889, 0.756065329175747, 0.29393094296416, 0.106641584154548, 
0.420158769864025, 0.356454970300771, 0.47515374360064, 0.580812111995106, 
0.000717130307059303, 0.154790410888561, 0.219513327079438, 0.364778107570437, 
0.412859716955486, 0.692560953694384, 0.32077704674508, 0.0877703437440174, 
0.275616887306484, 0.163821283189476, 0.808953944759211, 0.160209449757217, 
0.394250485937628, 0.790498740283875, 0.447255960695868, 0.755359668103058, 
0.537682737841053, 0.593538678937104, 0.196985122041494, 0.276494311008183, 
0.0872459557748893, 0.235580847722567, 0.694748763162609, 0.354039737000946, 
0.44200303784393, 0.165473335918548, 0.467358717157778, 0.0892700946170289, 
0.99962830878852, 0.14014494671621, 0.0690684955560723, 0.145798038742771, 
0.250339990117619, 0.341301599717557, 0.999244229176301, 0.478057677073386, 
0.499445867397323, 0.676004506849537, 0.719051923777321, 0.105192369641408, 
0.299010580450917, 0.684795758632428, 0.291732308319941, 0.0270760029206405, 
0.231610195736126, 0.611894118472659, 0.252430564645428, 0.131649581553132, 
0.510978021474668, 0.462763808757614, 0.237101727522011, 0.119024947839695, 
0.135741592765658, 0.494417857244557, 0.173666298996107, 0.318988535201983, 
0.173381070170447, 0.142219387103895, 0.656232876487217, 0.205846250120757, 
0.122824951848628, 0.162495489321409, 0.872492951796276, 0.909080602176691, 
0.66011964524832, 0.124102526142048, 0.138596610491767, 0.402363981778695, 
0.443105000056591, 0.351367846003605, 0.32105628268368, 0.218800962448852, 
0.118826349897186, 0.276304590212166, 0.0610016455322748, 0.558898605504491, 
0.896861618042103, 0.219229588571431, 0.452525152002443, 0.200601477807967, 
0.497083783289865, 0.110887650356807, 0.423945207587228, 0.0788144014885928, 
0.389111968949349, 0.234569095311629, 0.574908316733503, 0.0650452798024705, 
0.420115866748883, 0.457514278320592, 0.880126441902655, 0.0521590907403362, 
0.961235825781951, 0.270440842694789, 0.114619887870278, 0.34148441206563, 
0.334334164627438, 0.272603252040328, 0.0726735743345817, 0.0492160623167159, 
0.718306549552725, 0.186814116478276, 0.358501035429906, 0.657342239918421, 
0.336900266574, 0.704012501445306, 0.171667389605084, 0.0887593837030433, 
0.194855062569366, 0.992724683416655, 0.217781466318124, 0.244417900635284, 
0.772342328020165, 0.0787202755309474, 0.360467546646677, 0.208455253587378, 
0.412048159709031, 0.237356742436605, 0.0975803958838624, 0.211064024176639, 
0.311204223705994, 0.0583237843898937, 0.389651029808109, 0.345025935039879, 
0.624846648212751, 0.58851623834231, 0.887105187034623, 0.321097430780683, 
0.235333454689797, 0.317948692315331, 0.221829921658406, 0.741550229770297, 
0.20323638533706, 0.300011118692256, 0.10709664298948, 0.128658851710521, 
0.215608428571485, 0.850613750148793, 0.520018226875275, 0.229616805359878, 
0.795191910398009, 0.0903188238048897, 0.397921159282847, 0.203044598795871, 
0.56273414261286, 0.574577913744773, 0.442200678899054, 0.245465935323322, 
0.278019517090414, 0.352947382006002, 0.174645818312427, 0.122145774176944, 
0.170757436677423, 0.397071879644391, 0.191901705813107, 0.0904542790515756, 
0.185273222274775, 0.132590145000319, 0.371241327758872, 0.58624534957165, 
0.0326746116517388, 0.314917326956508, 0.949721350006339, 0.452309070827074, 
0.103763927657044, 0.0701849859768701, 0.586205681965722, 0.1872300676421, 
0.362091824661685, 0.528553649819102, 0.190539638058716, 0.0327545040641537, 
0.762165679327963, 0.274571490276717, 0.512464730498834, 0.27234499730487, 
0.650035734997207, 0.713152866696705, 0.199327736885447, 0.888922212975112, 
0.256517093465039, 0.0491122775280585, 0.145917596045208, 0.519396637888038, 
0.0794241734859925, 0.109718966941177, 0.149020433838973, 0.508447476265555, 
0.175000348118242, 0.226815958091545, 0.282973740661336, 0.320684074902339, 
0.299983143281491, 0.081224779932806, 0.959972054857087, 0.40619879677503, 
0.844555422937453, 0.534525371689877, 0.220441245612505, 0.188279840325184, 
0.184744989425758, 0.577591333604708, 0.182105298203728, 0.578046328506155, 
0.447224424542321, 0.545195283609091, 0.620149367286776, 0.143021628347417, 
0.365110938943906), actual = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L), .Label = c("0", 
"1"), class = "factor"), mod2 = c(0.00921301729977131, 0.420679152011871, 
0.280125766992569, 0.829287350177765, 0.0740553513169289, 0.0137514220550656, 
0.345879256725311, 0.675718426704407, 0.224963515996933, 0.0168505609035492, 
0.206158310174942, 0.0561505369842052, 0.013946115039289, 0.0312005542218685, 
0.812829315662384, 0.0040231547318399, 0.164083942770958, 0.144838228821754, 
0.0544591136276722, 0.0705596581101418, 0.0993659123778343, 0.0502875261008739, 
0.0711468532681465, 0.97149133682251, 0.0123780304566026, 0.0158327762037516, 
0.0151439299806952, 0.161300778388977, 0.0351154617965221, 0.371416091918945, 
0.102943770587444, 0.757962644100189, 0.0110324919223785, 0.047280989587307, 
0.353544414043427, 0.103088781237602, 0.050556942820549, 0.0350653082132339, 
0.401749402284622, 0.242998450994492, 0.95191890001297, 0.247199147939682, 
0.877671599388123, 0.0228813849389553, 0.316035985946655, 0.0982891768217087, 
0.105327241122723, 0.0394041128456593, 0.158778890967369, 0.965853333473206, 
0.00885774753987789, 0.0148940868675709, 0.0758267268538475, 
0.157344698905945, 0.551176071166992, 0.12992300093174, 0.0138051174581051, 
0.475996285676956, 0.948372185230255, 0.134769633412361, 0.556590795516968, 
0.241968929767609, 0.0591361075639725, 0.136440336704254, 0.0668027997016907, 
0.0214910972863436, 0.168018579483032, 0.0699143782258034, 0.0613136924803257, 
0.983657658100128, 0.01039685215801, 0.0587030127644539, 0.00913762766867876, 
0.0317753851413727, 0.0524604544043541, 0.0171482041478157, 0.0351034365594387, 
0.0221143364906311, 0.390063345432281, 0.0124582275748253, 0.0435651019215584, 
0.960456311702728, 0.981251120567322, 0.962678015232086, 0.68353945016861, 
0.971885144710541, 0.00406940141692758, 0.0203599724918604, 0.0633115172386169, 
0.130489587783813, 0.00637582363560796, 0.755789935588837, 0.00400055898353457, 
0.0196541585028172, 0.452250599861145, 0.902487516403198, 0.0917441099882126, 
0.70908921957016, 0.854791462421417, 0.0794311910867691, 0.0273289680480957, 
0.0311463791877031, 0.0127724697813392, 0.0723041146993637, 0.95717054605484, 
0.264508247375488, 0.127880096435547, 0.0171502381563187, 0.0879878774285316, 
0.057909045368433, 0.195128262042999, 0.0270854383707047, 0.0782773569226265, 
0.0383500903844833, 0.372881323099136, 0.285864651203156, 0.116122633218765, 
0.0114724459126592, 0.158793538808823, 0.761920690536499, 0.861652076244354, 
0.227078512310982, 0.0417833961546421, 0.968308210372925, 0.249760672450066, 
0.817656457424164, 0.657787919044495, 0.0230655912309885, 0.991730153560638, 
0.243526577949524, 0.156855791807175, 0.269827306270599, 0.2051682472229, 
0.254308879375458, 0.0800860077142715, 0.34795343875885, 0.972077071666718, 
0.0477356761693954, 0.0464455783367157, 0.736665904521942, 0.0210415944457054, 
0.0383482202887535, 0.00386064685881138, 0.012203705497086, 0.857867896556854, 
0.0814997106790543, 0.0443238392472267, 0.988256871700287, 0.0165475029498339, 
0.0615949258208275, 0.0449854917824268, 0.0454509183764458, 0.01393414568156, 
0.0262223742902279, 0.186277061700821, 0.0324785746634007, 0.033482164144516, 
0.00819115899503231, 0.0117478491738439, 0.0783571749925613, 
0.0223740469664335, 0.0198381245136261, 0.0501869209110737, 0.0376051589846611, 
0.0624551437795162, 0.0314894691109657, 0.860675990581512, 0.0612487271428108, 
0.276889532804489, 0.0167709998786449, 0.0528305657207966, 0.00499858101829886, 
0.0550522655248642, 0.0401820614933968, 0.15572227537632, 0.025402145460248, 
0.0761110484600067, 0.0188306048512459, 0.0380858443677425, 0.0132785346359015, 
0.0640550553798676, 0.00517340190708637, 0.0772885754704475, 
0.19677597284317, 0.70759129524231, 0.147291049361229, 0.0479679442942142, 
0.448112070560455, 0.438730537891388, 0.1216806396842, 0.00735889840871096, 
0.400070250034332, 0.11030688136816, 0.0442567691206932, 0.843577682971954, 
0.558653950691223, 0.0176850743591785, 0.0946979001164436, 0.0153300678357482, 
0.0788581073284149, 0.287300169467926, 0.18515208363533, 0.0233458057045937, 
0.222889557480812, 0.0536097027361393, 0.046075414866209, 0.454137802124023, 
0.0345671623945236, 0.0211565420031548, 0.00878079421818256, 
0.0967049226164818, 0.0367577895522118, 0.0918731242418289, 0.801138401031494, 
0.863967061042786, 0.481592088937759, 0.0336476154625416, 0.042931966483593, 
0.146649375557899, 0.879879474639893, 0.0114125544205308, 0.00469835428521037, 
0.0382965281605721, 0.00703405775129795, 0.0215587317943573, 
0.653248488903046, 0.0782845988869667, 0.121728673577309, 0.0159873776137829, 
0.146458551287651, 0.0206322781741619, 0.0499792955815792, 0.406131267547607, 
0.0681000128388405, 0.0272951126098633, 0.0921002179384232, 0.705728650093079, 
0.0432888120412827, 0.0904701724648476, 0.701448082923889, 0.150377616286278, 
0.0676496997475624, 0.0228539109230042, 0.0765143036842346, 0.113855704665184, 
0.0267140381038189, 0.0929989665746689, 0.152857646346092, 0.0418933369219303, 
0.565863370895386, 0.0166599620133638, 0.0156008023768663, 0.0115091372281313, 
0.903479337692261, 0.986901104450226, 0.928888380527496, 0.0101279402151704, 
0.0802175477147102, 0.0816037356853485, 0.184011369943619, 0.306637078523636, 
0.153407230973244, 0.0167400408536196, 0.22508542239666, 0.0621875934302807, 
0.045804962515831, 0.171572059392929, 0.282828807830811, 0.0179158430546522, 
0.840476810932159, 0.0105379819869995, 0.0114276595413685, 0.0119720129296184, 
0.181616917252541, 0.0978458821773529, 0.12760978937149, 0.302467346191406, 
0.833696305751801, 0.0464334487915039, 0.945941805839539, 0.511578798294067, 
0.974729835987091, 0.0164413973689079, 0.0989365875720978, 0.0765514746308327, 
0.0109183257445693, 0.27131775021553, 0.107158131897449, 0.0736118629574776, 
0.00482818158343434, 0.0201019216328859, 0.941181838512421, 0.0378244519233704, 
0.797275304794312, 0.351534575223923, 0.117122322320938, 0.493010193109512, 
0.0246974248439074, 0.0206503849476576, 0.0464012213051319, 0.959466278553009, 
0.103862524032593, 0.0122975073754787, 0.380784571170807, 0.0136013478040695, 
0.184185728430748, 0.0669872164726257, 0.214213669300079, 0.0391909405589104, 
0.0619069524109364, 0.0159242562949657, 0.0373520478606224, 0.00958761665970087, 
0.377378851175308, 0.166095584630966, 0.925777494907379, 0.0208236053586006, 
0.0404847823083401, 0.559196531772614, 0.0746391415596008, 0.325236350297928, 
0.0599571503698826, 0.26615184545517, 0.0652189999818802, 0.0173883624374866, 
0.0242639016360044, 0.024857934564352, 0.0169697199016809, 0.911997854709625, 
0.100104205310345, 0.167153507471085, 0.787109971046448, 0.0342921391129494, 
0.15250888466835, 0.0209930054843426, 0.0491697303950787, 0.13340713083744, 
0.471260368824005, 0.0106122875586152, 0.144313588738441, 0.0787744149565697, 
0.235925808548927, 0.0196361448615789, 0.0727006196975708, 0.0628127604722977, 
0.0305791813880205, 0.0411793142557144, 0.195792764425278, 0.0215679779648781, 
0.239042669534683, 0.150531396269798, 0.0177980363368988, 0.0576319955289364, 
0.959489762783051, 0.480692893266678, 0.0187541544437408, 0.00829488877207041, 
0.0474584363400936, 0.0482899323105812, 0.785768091678619, 0.0142318215221167, 
0.0503279566764832, 0.00939451158046722, 0.37018147110939, 0.0408202894032001, 
0.196333780884743, 0.128449380397797, 0.934917628765106, 0.792946517467499, 
0.138556912541389, 0.706277251243591, 0.00852821208536625, 0.0416146814823151, 
0.0253815017640591, 0.825974524021149, 0.0193344969302416, 0.0097988685593009, 
0.0383418351411819, 0.791619479656219, 0.138332143425941, 0.017676180228591, 
0.0617045052349567, 0.00605513388291001, 0.0927852019667625, 
0.0261132270097733, 0.953198134899139, 0.182122096419334, 0.958361387252808, 
0.270839661359787, 0.0256280936300755, 0.0315133333206177, 0.0611352697014809, 
0.410940438508987, 0.0302151944488287, 0.868182957172394, 0.0327513180673122, 
0.0963760241866112, 0.955038785934448, 0.0473414175212383, 0.0430381260812283
)), row.names = c(NA, 400L), class = "data.frame")

0 个答案:

没有答案