产生图的代码如下:
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="")
从图表和代码中都可以看出,颜色方案的范围是-20到20.我的数据中的plotval实际上代表了P值的-log10,所以总是一个正数,但我把它拆分了根据效果的方向(mockdata中的方向列)将其乘以1或-1,分为正数和负数。
我想要做的是将渐变色条分成两个,使其范围从0到20,但我仍然希望每个瓷砖为红色或蓝色,我想要蓝色和红色的强度可以在colorbar上并排看到。我还想增加颜色条的大小,但我想这绝对是另一个问题
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", "41", "42"), class = "factor"),
category = structure(c(3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 3L, 4L, 4L, 4L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L,
8L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 3L, 4L, 4L, 4L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L,
8L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 3L, 4L, 4L, 4L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L,
8L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 8L), .Label = c("x1", "x2", "x3", "x4",
"x5", "x6", "x7", "x8", "x9"), 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(6760.4856938748, 2.03462777623711e-09, 41.184282929783,
0.0391464204769019, 0.305206224642095, 0.000215483198859283,
6.32953239686189e-08, 101.908214964603, 1172.4434810623,
0.256516710363617, 3.19424561429881, 2447.68986203703, 0.00034685390841221,
0.0766588456064384, 22877715.3007328, 9841.61882613746, 3652.68781929919,
51891.7266692688, 0.194178098258849, 1026.89550294706, 7389.35002875074,
8.72167728953903e-05, 0.615710674200898, 90613.5488833331,
0.0001007185458695, 0.329336214710806, 58860.1232768614,
0.000150880187013758, 0.00043394288165634, 589.188126109299,
13.3763683418565, 467886.366054549, 248.393646104426, 189074.428816151,
303206.257085955, 204105.426845067, 6538706.25322294, 5.70435826167575,
1223246.72689726, 98.8553275302565, 3.01666336637105e-10,
0.000421841985089921, 0.00158775289621832, 889227.11315509,
6.7862868179393, 11.0180525555011, 458.619294036059, 387926.980522322,
1367759.72124845, 127.106985922082, 21709768.964503, 249.583742407546,
494318.314688319, 6.84445784041661e-05, 0.0181257313976045,
2.85258646415982e-06, 0.0475591361913438, 0.000132886078114762,
530521.346229848, 0.220839329870672, 0.0796092084099546,
2.16536480257164e-05, 4.27202841139916e-07, 0.442895659664418,
0.241251203711246, 0.0679836008245645, 3.24194712523739,
12.3229213534339, 4.00236477738573, 3.65965017262511, 12.6636282554106,
484.9507381133, 33.498634194894, 9133015.34747621, 854.088489555501,
10.5754804155603, 251.152169076775, 66.5138965438843, 7.31026529212285,
12.5369593061199, 883.371244544336, 2188355.36818768, 6.82570529049995,
7.51815377289302, 2.10761714681621e-05, 0.222448185938361,
5.45130776104763, 327.118134199018, 60383.600655089, 1520.1609648366,
1481664.13388885, 34818.2575270259, 4060744.53269009, 0.000329878093050015,
3.50190559072499e-09, 0.365265550723291, 0.259371482496831,
5905.41200098933, 0.0213725080705001, 0.0019791757328749,
107933.355503732, 3.39775429580098e-12, 0.0330528161763248,
0.386107904764506, 1.99602333972179e-09, 0.000228093064728933,
5.19580674670871, 563.142766773764, 7356479.18831348, 315961.427593756,
261.353018397329, 44.4265659338378, 59015.8931254825, 33849.8516454425,
12037.241852193, 22886385.0415054, 9597313.08865641, 34.1796031059148,
3426361.9581678, 24812.0886306435, 16.2435772262661, 5335252.58800916,
15.4267958636366, 6.23003438750091, 6074284.64243093, 3625.64806417381,
6.8968999739466e-10, 6.28467534994529e-12, 8.67182915364106e-08,
0.134069855760784, 0.000162817646946365, 9.75584343397934e-07,
0.000117617735155071, 0.000113004292729558, 1421451.62202609,
9547535.94782668, 0.247463394835223, 1592.67069635606, 19898.2670219713,
454400.812967405, 0.00323291029572383, 9988.88694126955,
143.08805454549, 136687.142590513, 0.00242354231190896, 0.533822212459591,
0.00344026378149421, 484468.802936889, 2061965.14117208,
31.6052901005199, 0.0682189910835192, 14225946.1418798, 22656006.1427591,
8.68723197155215e-10, 0.00908810773970795, 1.32803636893067e-10,
3.2838750723857e-09, 0.0867179882627688, 0.0978011155313282,
0.349647525170572, 925.768941042692, 677721.086653615, 14806.4581572174,
3994895.41282463, 0.13512052318472, 0.0284052204027308, 14772.9294370976,
832124.265001897, 0.0506818824206226, 653153.199476183, 2412.38065661841,
2.58407754903887, 312946.331847625, 73.8532458134753, 0.0333143376007489,
0.148231771582762, 1077.07159652263, 303929.715210286, 248.050418381286,
1.32713216726536e-08), effect = c(-0.0163190960149514, 0.0239828026929405,
0.041119697076967, 0.0209172832321608, 0.0175906130298972,
0.0834614832205, 0.0454379665875109, 0.0386831358592724,
0.111290387209505, 0.0417666009957902, 0.0181594607740641,
0.0314977892715717, 0.0517389743010281, 0.0151006425358355,
0.102545733351982, 0.0141441804980859, 0.0616852086092345,
0.0224337526553543, 0.0769583981642733, 0.0152865895595402,
0.016476178103406, 0.0850464780157665, 0.259761646456551,
0.0758096357842209, 0.0879871401163749, 0.060577639895305,
-0.119584728001896, -0.0429780663246056, -0.0982076786158141,
-0.00853408648534678, -0.0398300310844788, -0.0130306262269616,
-0.0117783024812117, -0.0277653734748019, 0.0044146808370715,
-0.00858548331854399, -0.019241126838373, -0.00578884017923847,
0.00226822950309142, 0.0428684006392723, 0.0693526182788657,
0.0497740775628015, 0.516295204419154, 0.00640383203849196,
-0.00759227766834665, -0.0144951201040298, -0.0301134012710303,
-0.00177543890387751, 0.0844729727849364, 0.035083882999979,
-0.0452607458985876, -0.0112498347652378, 0.00432465949184261,
-0.0612693958135787, -0.161890494475421, -0.109885795143899,
-0.0367297743779374, -0.128480388877075, -0.0862830814238638,
-0.0767455417397432, 0.0879696206150576, 0.0341978476362303,
0.272396625438286, 0.0743011988057289, 0.0929358487669378,
0.0631542372947326, 0.0120936625302536, -0.0102721783333691,
0.0231718474438414, 0.031607051653089, -0.0305949081690051,
0.00330395034884568, 0.0373053051750641, -0.00385151004793588,
-0.0157412786940113, -0.0132287150372285, -0.0120506314486498,
-0.025044061802607, 0.00470372186431196, -0.0110743076334009,
-0.0386239261622541, 0.00837054636189714, 0.057136428153608,
0.0312680735700298, 0.0118706124991179, 0.128234673021361,
0.097326263380819, -0.00329520180332474, -0.071533496180363,
0.003789002995193, 0.0046909737332724, 0.0337729519954883,
0.0327930393051356, 0.0358750303006964, 0.104755410676543,
0.110139205213869, 0.0104813831771491, 0.038564461505739,
0.0174699813771993, 0.0667967652778607, 0.00452704565399326,
0.169065310950391, 0.0860051315499004, 0.0113816127441823,
0.054028218719922, 0.0828069492580835, 0.0566902515183901,
-0.0177524070698768, -0.0386299683572492, 0.0342109822338447,
0.00547588201891631, 0.0272576717302203, 0.0173982318869093,
0.00713163621583953, 0.0226481978020165, 0.0472525993298041,
0.0273573262972059, 0.025199426283408, 0.0744462951082736,
0.0153618732415838, -0.0200185553769581, 0.00188477410376072,
-0.00934913057903759, 0.00600234114364721, 0.0179457906957483,
0.00430101870279759, 0.174154071440548, 0.173673770147469,
0.0951350053551374, 0.0914389204777777, 0.135948060156079,
0.0277299892479554, 0.16803169351595, 0.0775422760432586,
0.0510707831236068, 0.0160622114639729, 0.0134597473251168,
0.0683573948296253, 0.0886360341968248, -0.00352396978265606,
-0.0826520910456777, 0.0044728665513685, -0.159909011556907,
0.0111881761993282, 0.131640942119481, 0.0316929149347357,
0.195644963948708, 0.067380251345993, 0.0259465675571933,
0.0455342345905025, 0.0971025906472932, 0.0201913183727302,
0.0349939607223961, 0.195861080450541, 0.0655886359004071,
0.146886606410868, 0.0892874895878602, 0.0796735966091277,
0.146650136993383, 0.120645912644686, -0.0752509299371392,
-0.00163803947679698, -0.0574957284606062, 0.00698636531233787,
0.0526791440398665, 0.018473633833928, -0.103073001462617,
-0.0948950576502364, -0.102463654498151, -0.0758695690445602,
-0.00213230587204453, -0.00568008091184311, -0.0182279768547043,
0.0899803113373462, 0.177084057032596, 0.0913169797144365,
0.0116673552151304, 0.0409140209831297, 0.0411965956436703,
0.0200981129338266), 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(-3.82997789810028,
8.69151503091653, -1.61473150877182, 1.40730794339407, 0.515406613246166,
3.66658658595408, 7.1986283729153, -2.00820919450685, -3.06909191624479,
0.590884338223415, -0.504368307179268, -3.38875638907046,
3.45985340742856, 1.11543772500508, -7.35941265115998, -3.99306654044701,
-3.56261255637583, -4.71509812185044, 0.711799756620258,
-3.01152625196879, -3.86860623929587, 4.05939998667948, 0.210623317244372,
-4.95719313989901, 3.99689055300952, 0.482360510453302, -4.76982116735253,
3.82136778630483, 3.36256743134917, -2.77025398593507, -1.1263382193229,
-5.67014039027971, -2.39514048241807, -5.27663279708947,
-5.48173815932764, -5.30985455208272, -6.81549182742701,
-0.756206793523825, -6.08751406236996, -1.99500007936184,
9.52047315064185, 3.37485019798332, 2.79921708639208, -5.94901269617059,
-0.831632210867015, -1.04210483960289, -2.66145232147358,
-5.58874998605655, -6.13600981017556, -2.104169420452, -7.33665520171442,
-2.39721629249818, -5.69400670154053, 4.16466094727295, 1.7417044600378,
5.5447611828998, 1.32276604199993, 3.87652051578144, -5.72470286300328,
0.655923579381524, 1.09903669444412, 4.66446892684065, 6.36936586766657,
0.35369857573924, 0.617530511087391, 1.16759583626924, -0.510805927414134,
-1.09071367676328, -0.602316667904953, -0.563439573001041,
-1.1025581531972, -2.68569762468031, -1.52502710035885, -6.96061418745396,
-2.93150286897326, -1.02430010532252, -2.3999369332979, -1.82291239059371,
-0.863933137944786, -1.09819221618344, -2.94614325802066,
-6.34011784869935, -0.83414753306763, -0.876111204330165,
4.67620827682094, 0.652771131566519, -0.736500701409775,
-2.51470462044497, -4.78091900630239, -3.18188957639156,
-6.17074976819802, -4.54180703314166, -6.60860566825564,
3.48164652465895, 8.45569556641358, 0.4373912854565, 0.586077775650145,
-3.77125020229067, 1.67014451025219, 2.70351564266601, -5.03315567890536,
11.4688080297742, 1.48079153170835, 0.413291306999458, 8.69983438476552,
3.64188791943946, -0.71565298951438, -2.75061851023672, -6.86667001067782,
-5.49963406741059, -2.41722752018175, -1.64764274462453,
-4.77096898396776, -4.52955676963248, -4.08052698645697,
-7.35957720025387, -6.98214966282064, -1.53376701537969,
-6.53483323957859, -4.3946633238282, -1.21068167752515, -6.72715498514415,
-1.18827573272272, -0.794490443811766, -6.78349513920237,
-3.55938564553853, 9.16134607255797, 11.2017171519269, 7.06188928688985,
0.872668857855968, 3.78829852600649, 6.01073517804921, 3.92952718761996,
3.94690505852234, -6.15273208341116, -6.97989130222874, 0.606489033485156,
-3.20212598952403, -4.29881525452187, -5.65743909969887,
2.49040634568619, -3.99951709761578, -2.15560337896229, -5.13572766479016,
2.61554939365603, 0.272603359083682, 2.4634082567147, -5.68526581616783,
-6.31428131898601, -1.49975978101014, 1.1660947078266, -7.15308116016609,
-7.35518335376065, 9.06111858154676, 2.04152653303234, 9.87679003143666,
8.48361337299155, 1.06189080571935, 1.00965619156887, 0.456369541379321,
-2.96650260638842, -5.83105099851421, -4.17045118365708,
-6.60150541388087, 0.869278681851812, 1.54660183658146, -4.16946662342416,
-5.92018818635659, 1.29514726297355, -5.81501505858544, -3.38244583747527,
-0.41230554284251, -5.49546986540307, -1.86836958709421,
1.47736881752115, 0.829058700916523, -3.03224457325269, -5.48277316313018,
-2.39453996388795, 7.87708582419226), type = 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", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2")), .Names = c("Measurement",
"category", "variable", "Pval", "effect", "direction", "plotval",
"type"), row.names = c(23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 65L,
66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L,
79L, 80L, 81L, 82L, 83L, 84L, 107L, 108L, 109L, 110L, 111L, 112L,
113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L,
124L, 125L, 126L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L,
157L, 158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L,
168L, 191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 199L, 200L,
201L, 202L, 203L, 204L, 205L, 206L, 207L, 208L, 209L, 210L, 233L,
234L, 235L, 236L, 237L, 238L, 239L, 240L, 241L, 242L, 243L, 244L,
245L, 246L, 247L, 248L, 249L, 250L, 251L, 252L, 275L, 276L, 277L,
278L, 279L, 280L, 281L, 282L, 283L, 284L, 285L, 286L, 287L, 288L,
289L, 290L, 291L, 292L, 293L, 294L, 317L, 318L, 319L, 320L, 321L,
322L, 323L, 324L, 325L, 326L, 327L, 328L, 329L, 330L, 331L, 332L,
333L, 334L, 335L, 336L, 359L, 360L, 361L, 362L, 363L, 364L, 365L,
366L, 367L, 368L, 369L, 370L, 371L, 372L, 373L, 374L, 375L, 376L,
377L, 378L), class = "data.frame")
非常感谢你的帮助