如何在不拖动绘图面板的情况下缩放R ggplot2中的图例?

问题描述 投票:0回答:1

我的数据框:

 dput(data)
structure(list(Analysis = c("1", "1", "1", "1", "1", "2", "2", 
"2", "2", "2", "3", "3", "3", "3", "3", "4", "4", "4", "4", "4", 
"5", "5", "5", "5", "5", "44931", "44931", "44931", "44931", 
"44931", "44931", "44931", "44931", "44931", "44931", "6", "6", 
"6", "6", "6", "7", "7", "7", "7", "7", "8", "8", "8", "8", "8", 
"9", "9", "9", "9", "9", "10", "10", "10", "10", "10", "45087", 
"45087", "45087", "45087", "45087", "45087", "45087", "45087", 
"45087", "45087", "11", "11", "11", "11", "11", "12", "12", "12", 
"12", "12", "13", "13", "13", "13", "13", "14", "14", "14", "14", 
"14", "15", "15", "15", "15", "15", "45245", "45245", "45245", 
"45245", "45245", "45245", "45245", "45245", "45245", "45245", 
"16", "16", "16", "16", "16", "17", "17", "17", "17", "17", "18", 
"18", "18", "18", "18", "19", "19", "19", "19", "19", "20", "20", 
"20", "20", "20", "16-20", "16-20", "16-20", "16-20", "16-20", 
"16-20.", "16-20.", "16-20.", "16-20.", "16-20."), Outcome = c("BMI", 
"BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI", 
"BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI", 
"BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI", 
"BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "BMI", "WHR", "WHR", 
"WHR", "WHR", "WHR", "WHR", "WHR", "WHR", "WHR", "WHR", "WHR", 
"WHR", "WHR", "WHR", "WHR", "WHR", "WHR", "WHR", "WHR", "WHR", 
"WHR", "WHR", "WHR", "WHR", "WHR", "WHR", "WHR", "WHR", "WHR", 
"WHR", "WHR", "WHR", "WHR", "WHR", "WHR", "BF", "BF", "BF", "BF", 
"BF", "BF", "BF", "BF", "BF", "BF", "BF", "BF", "BF", "BF", "BF", 
"BF", "BF", "BF", "BF", "BF", "BF", "BF", "BF", "BF", "BF", "BF", 
"BF", "BF", "BF", "BF", "BF", "BF", "BF", "BF", "BF", "WC", "WC", 
"WC", "WC", "WC", "WC", "WC", "WC", "WC", "WC", "WC", "WC", "WC", 
"WC", "WC", "WC", "WC", "WC", "WC", "WC", "WC", "WC", "WC", "WC", 
"WC", "WC", "WC", "WC", "WC", "WC", "WC", "WC", "WC", "WC", "WC"
), Covariate = c("HD", "HD", "HD", "HD", "HD", "PA", "PA", "PA", 
"PA", "PA", "PALC", "PALC", "PALC", "PALC", "PALC", "SMK", "SMK", 
"SMK", "SMK", "SMK", "NS", "NS", "NS", "NS", "NS", "sum", "sum", 
"sum", "sum", "sum", "PC1", "PC1", "PC1", "PC1", "PC1", "HD", 
"HD", "HD", "HD", "HD", "PA", "PA", "PA", "PA", "PA", "PALC", 
"PALC", "PALC", "PALC", "PALC", "SMK", "SMK", "SMK", "SMK", "SMK", 
"NS", "NS", "NS", "NS", "NS", "sum", "sum", "sum", "sum", "sum", 
"PC1", "PC1", "PC1", "PC1", "PC1", "HD", "HD", "HD", "HD", "HD", 
"PA", "PA", "PA", "PA", "PA", "PALC", "PALC", "PALC", "PALC", 
"PALC", "SMK", "SMK", "SMK", "SMK", "SMK", "NS", "NS", "NS", 
"NS", "NS", "sum", "sum", "sum", "sum", "sum", "PC1", "PC1", 
"PC1", "PC1", "PC1", "HD", "HD", "HD", "HD", "HD", "PA", "PA", 
"PA", "PA", "PA", "PALC", "PALC", "PALC", "PALC", "PALC", "SMK", 
"SMK", "SMK", "SMK", "SMK", "NS", "NS", "NS", "NS", "NS", "sum", 
"sum", "sum", "sum", "sum", "PC1", "PC1", "PC1", "PC1", "PC1"
), Model = 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*", "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*", "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*"), Component = c("E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`", "E", 
"PRS_add", "PRS_gxe", "`PRS_gxe x E`", "`PRS_gxe x E`"), Estimate = c(-0.062380807, 
0.261418927, -0.008830466, 0.012499253, NA, -0.0950719745, 0.2559453192, 
0.0093277345, 0.0117124572, NA, 0.0166398868, 0.2461008616, 0.0029314746, 
0.0226989723, NA, 0.0674496709, 0.2510790512, 0.0035534067, 0.018918288, 
NA, 0.0094429625, 0.2591651705, -0.009464173, 0.0107123808, NA, 
-0.0232108, 0.2178634, -0.008581625, 0.01278613, NA, -0.07834664, 
0.2166238, 0.008461322, 0.03421711, NA, -0.0586041847, 0.1659735857, 
0.0014867014, 0.0068498045, NA, -0.0684145928, 0.1589283042, 
-0.0008041458, 0.0019058497, NA, 0.043087963, 0.1567704945, 0.0031239276, 
0.0151203793, NA, 0.1130607299, 0.1564277515, -0.0004048215, 
0.0076629355, NA, 0.0308930862, 0.1611094866, -0.0032195266, 
0.0062717681, NA, 0.033923884, 0.135786842, -0.001091359, 0.009961985, 
NA, -0.1091798016, 0.1334177138, 0.0082748328, 0.0217338742, 
NA, -0.0532114142, 0.199453988, -0.0069068752, 0.0110325724, 
NA, -0.1009898697, 0.1932538134, 0.0027129119, 0.0072478923, 
NA, 0.0227549873, 0.1912166479, 0.0025180754, 0.0124924676, NA, 
0.05905524, 0.1910188, -0.0004256009, 0.01166245, NA, 0.0168359536, 
0.1980516157, -0.0100613659, 0.0058196998, NA, -0.025850049, 
0.1657300912, -0.0035409, 0.0046373628, NA, -0.0834131523, 0.1644548358, 
0.0011601316, 0.0225414204, NA, -0.0695000701, 0.2262457191, 
-0.0035872725, 0.0122174121, NA, -0.1093854, 0.2154001, 0.002857311, 
0.009985201, NA, 0.0396458, 0.2114433, 0.002295243, 0.0216002, 
NA, 0.1059817367, 0.2135751165, 0.0038698127, 0.0113469119, NA, 
0.0253791989, 0.2207742773, -0.0044751633, 0.0092238271, NA, 
0.001528698, 0.1824065, -0.007178863, 0.01229887, NA, -0.116924816, 
0.1805862599, 0.0067286533, 0.0304822783, NA), Standard.Error = c(0.0042631555, 
0.0039966993, 0.0040101849, 0.0041433122, NA, 0.0059875456, 0.0043898351, 
0.0043814963, 0.0059643889, NA, 0.004456627, 0.0042164649, 0.0042192539, 
0.0041970341, NA, 0.0049525205, 0.0043946299, 0.0047202569, 0.0047586089, 
NA, 0.004712734, 0.0044467898, 0.0044310905, 0.0046230496, NA, 
0.0055383474, 0.005436978, 0.0054572147, 0.0054341941, NA, 0.0077533177, 
0.0053896156, 0.0054924567, 0.007460565, NA, 0.0032629111, 0.0030114308, 
0.0030109788, 0.0031728141, NA, 0.0042602873, 0.0032867379, 0.003267699, 
0.0042434557, NA, 0.0040536565, 0.0032199786, 0.0032208938, 0.0038851264, 
NA, 0.0040909885, 0.0032807477, 0.0033939093, 0.0039636985, NA, 
0.0033543027, 0.00329436, 0.0032935824, 0.0032833201, NA, 0.004650396, 
0.0042576777, 0.0042474218, 0.004569017, NA, 0.0066563262, 0.0042098678, 
0.0042211134, 0.0064479665, NA, 0.0032705315, 0.0030154972, 0.0030057403, 
0.0031829444, NA, 0.0050058966, 0.0032664303, 0.0032498159, 0.0049974453, 
NA, 0.0033630348, 0.0032109432, 0.0031949128, 0.0031599641, NA, 
0.0034515884, 0.0033183009, 0.0035137043, 0.0032969678, NA, 0.0033513282, 
0.0033242799, 0.0033094765, 0.003277501, NA, 0.0047037403, 0.0042207196, 
0.0042209977, 0.0046260399, NA, 0.0055754598, 0.0042016494, 0.0042540468, 
0.0053314262, NA, 0.0037921467, 0.003546833, 0.0035654461, 0.0036869564, 
NA, 0.0053083414, 0.0038543085, 0.0038431418, 0.0052894177, NA, 
0.003953834, 0.0037460272, 0.0037464006, 0.0037182243, NA, 0.0040595846, 
0.0038877666, 0.0040953111, 0.0038750877, NA, 0.0040095905, 0.0039234045, 
0.0039192394, 0.0039265844, NA, 0.0054859283, 0.0049306632, 0.0049408194, 
0.0053950349, NA, 0.0070745859, 0.0048685424, 0.0049473302, 0.0068144155, 
NA), Test.Statistic = c(-14.6325432, 65.4087058, -2.2020096, 
3.0167298, NA, -15.87828823, 58.30408476, 2.1288925, 1.96373131, 
NA, 3.7337401, 58.3666342, 0.6947851, 5.4083363, NA, 13.61926132, 
57.13315027, 0.75279943, 3.97559213, NA, 2.0037122, 58.2814076, 
-2.1358564, 2.3171676, NA, -4.19092599, 40.07067349, -1.57252832, 
2.3529033, NA, -10.104918018, 40.192804853, 1.540535134, 4.586396952, 
NA, -17.9607052, 55.1145268, 0.4937602, 2.1589051, NA, -16.05868043, 
48.35441941, -0.2460893, 0.4491268, NA, 10.62940655, 48.68681256, 
0.96989462, 3.89186294, NA, 27.63653098, 47.68051823, -0.11927883, 
1.93327911, NA, 9.2099876, 48.90463912, -0.97751512, 1.91019087, 
NA, 7.2948377, 31.8922315, -0.2569463, 2.1803344, NA, -16.4024114, 
31.6916634, 1.9603437, 3.3706556, NA, -16.26995919, 66.14298493, 
-2.29789488, 3.466153, NA, -20.17418224, 59.16361186, 0.83478941, 
1.45031947, NA, 6.7662062, 59.5515515, 0.7881515, 3.9533574, 
NA, 17.10958442, 57.5652388, -0.12112599, 3.53732627, NA, 5.023666, 
59.5772978, -3.0401685, 1.7756516, NA, -5.49563695, 39.26583735, 
-0.83887751, 1.00244764, NA, -14.96076654, 39.14054213, 0.27271247, 
4.22802821, NA, -18.3273684, 63.7880941, -1.0061217, 3.313685, 
NA, -20.60633113, 55.88553101, 0.74348313, 1.88776935, NA, 10.02717967, 
56.44467649, 0.61265298, 5.80927926, NA, 26.1065469, 54.9351698, 
0.9449374, 2.9281691, NA, 6.3296237, 56.2710977, -1.1418448, 
2.3490714, NA, 0.27865802, 36.99431935, -1.45297024, 2.27966501, 
NA, -16.527443, 37.0924695, 1.3600575, 4.4732051, NA), P.value = c(2.138749e-48, 
0, 0.02766868, 0.002556318, 0.002, 1.258141e-56, 0, 0.03326834, 
0.04956714, 0.03, 0.0001888682, 0, 0.4871933, 6.390652e-08, 0, 
3.69907e-42, 0, 0.4515742, 7.030912e-05, 0.00025, 0.04510662, 
0, 0.03269626, 0.02049887, 0.022, 2.786758e-05, 0, 0.1158395, 
0.0186344, 0.025, 5.779481e-24, 0, 0.1234413, 4.529257e-06, 2e-05, 
6.296266e-72, 0, 0.6214775, 0.03086175, 0.017, 7.089679e-58, 
0, 0.8056142, 0.6533423, 0.954, 2.321999e-26, 0, 0.3321038, 9.960751e-05, 
7e-05, 8.762439e-167, 0, 0.905055, 0.05320781, 0.023, 3.391516e-20, 
0, 0.3283192, 0.05611471, 0.056, 3.070117e-13, 3.061332e-219, 
0.7972222, 0.02924104, 0.034, 3.525826e-60, 1.447669e-216, 0.04996561, 
0.0007509156, 0.001, 2.215979e-59, 0, 0.02157148, 0.0005283629, 
0.00064, 4.030583e-90, 0, 0.4038406, 0.1469762, 0.141, 1.336906e-11, 
0, 0.4306119, 7.71707e-05, 7e-05, 1.999871e-65, 0, 0.9035918, 
0.0004045942, 0.00032, 5.088211e-07, 0, 0.002365775, 0.07579691, 
0.087, 3.927641e-08, 0, 0.4015454, 0.3161363, 0.315, 2.095113e-50, 
0, 0.7850763, 2.365113e-05, 4e-05, 8.279006e-75, 0, 0.3143614, 
0.0009213338, 0.00125, 6.275027e-94, 0, 0.4571929, 0.05906308, 
0.061, 1.219482e-23, 0, 0.5401086, 6.312492e-09, 0, 3.564116e-149, 
0, 0.3446957, 0.003411279, 0.002, 2.479661e-10, 0, 0.2535243, 
0.01882441, 0.028, 0.7805094, 1.746567e-292, 0.1462433, 0.0226351, 
0.035, 4.555706e-61, 5.449164e-294, 0.1738228, 7.736085e-06, 
5e-05), R_sq = c(0.100865397143315, 0.100865397143315, 0.100865397143315, 
0.100865397143315, NA, 0.107073178974154, 0.107073178974154, 
0.107073178974154, 0.107073178974154, NA, 0.0969133176352893, 
0.0969133176352893, 0.0969133176352893, 0.0969133176352893, NA, 
0.0975421105075435, 0.0975421105075435, 0.0975421105075435, 0.0975421105075435, 
NA, 0.0952375126194014, 0.0952375126194014, 0.0952375126194014, 
0.0952375126194014, NA, 0.0862871099825099, 0.0862871099825099, 
0.0862871099825099, 0.0862871099825099, NA, 0.0958838532501455, 
0.0958838532501455, 0.0958838532501455, 0.0958838532501455, NA, 
0.492841574091161, 0.492841574091161, 0.492841574091161, 0.492841574091161, 
NA, 0.499725679443944, 0.499725679443944, 0.499725679443944, 
0.499725679443944, NA, 0.497783354601557, 0.497783354601557, 
0.497783354601557, 0.497783354601557, NA, 0.494536315222386, 
0.494536315222386, 0.494536315222386, 0.494536315222386, NA, 
0.491139458182066, 0.491139458182066, 0.491139458182066, 0.491139458182066, 
NA, 0.491781984165542, 0.491781984165542, 0.491781984165542, 
0.491781984165542, NA, 0.504545894379456, 0.504545894379456, 
0.504545894379456, 0.504545894379456, NA, 0.503534909721919, 
0.503534909721919, 0.503534909721919, 0.503534909721919, NA, 
0.508038672984895, 0.508038672984895, 0.508038672984895, 0.508038672984895, 
NA, 0.499516269784387, 0.499516269784387, 0.499516269784387, 
0.499516269784387, NA, 0.503140516739568, 0.503140516739568, 
0.503140516739568, 0.503140516739568, NA, 0.501412976625901, 
0.501412976625901, 0.501412976625901, 0.501412976625901, NA, 
0.491414493335579, 0.491414493335579, 0.491414493335579, 0.491414493335579, 
NA, 0.499276463956527, 0.499276463956527, 0.499276463956527, 
0.499276463956527, NA, 0.293359186738059, 0.293359186738059, 
0.293359186738059, 0.293359186738059, NA, 0.308193711470299, 
0.308193711470299, 0.308193711470299, 0.308193711470299, NA, 
0.301429071351266, 0.301429071351266, 0.301429071351266, 0.301429071351266, 
NA, 0.288935043551192, 0.288935043551192, 0.288935043551192, 
0.288935043551192, NA, 0.287835593326278, 0.287835593326278, 
0.287835593326278, 0.287835593326278, NA, 0.294130663800099, 
0.294130663800099, 0.294130663800099, 0.294130663800099, NA, 
0.311267603228644, 0.311267603228644, 0.311267603228644, 0.311267603228644, 
NA), R_sq.P.value = c(0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 0, 0, 0, 
0, NA, 0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 0, 0, 
0, 0, NA, 0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 0, 
0, 0, 0, NA, 0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 
0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 
0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 
0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 0, 0, 0, 0, NA, 
0, 0, 0, 0, NA, 0, 0, 0, 0, NA), Var.R_sq = c(5.806319e-06, 5.806319e-06, 
5.806319e-06, 5.806319e-06, NA, 7.262982e-06, 7.262982e-06, 7.262982e-06, 
7.262982e-06, NA, 6.377753e-06, 6.377753e-06, 6.377753e-06, 6.377753e-06, 
NA, 6.749924e-06, 6.749924e-06, 6.749924e-06, 6.749924e-06, NA, 
6.620368e-06, 6.620368e-06, 6.620368e-06, 6.620368e-06, NA, 1.035739e-05, 
1.035739e-05, 1.035739e-05, 1.035739e-05, NA, 1.126859e-05, 1.126859e-05, 
1.126859e-05, 1.126859e-05, NA, 9.026057e-06, 9.026057e-06, 9.026057e-06, 
9.026057e-06, NA, 1.064034e-05, 1.064034e-05, 1.064034e-05, 1.064034e-05, 
NA, 1.01305e-05, 1.01305e-05, 1.01305e-05, 1.01305e-05, NA, 1.073528e-05, 
1.073528e-05, 1.073528e-05, 1.073528e-05, NA, 1.079935e-05, 1.079935e-05, 
1.079935e-05, 1.079935e-05, NA, 1.826135e-05, 1.826135e-05, 1.826135e-05, 
1.826135e-05, NA, 1.780604e-05, 1.780604e-05, 1.780604e-05, 1.780604e-05, 
NA, 8.964453e-06, 8.964453e-06, 8.964453e-06, 8.964453e-06, NA, 
1.060793e-05, 1.060793e-05, 1.060793e-05, 1.060793e-05, NA, 1.02462e-05, 
1.02462e-05, 1.02462e-05, 1.02462e-05, NA, 1.070066e-05, 1.070066e-05, 
1.070066e-05, 1.070066e-05, NA, 1.073682e-05, 1.073682e-05, 1.073682e-05, 
1.073682e-05, NA, 1.852647e-05, 1.852647e-05, 1.852647e-05, 1.852647e-05, 
NA, 1.824541e-05, 1.824541e-05, 1.824541e-05, 1.824541e-05, NA, 
1.042957e-05, 1.042957e-05, 1.042957e-05, 1.042957e-05, NA, 1.254755e-05, 
1.254755e-05, 1.254755e-05, 1.254755e-05, NA, 1.186818e-05, 1.186818e-05, 
1.186818e-05, 1.186818e-05, NA, 1.241146e-05, 1.241146e-05, 1.241146e-05, 
1.241146e-05, NA, 1.239539e-05, 1.239539e-05, 1.239539e-05, 1.239539e-05, 
NA, 2.106666e-05, 2.106666e-05, 2.106666e-05, 2.106666e-05, NA, 
2.122465e-05, 2.122465e-05, 2.122465e-05, 2.122465e-05, NA), 
    r = c(0.31759313144858, 0.31759313144858, 0.31759313144858, 
    0.31759313144858, NA, 0.327220382883087, 0.327220382883087, 
    0.327220382883087, 0.327220382883087, NA, 0.311309038794715, 
    0.311309038794715, 0.311309038794715, 0.311309038794715, 
    NA, 0.312317323418896, 0.312317323418896, 0.312317323418896, 
    0.312317323418896, NA, 0.30860575597257, 0.30860575597257, 
    0.30860575597257, 0.30860575597257, NA, 0.293746676547174, 
    0.293746676547174, 0.293746676547174, 0.293746676547174, 
    NA, 0.309651179959233, 0.309651179959233, 0.309651179959233, 
    0.309651179959233, NA, 0.702026761663087, 0.702026761663087, 
    0.702026761663087, 0.702026761663087, NA, 0.706912780648323, 
    0.706912780648323, 0.706912780648323, 0.706912780648323, 
    NA, 0.705537635141852, 0.705537635141852, 0.705537635141852, 
    0.705537635141852, NA, 0.703232760344956, 0.703232760344956, 
    0.703232760344956, 0.703232760344956, NA, 0.700813426085763, 
    0.700813426085763, 0.700813426085763, 0.700813426085763, 
    NA, 0.701271690691662, 0.701271690691662, 0.701271690691662, 
    0.701271690691662, NA, 0.710313940718789, 0.710313940718789, 
    0.710313940718789, 0.710313940718789, NA, 0.709601937512799, 
    0.709601937512799, 0.709601937512799, 0.709601937512799, 
    NA, 0.712768316485024, 0.712768316485024, 0.712768316485024, 
    0.712768316485024, NA, 0.706764649501082, 0.706764649501082, 
    0.706764649501082, 0.706764649501082, NA, 0.70932398573541, 
    0.70932398573541, 0.70932398573541, 0.70932398573541, NA, 
    0.708105201665615, 0.708105201665615, 0.708105201665615, 
    0.708105201665615, NA, 0.701009624281706, 0.701009624281706, 
    0.701009624281706, 0.701009624281706, NA, 0.706594978722979, 
    0.706594978722979, 0.706594978722979, 0.706594978722979, 
    NA, 0.541626427289196, 0.541626427289196, 0.541626427289196, 
    0.541626427289196, NA, 0.555151971508973, 0.555151971508973, 
    0.555151971508973, 0.555151971508973, NA, 0.549025565298435, 
    0.549025565298435, 0.549025565298435, 0.549025565298435, 
    NA, 0.537526784031449, 0.537526784031449, 0.537526784031449, 
    0.537526784031449, NA, 0.536503115858872, 0.536503115858872, 
    0.536503115858872, 0.536503115858872, NA, 0.542338145256351, 
    0.542338145256351, 0.542338145256351, 0.542338145256351, 
    NA, 0.557913616278223, 0.557913616278223, 0.557913616278223, 
    0.557913616278223, NA)), row.names = c(NA, 140L), class = "data.frame")

R代码:

library(zoo)
df$Estimate <- zoo::na.locf(df$Estimate) 

df$QT_vs_E <- paste0(df$Outcome, "/", df$Covariate)
colnames(df)[6] <- "estimate"
colnames(df)[9] <- "p"
df$Model <- as.character(df$Model)
head(df)

QTdata0 <- df
QTdata <- QTdata0[QTdata0$Component == "`PRS_gxe x E`", ]


# Create a new column with p-value intervals
QTdata$p_interval <- cut(QTdata$p,
                         breaks = c(0, 0.00001, 0.0001, 0.001282051, 0.05, 1),
                         labels = c("< 0.00001", "0.0001", "0.001", "0.05", "1"),
                         include.lowest = TRUE)

# Define your custom legend values and sizes
legend_data <- data.frame(
  p_value_interval = c("< 0.00001", "0.0001", "0.001", "0.05", "1"),
  size = c(15,13,11,9,8)
)

# Define a labelling function
model_labeller <- function(x) {
  return(paste("Model", x))
}

QTdata$Model <- as.character(QTdata$Model)

library(ggplot2)
#Create the plot
ggplot(data = QTdata, aes(x = QT_vs_E, y = Model, fill = estimate, size = p_interval)) +
  geom_tile(color = "white", fill = "white") +
  geom_point(shape = 22, color = "white", stroke = 0) +
  geom_hline(yintercept = seq_along(unique(QTdata$Model)) + 0.5, color = "grey") +
  geom_vline(xintercept = seq_along(unique(QTdata$QT_vs_E)) + 0.5, color = "grey") +
  geom_vline(xintercept = 0.5, color = "grey")+
  geom_hline(yintercept = 0.5, color = "grey")+
  geom_text(data = subset(QTdata,(p <= 0.001282051)&(p > 0.0001)),
            aes(label = "\u273B"),
            size = 4, vjust = 0.5, hjust = 0.5,
            show.legend = FALSE) +
  geom_text(data = subset(QTdata, (p <= 0.0001)&(p > 0.00001)),
            aes(label = "\u273B\u273B"),
            size = 4, vjust = 0.5, hjust = 0.5,
            show.legend = FALSE) +
  geom_text(data = subset(QTdata, (p <= 0.00001)),
            aes(label = "\u273B\u273B\u273B"),
            size = 4, vjust = 0.5, hjust = 0.5,
            show.legend = FALSE) +
  scale_fill_gradientn(colours = c("red4", "white", "steelblue"),
                       limit = c(-max(abs(QTdata$estimate))-0.000, max(abs(QTdata$estimate))+0.000),
                       guide = guide_colorbar(title = "Estimate",
                                              label.position = "right",
                                              barwidth = 1,
                                              barheight = 8,
                                              title.position = "top",
                                              order = 1)) +
  scale_size_manual(values = legend_data$size,
                    breaks = legend_data$p_value_interval,
                    labels = legend_data$p_value_interval,
                    guide = guide_legend(title = "p value",
                                         label.position = "right",
                                         title.position = "top")) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45,
                                   hjust = 1,
                                   vjust = 1,
                                   size = 12,
                                   colour = "black"),
        axis.text.y = element_text(size=12, color = "black"),
        axis.title.y = element_text(size = 12),
        legend.text = element_text(size = 12),
        legend.title = element_text(size = 12),
        legend.key.width = unit(0.01, "cm"),
        legend.key.height = unit(0.01, "cm"),
        legend.box = "horizontal",
        legend.spacing.y = unit(0.5, "cm"),
        legend.position = "right",
        legend.box.margin = margin(b=-10, t=30, l=-1, r=11),
        legend.box.just = ("center"),
        plot.margin = margin(b=5, t=20, l=5, r=5)) +
  labs(x = "", y = "Model", fill = "Estimate", size = "p value") +
  guides(size = guide_legend(reverse = F,
                             label.position = "right",
                             title.position = "top",
                             override.aes = list(colour="grey",
                                                 shape=15,
                                                 fill="white",
                                                 size=legend_data$size-2)))

我需要的图像(请注意,我已使用 Paint 手动在 p 值图例中添加了星号):

我得到的图像(当我更新 RStudio 时,对于相同的代码我无法控制 p 值图例高度):

当我最大化绘图面板时,我可以看到完整的 p 值图例,但它拉伸了整个热图,这不是我想要的。请参阅下面的拉伸图像:

我需要知道如何修改我的代码,以便我可以正确缩放图例高度(就像我将条形高度设置为估计颜色条一样),以获得与第一个最相似的图像(图像1 ).

r ggplot2 scale legend-properties
1个回答
0
投票

我相信

geom_tile(...)
导致了这个问题。我不知道为什么,但如果你删除它,size图例应该更美观。

ggplot(data = QTdata, aes(x = QT_vs_E, y = Model, fill = estimate, size = p_interval)) +
  # geom_tile(color = "white", fill = "white") +
  geom_point(shape = 22, color = "white", stroke = 0) +
  geom_hline(yintercept = seq_along(unique(QTdata$Model)) + 0.5, color = "grey") + ...

© www.soinside.com 2019 - 2024. All rights reserved.