I have this heatplot that I've generated using ggplot2.
这是我用ggplot2生成的热图。
What I would like to do is to add white space between the variables listed in capital letters and the variables listed in lower case letters so as to separate these two categories. I still want the tiles to be of equal width.
我想做的是在大写字母和小写字母中列出的变量之间添加空白,以便区分这两类。我还是想让瓷砖的宽度相等。
The code I've used to generate the plot is:
我用来生成情节的代码是:
ggplot(mockdata, aes(variable, Measurement)) +
geom_tile(aes(fill = mockdata$plotval), colour = "dark red") + scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") +
theme_minimal() + theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28)) +
labs(title="", x="", y="", fill="")
And my data looks as follows:
我的数据如下:
> head(mockdata) Measurement variable Pval effect
> direction plotval category 21 20 A 4.511987e+04
> -0.004892941 -1 -4.6543678 Measured 22 19 A 3.436853e-08 0.054344854 1 7.4638390 Measured 23 18 A 1.465755e+00 0.355139910 1 -0.1660613 Measured 24
> 17 A 7.006222e-04 -0.079390247 -1 3.1545161 Measured 25
> 16 A 4.705051e-04 -0.017708611 -1 3.3274357 Measured 26
> 15 A 2.301113e+03 -0.022934623 -1 -3.3619379 Measured
PS. While it is strictly another question, I would also like to edit the legend so that it ranges simply from 0-20 and is split in two vertically, showing on one side the red gradient and the blue gradient on the other.
另外,虽然这完全是另一个问题,但我也想编辑图例,使其范围仅为0-20,并且垂直地一分为二,一边显示红色渐变,另一边显示蓝色渐变。
Thank you very much for your help.
非常感谢你的帮助。
Edit: dput output as requested
按要求编辑:dput输出。
dput(mockdata)
structure(list(Measurement = structure(c(20L, 19L, 18L, 17L,
16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L,
2L, 1L, 20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L,
9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 20L, 19L, 18L, 17L, 16L,
15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L,
1L, 20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L,
8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 20L, 19L, 18L, 17L, 16L, 15L,
14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L,
20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L,
7L, 6L, 5L, 4L, 3L, 2L, 1L, 20L, 19L, 18L, 17L, 16L, 15L, 14L,
13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 20L,
19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L,
6L, 5L, 4L, 3L, 2L, 1L, 20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L,
12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L), .Label = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35",
"36", "37", "38", "39", "40"), class = "factor"), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L), .Label = c("A", "B", "C", "a", "b", "c", "d", "e",
"f"), class = "factor"), Pval = c(45119.8699435164, 3.43685309800565e-08,
1.46575455003134, 0.000700622204341352, 0.000470505115493356,
2301.1126328306, 4.54797845872279e-09, 2.82713540460051, 7.27455376527109,
0.000708124149686762, 2.42898037459018, 129.848099736546, 0.199979430821282,
3.14108095159057, 1.27073870790998, 107.060952494286, 5853.27997745224,
0.0742582348172777, 0.179994821770633, 896.77644657471, 23.2401456908433,
1.04257158400361e-05, 0.818717078694074, 0.000516863241751892,
0.445544049614064, 37.9699212955842, 4.87750761132064, 0.138732186303325,
0.0185924820003653, 30.8203248121754, 2.40190754411345, 61.2579350060232,
6.44618209352764, 1047821.38958175, 704454.528072486, 1184.40676026588,
9.27799786552194, 244051.746539605, 375048.779613815, 9134.54101073437,
3.11905288061898e-08, 2.00150860109179e-05, 7.0931638758186e-05,
2.43242155771004, 1602.73424833609, 6.02923321145412, 3.21794638623681,
39545.9388361236, 475.781444413131, 455.686197237482, 4.67829129583755,
909.58298359142, 2.02989433872695e-12, 0.000760836775114312,
0.000112864936813818, 0.00189775331609849, 0.0815921243521161,
31.0427655620544, 64.1785732227039, 0.00217933417639979, 0.32663419999951,
2.54891270451988e-07, 8.48612397192046e-08, 0.370184441863085,
0.0773444299723239, 1580.93083600716, 25.4862794021994, 30.2642707469218,
224.214639928028, 14.7414363362227, 18.0511919451453, 21037.258121794,
21659.968905852, 17721957.0100965, 41240.6588986806, 111.028537460958,
21.7975275086975, 276.44178388089, 171.535750708364, 6.54776065462672,
151.823636593007, 77.7232628471612, 378439.528032194, 8.9932059191815e-10,
7.49689679316569e-08, 0.0155924907385915, 1032.31959077079, 69.010587250471,
3136.70323628637, 249713.871697334, 19360.5708508639, 40.7258690163879,
0.0162957051889601, 1.02869393872855e-06, 0.025268197376688,
0.528481476061038, 0.982629085096339, 9.45655313066933e-07, 4.57557582847333e-05,
7.45403294058092e-05, 4.53918933391971, 3.30530837062386e-13,
0.630234341296706, 9.82668496529192e-08, 8.53389810467837e-08,
9.02891336492619, 5451.02888390797, 3.90681590706977, 329.545150676593,
103.491400305633, 697.038514954349, 222.73520900148, 10798.3042798457,
21.0644243649671, 43176.6607108169, 4235.53654423626, 3100.32036580987,
0.474678829666085, 6.40621457829452, 122.637549472055, 43.0666245228938,
12.0250322640284, 61440.278302552, 6788575.58160297, 51.9360282881212,
769.96899739087, 7.59005145509618e-09, 2.43526615569376e-14,
7.39725489778816e-07, 0.000232784090617743, 2.71678851958226e-05,
7.42801688358956e-06, 0.000419885957835749, 13.5780839769886,
3.32442709394189, 0.335486082296544, 3.94230896978352, 1.46425278783307,
8.69549979650646, 596385.229504125, 0.000115943040096764, 3628.23773839244,
49454.7726336256, 0.000504974333790079, 0.00995546823577904,
0.233933121119438, 0.363364289185792, 9193.93256769758, 354.456908896329,
5661.68504768727, 0.121589829029783, 940.93343158607, 2.03003608173289e-18,
0.0149649659043151, 4.52682955802236e-10, 7.15334079727642e-09,
0.000310851843411754, 1.93389728243117e-06, 2.03883508956486e-07,
0.00284517176384008, 540.061334125012, 21318282.1015595, 14894.4660549342,
0.000266471541489485, 0.246672590081899, 0.102617416010794, 30584.5294303485,
1.6644151457974, 0.274144521451231, 5.61250055231602, 801087.819256331,
649321.765215902, 1.09526223361541, 3.19905699111348e-05, 0.383890848761449,
895.643897487244, 2.68459116542856, 26.4804232740359, 5.94041739131986,
1.53119475830081e-07), effect = c(-0.0048929408170183, 0.0543448544236634,
0.355139910284011, -0.0793902472151885, -0.0177086113171934,
-0.0229346227548993, 0.0859476877708055, 0.0616530901499479,
0.0255808327961239, 0.0587050814255305, 0.0141476490245787, 0.016845032477233,
0.0534824934671667, 0.109055235276048, 0.111112652283139, 0.0234941463950778,
0.0509358202565552, 0.0252735688725701, 0.0359067099796669, 0.00562271637183446,
0.0507704293161921, 0.17283228087345, 0.16140675832248, -0.0818286747635847,
-0.00755469414427661, -0.0104447567093411, -0.128088271682644,
-0.0534210973538898, -0.373370146687058, -0.0158548424704536,
-0.0748163315974356, -0.0904460324042191, -0.0028512716452736,
0.014271561838011, -0.00190156753813359, -0.0239279177026479,
-0.0094336807819733, 0.0222425549129021, 0.0292506342181443,
0.0325754202368439, 0.0148015986703938, 0.0421882264651577, 0.423260142208342,
0.0203382101649578, 0.00402969463133637, 0.0866038959569813,
-0.0126180724522637, -0.00101624925914612, 0.0424729539424711,
0.0109268890743347, -0.100768815837666, -0.00606344260837149,
-0.0909174554780339, -0.0959700647301915, -0.0768705808220001,
-0.0662807953036616, -0.00706464147110935, -0.0260825101925435,
-0.116360501719279, -0.0685817739681672, 0.0190371136722137,
0.314176452865903, 0.0852019981707266, -0.109955683402951, -0.0429494359040651,
-0.018893257986317, 0.0359280715736875, -0.00777719486360404,
0.0268854711195253, 0.035717165605315, -0.0185632636448169, 0.00219893106540051,
-0.0575168050187894, -0.0268860255228866, -0.0120997481434518,
-0.0174770295188239, -0.0365387268315564, 0.00427667014560775,
0.0290413811465955, -0.0303874959875708, 0.0494496371834744,
-0.0172878730121681, -0.0596619493875312, 0.0674472930186462,
0.0581172695200586, 0.0352865160196343, -0.0390338901505756,
0.0201101954340249, 0.0968644367520139, -0.00587675128187206,
-0.0990223012750421, -0.022532243059551, -0.109508938534005,
-0.0828800247402709, -0.0224991372217077, -0.0864470271893071,
-0.0120276456354962, -0.171616504240665, -0.0405716645109222,
-0.150207882114913, 0.019187309212901, 0.0583618216547288, 0.312977089073896,
-0.116708039798045, -0.0727212548658722, -0.0334904589242441,
0.0250211767009604, -0.0262773156800457, -0.00638328497889965,
0.0207436409328599, 0.00526977878443117, 0.0121248749949723,
0.0372084648778052, 0.0119773388777849, 0.0299841410899367, 0.0179852238184209,
0.00615922907815808, 0.0923011852715508, 0.0286655081413075,
0.0615892570124506, -0.040101427234219, 0.00471273799312188,
-0.0695325837078327, -0.0194435878635383, -0.0113447270023325,
-0.052896201189637, 0.15391622949438, 0.0625317280746324, 0.305195811975115,
0.0175140430499281, 0.0944330305254164, 0.125486034995771, 0.204097960624017,
0.0139041133925463, 0.115503245160239, 0.0744749299835659, 0.0309401153645095,
0.0774734185854047, 0.128577840642144, -0.00523940764293137,
-0.0826725654792668, 0.00061718469555555, -0.139447399226457,
-0.015892803158233, -0.0776351210087576, -0.114375795737603,
0.506655172546014, 0.0139569906287469, 0.032593809131997, 0.0838209562382717,
0.026889024005808, 0.114847700394033, 0.0344957168306127, 0.309849172983949,
0.224583305802426, 0.0920680732068372, 0.20205450223499, 0.129261938040113,
0.118741527574873, 0.0666598681717314, -0.0259685736746482, -0.00493768230442141,
-0.0513043909367941, -0.0228292902998043, -0.125120892334107,
-0.0354061460089046, -0.145910823420589, -0.0505094213129978,
-0.357522550450534, -0.0492725484140125, -0.00672879449498781,
-0.00498836470563034, 0.0857101915123005, 0.245202106389044,
0.0652610111098569, 0.00803793757485241, 0.0456354806905821,
0.0567597911088563, 0.155069881077418, 0.0398552868265476), direction = c(-1,
1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1,
1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1,
-1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1,
1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1,
-1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1,
-1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1),
plotval = c(-4.65436783913808, 7.4638390305887, -0.166061251006543,
3.15451610300161, 3.32743565041216, -3.36193787665198, 8.34218160099397,
-0.45134660936061, -0.861806358039495, 3.14989059429165,
-0.385424005854277, -2.11343559832442, 0.699014672037075,
-0.497079129132047, -0.104056259146589, -2.02963110287305,
-3.7673992980343, 1.12925537854411, 0.744739988834407, -2.95268419318977,
-1.36623884628847, 4.98189411623867, 0.0868661499973576,
3.28662435286223, 0.351109352101632, -1.57943969693098, -0.688197955780953,
0.857822769551858, 1.73066263028667, -1.48883721139058, -0.380556286225031,
-1.78716235272839, -0.809302569419764, -6.02028725962301,
-5.84785296501876, -3.073500877557, -0.967454268269813, -5.38748192000237,
-5.57408775663275, -3.96068672995749, 7.50597726212444, 4.69864253925926,
4.14916000637992, -0.386038843759309, -3.20486151733282,
-0.780262082752201, -0.507578804102515, -4.59710189032113,
-2.67740750044583, -2.65866587397551, -0.670087259849411,
-2.95884232700787, 11.6925265676543, 3.11870850390026, 3.94744095721937,
2.72176024102353, 1.08835175932808, -1.49196040507446, -1.8073900579076,
2.66167617047025, 0.485938344726535, 6.59364503804761, 7.0712906280104,
0.431581837757606, 1.11157095694052, -3.19891287043981, -1.40630643990113,
-1.48093021354005, -2.35066396611971, -1.16853980119637,
-1.25650588425321, -4.32298913566366, -4.3356578288347, -7.24851167871111,
-4.61532559535481, -2.04543461904724, -1.33840723441989,
-2.4416036868115, -2.23435464751167, -0.816092795927033,
-2.18133938975974, -1.89055102413018, -5.57799649218455,
9.04608546234764, 7.12511846787433, 1.80708450523289, -3.01381416921109,
-1.83891572308132, -3.49647343200987, -5.39744266823951,
-4.28691815843339, -1.6098703601382, 1.7879268409032, 5.98771381910823,
1.59742573937338, 0.276970230656392, 0.00761038521452666,
6.02426713306055, 4.33955424293266, 4.12760869236016, -0.656978297958022,
12.4807880165375, 0.200497935929844, 7.00759296681644, 7.06885254695129,
-0.955635485860221, -3.73647848327309, -0.591822947332438,
-2.51791492529225, -2.01490426327141, -2.84325677575944,
-2.34778887382083, -4.03335556106781, -1.32354959550492,
-4.63524905089898, -3.62690843318892, -3.49140657316158,
0.323600137070818, -0.806601481409993, -2.08862346391581,
-1.63414083395139, -1.08008625029083, -4.78845317422705,
-6.83177865764632, -1.71546873469442, -2.88647323876581,
8.11975527988995, 13.6134535668385, 6.13092941593436, 3.63304670440456,
4.56594416664396, 5.1291271177969, 3.37686864914492, -1.13283849034736,
-0.521716813134629, 0.474325491909851, -0.595750658150867,
-0.165616059566562, -0.939294549263021, -5.77552687887169,
3.93575531635149, -3.55969573624066, -4.6942082095516, 3.29673069510235,
2.0019383089733, 0.630908284813054, 0.439657756641714, -3.96350131412125,
-2.54956344582082, -3.75294570645333, 0.915102752209537,
-2.97355889939168, 17.6924962428955, 1.82492426830489, 9.34420585704084,
8.14549108393769, 3.50744655283763, 5.71356659688074, 6.6906179005526,
2.54589150999432, -2.732443084933, -7.32875220486331, -4.17302493900107,
3.57434916575198, 0.607879106033299, 0.98877892542788, -4.48550180288597,
-0.221261659119412, 0.562020428705443, -0.749156396749516,
-5.90368012822885, -5.81245996063456, -0.039518112762786,
4.49497802290257, 0.415792240525927, -2.95213537116121, -0.428878156658466,
-1.42292492276573, -0.773816960869839, 6.81496956627369),
category = c("Measured", "Measured", "Measured", "Measured",
"Measured", "Measured", "Measured", "Measured", "Measured",
"Measured", "Measured", "Measured", "Measured", "Measured",
"Measured", "Measured", "Measured", "Measured", "Measured",
"Measured", "Measured", "Measured", "Measured", "Measured",
"Measured", "Measured", "Measured", "Measured", "Measured",
"Measured", "Measured", "Measured", "Measured", "Measured",
"Measured", "Measured", "Measured", "Measured", "Measured",
"Measured", "Measured", "Measured", "Measured", "Measured",
"Measured", "Measured", "Measured", "Measured", "Measured",
"Measured", "Measured", "Measured", "Measured", "Measured",
"Measured", "Measured", "Measured", "Measured", "Measured",
"Measured", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived", "Derived", "Derived", "Derived", "Derived", "Derived",
"Derived")), .Names = c("Measurement", "variable", "Pval",
"effect", "direction", "plotval", "category"), row.names = c(21L,
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L,
35L, 36L, 37L, 38L, 39L, 40L, 61L, 62L, 63L, 64L, 65L, 66L, 67L,
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L,
101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L,
112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 141L, 142L,
143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L,
154L, 155L, 156L, 157L, 158L, 159L, 160L, 181L, 182L, 183L, 184L,
185L, 186L, 187L, 188L, 189L, 190L, 191L, 192L, 193L, 194L, 195L,
196L, 197L, 198L, 199L, 200L, 221L, 222L, 223L, 224L, 225L, 226L,
227L, 228L, 229L, 230L, 231L, 232L, 233L, 234L, 235L, 236L, 237L,
238L, 239L, 240L, 261L, 262L, 263L, 264L, 265L, 266L, 267L, 268L,
269L, 270L, 271L, 272L, 273L, 274L, 275L, 276L, 277L, 278L, 279L,
280L, 301L, 302L, 303L, 304L, 305L, 306L, 307L, 308L, 309L, 310L,
311L, 312L, 313L, 314L, 315L, 316L, 317L, 318L, 319L, 320L, 341L,
342L, 343L, 344L, 345L, 346L, 347L, 348L, 349L, 350L, 351L, 352L,
353L, 354L, 355L, 356L, 357L, 358L, 359L, 360L), class = "data.frame")
1 个解决方案
#1
3
Option 1
Use facets
使用方面
mockdata$type = ifelse(mockdata$variable %in% LETTERS[1:3], "1", "2")
ggplot(mockdata) +
facet_wrap(~ type, scales='free_x') +
geom_tile(aes(variable, Measurement, fill = mockdata$plotval), colour = "dark red") +
scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") +
theme_minimal() +
theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28)) +
labs(title="", x="", y="", fill="")
Although this is not quite perfect, because dropping the unused x-axis categories from each facet requires specifying scales='free_x'
- which unfortunately does not keep the boxes the same width in each facet.
虽然这不是很完美,因为从每个方面删除未使用的x轴类别需要指定scale ='free_x'——不幸的是,这并不能使每个方面的方框保持相同的宽度。
Option2
To remedy this we can also set space="free_x"
in facet_grid. Which I think gives what you are looking for:
为了解决这个问题,我们还可以在facet_grid中设置space="free_x"。我认为这就是你想要的
ggplot(mockdata) +
facet_grid(~ type, scales='free_x', space="free_x") +
geom_tile(aes(variable, Measurement, fill = mockdata$plotval), colour = "dark red") +
scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") +
theme_minimal() +
theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28)) +
labs(title="", x="", y="", fill="")
Option 3
An alternative solution is to create separate plots for each category, and arrange them together. Here I put them together using cowplot::plot_grid
, which allows us to set the relative widths how we want them (although you may need to tweak the rel_widths values a bit to get it just right):
另一种解决方案是为每个类别创建单独的情节,并将它们排列在一起。在这里,我使用cowplot::plot_grid将它们放在一起,它允许我们按照我们希望的方式设置相对宽度(尽管您可能需要稍微调整rel_widths值以使其正确):
library(cowplot)
p1 <- ggplot(mockdata[which(mockdata$type=="1"),]) +
geom_tile(aes(variable, Measurement, fill = plotval), colour = "dark red") +
scale_fill_gradient2(limits=c(-20, 20), high = "firebrick3", low = "dodgerblue4") +
theme_minimal() +
theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28),
legend.position="none") +
labs(title="", x="", y="", fill="")
p2 <- ggplot(mockdata[which(mockdata$type=="2"),]) +
geom_tile(aes(variable, Measurement, fill = plotval), colour = "dark red") +
scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") +
theme_minimal() +
theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_blank()) +
labs(title="", x="", y="", fill="")
plot_grid(p1,p2, nrow = 1, rel_widths = c(1,2))
Option 4
You can create an extra factor level in the data, which lies between the columns you want to separate, and then plot tiles for that column in white.
您可以在数据中创建一个额外的因子级别,它位于要分离的列之间,然后用白色绘制该列的块。
mockdata = rbind(mockdata,
data.frame(Measurement=1:20,
variable="",
Pval = NA,
effect = NA,
direction = NA,
plotval = 0,
category = "Measured"))
mockdata$variable = factor(mockdata$variable, levels = c("A", "B", "C", "", "a", "b", "c", "d", "e","f"))
ggplot(mockdata, aes(variable, Measurement)) +
geom_tile(fill = NA, colour = NA) +
geom_tile(data = mockdata[which(mockdata$variable==""),], fill = "white", colour = "white") +
geom_tile(data = mockdata[which(mockdata$variable!=""),], aes(fill = plotval), colour = "dark red") +
scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") +
theme_minimal() +
theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28)) +
labs(title="", x="", y="", fill="")
#1
3
Option 1
Use facets
使用方面
mockdata$type = ifelse(mockdata$variable %in% LETTERS[1:3], "1", "2")
ggplot(mockdata) +
facet_wrap(~ type, scales='free_x') +
geom_tile(aes(variable, Measurement, fill = mockdata$plotval), colour = "dark red") +
scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") +
theme_minimal() +
theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28)) +
labs(title="", x="", y="", fill="")
Although this is not quite perfect, because dropping the unused x-axis categories from each facet requires specifying scales='free_x'
- which unfortunately does not keep the boxes the same width in each facet.
虽然这不是很完美,因为从每个方面删除未使用的x轴类别需要指定scale ='free_x'——不幸的是,这并不能使每个方面的方框保持相同的宽度。
Option2
To remedy this we can also set space="free_x"
in facet_grid. Which I think gives what you are looking for:
为了解决这个问题,我们还可以在facet_grid中设置space="free_x"。我认为这就是你想要的
ggplot(mockdata) +
facet_grid(~ type, scales='free_x', space="free_x") +
geom_tile(aes(variable, Measurement, fill = mockdata$plotval), colour = "dark red") +
scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") +
theme_minimal() +
theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28)) +
labs(title="", x="", y="", fill="")
Option 3
An alternative solution is to create separate plots for each category, and arrange them together. Here I put them together using cowplot::plot_grid
, which allows us to set the relative widths how we want them (although you may need to tweak the rel_widths values a bit to get it just right):
另一种解决方案是为每个类别创建单独的情节,并将它们排列在一起。在这里,我使用cowplot::plot_grid将它们放在一起,它允许我们按照我们希望的方式设置相对宽度(尽管您可能需要稍微调整rel_widths值以使其正确):
library(cowplot)
p1 <- ggplot(mockdata[which(mockdata$type=="1"),]) +
geom_tile(aes(variable, Measurement, fill = plotval), colour = "dark red") +
scale_fill_gradient2(limits=c(-20, 20), high = "firebrick3", low = "dodgerblue4") +
theme_minimal() +
theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28),
legend.position="none") +
labs(title="", x="", y="", fill="")
p2 <- ggplot(mockdata[which(mockdata$type=="2"),]) +
geom_tile(aes(variable, Measurement, fill = plotval), colour = "dark red") +
scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") +
theme_minimal() +
theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_blank()) +
labs(title="", x="", y="", fill="")
plot_grid(p1,p2, nrow = 1, rel_widths = c(1,2))
Option 4
You can create an extra factor level in the data, which lies between the columns you want to separate, and then plot tiles for that column in white.
您可以在数据中创建一个额外的因子级别,它位于要分离的列之间,然后用白色绘制该列的块。
mockdata = rbind(mockdata,
data.frame(Measurement=1:20,
variable="",
Pval = NA,
effect = NA,
direction = NA,
plotval = 0,
category = "Measured"))
mockdata$variable = factor(mockdata$variable, levels = c("A", "B", "C", "", "a", "b", "c", "d", "e","f"))
ggplot(mockdata, aes(variable, Measurement)) +
geom_tile(fill = NA, colour = NA) +
geom_tile(data = mockdata[which(mockdata$variable==""),], fill = "white", colour = "white") +
geom_tile(data = mockdata[which(mockdata$variable!=""),], aes(fill = plotval), colour = "dark red") +
scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") +
theme_minimal() +
theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28)) +
labs(title="", x="", y="", fill="")