This question already has an answer here:
Issue when passing variable with dollar sign notation ($) to aes() in combination with facet_grid() or facet_wrap()
(1 answer)
Closed 6 years ago.
I have this plot that I've generated using ggplot2 in R.
The code that produces this plot is:
plot <- ggplot(mockdata, aes(variable, Measurement)) +
geom_tile(aes(fill = mockdata$plotval), colour = "dark red") + facet_grid(~type, scales='free', space='free') + 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, face = "italic")) +
labs(title="", x="", y="", fill="") + theme(strip.text.x=element_blank(),strip.text.y=element_text(size=20, angle=0))
As you can see, I've added a horizontal space based on capitalization of my x-axis variables. Now however, I would like to also add vertical spacing based on another factor variable. I simply change the code to be:
ggplot(mockdata, aes(variable, Measurement)) +
geom_tile(aes(fill = mockdata$plotval), colour = "dark red") + facet_grid(category~type, scales='free', space='free') + 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, face = "italic")) +
labs(title="", x="", y="", fill="") + theme(strip.text.x=element_blank(),strip.text.y=element_text(size=20, angle=0))
The above gives exactly the look I'm going after but notice that the values are all messed up! For example 20:b and 20:c are blue in the first plot but red in the second plot. The first plot contains the correct values.
I've been going crazy going over my factor labelling for hours but I can't seem to find the problem. I would like a solution as how I can add the vertical space to my first plot, using facet_grid or no, it doesn't matter, and special bonus points will be given to anyone who can explain what went wrong in my second plot.
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(42.7006323958918, 0.0125450399252769, 5675.14711907005,
0.0931416002606762, 0.00232094737764211, 0.000216076621032813,
0.00396622838836271, 6.52388877927194, 11.5722289932645,
0.000334263699328083, 27.0431686878052, 327952.740805895,
0.0297787229949035, 0.585782163292434, 36.9115682159781,
4165.1649504391, 52.9995249740471, 6186.78975112519, 7.85106982537471e-05,
294938.11483059, 9197376.02201148, 1.56761799701167e-05,
0.687511811844484, 2951357.69281822, 0.281822039246925, 0.550068489025187,
12173.6665259036, 0.000654862439823334, 0.00448573254652099,
7319.35702087325, 315.359225529075, 5848068.52630072, 13904253.2299896,
6.66288479812568, 69897.571357397, 10503715.2130853, 46675.3343032854,
80.0136913688775, 2.74356992078766, 348201.375999632, 1.07521153836152e-06,
1.98853436745688e-05, 0.400197989005822, 3511901.4371568,
164.269778446034, 3.24558993609286, 65394.4866129748, 55.4132232221762,
4.83069482078633, 38.6041603684776, 37.9912973942591, 104522.510922666,
6345.6890512069, 0.0193603214641399, 9.18538323079328e-09,
0.000103476856048387, 0.000555384674445469, 3.22868352890832e-05,
113007.480780211, 0.000160838575168945, 0.00217855024162056,
8.64496348775897e-06, 7.06449122162668e-07, 0.619370137929941,
0.270921839221627, 0.011388566962421, 937781.722037049, 652.688753412217,
47171.0329654517, 8.70296668276766, 8276277.85721442, 28353.82586081,
175700.845731391, 1551633.67731154, 689.167798328347, 109.943340419374,
11.3781857520997, 1617599.77065294, 37.1569915088865, 4203.89382661281,
117832.471263455, 1251784.09345768, 535.751672862479, 165398.434479864,
2.55163228104252e-09, 9.8013680674497e-10, 129.319151038722,
556.223625027009, 869.236740102022, 3092.82667967769, 4257.32149187776,
772478.486004829, 15.7002509424478, 0.000219697884882267,
0.000427755809814034, 0.192409289349257, 0.280314538898884,
372.959096370547, 0.00465127237377936, 2.10935354960679e-05,
232459.76019099, 5.77106623224624e-08, 0.0483934897590051,
0.0146558784756261, 1.47248474221649e-08, 0.000222461186585379,
852431.075531575, 528.683263593354, 8298.0240769841, 14.7493364766307,
6.2911608490762, 4536740.5530938, 106.089248136605, 99.5504144351031,
114043.212772726, 253658.532323196, 140833.560642241, 3335.43395071637,
36.6239752632138, 198.244201535186, 14553.9246345935, 2.47559453576517,
1469.76758256116, 4809.5093947632, 2111515.77618631, 3211.80355220151,
0.0046904155982774, 8.1507521777127e-07, 0.000265158173925916,
0.0331951291090042, 9.47798135335384e-10, 0.00629215449841144,
8.27956294929558e-05, 0.000192338169994771, 554785.490344723,
793.314954892235, 0.106735781318401, 14029944.8981012, 527616.898199668,
478322.816562339, 0.00139976568423574, 520.741670562665,
1289142.03044888, 5.80426527938221, 0.00134726649987191,
0.370746844567892, 0.000167132571538772, 3182343.07410991,
82449.5457770316, 2687893.44907279, 0.178274475177317, 50.1455573308552,
414876.812590266, 2.60936167209485e-09, 2.32090832789666e-08,
8.42629935014704e-10, 0.000255374171026513, 1.10761356981753e-06,
5.37793701940977e-05, 0.557162933357393, 829.829809862024,
6184566.2549798, 2782.96376300611, 898.722084215034, 0.0049389188839104,
0.736095078701914, 73.501019996704, 280617.802045198, 0.00108189739567757,
139765.97782083, 18925.6574878606, 39945.1297996732, 11654.4707728977,
5.25244181877881, 0.000951333348397858, 0.701154190998343,
3783700.00624337, 1048363.72082207, 627347.0256674, 0.00056428282172991
), effect = c(-0.00504202541860286, 0.0572728382379748, 0.19355610290193,
0.0908419036193052, 0.0753460915818811, 0.0304533379241126,
0.338999529965851, 0.00450925741610117, 0.0142515711297281,
0.0236642558989115, 0.112084463317692, 0.0355492039503297,
0.046179641636787, 0.0833907727035694, 0.0119081147311255,
0.0409084851564839, 0.00974046553876251, 0.00623992766439915,
0.0828237061289605, 0.0460014467529953, 0.0220280811514705,
0.072554252236383, 0.181702209021198, 0.0496502124621067,
0.103706638295506, 0.0514006496664137, -0.09126288844347,
-0.0579011936709518, -0.0313363166032359, -0.0137049341579666,
-0.0538731912627118, -0.0871985756458715, -0.00540516899377108,
-0.0335420569797512, 0.00837464914354496, -0.00433782674791291,
-0.0186401470510755, -0.00957092739050277, 0.0233458885691129,
0.0363082683534594, 0.0949001163895009, 0.0980889928637072,
0.272094653271255, 0.0116498581551015, -0.0239706620213576,
-0.00948922481425107, -0.0463841533571947, -0.000196251500025392,
0.0332074957195669, 0.0306732835672796, -0.078594298822456,
-0.0013592321904609, 0.00823043498271145, -0.0792124313370092,
-0.114084449997195, -0.0535820005729329, -0.0193882875198498,
-0.145900537042366, -0.042750018055737, -0.0353791699868627,
0.0917785141337663, 0.198537943334598, 0.21948014556882,
0.0524090722153662, 0.014529019941343, 0.0986667789371684,
0.013579010005435, -0.0140683606411563, 0.0151127260881476,
0.00348067883725744, -0.0239177792519331, 0.00323518849038519,
0.0248013017682591, -0.0205422166822478, -0.00862063024472445,
-0.0253349156677723, -0.00206032852686476, -0.034685899461899,
0.0291777396060294, -0.00790295204452705, -0.0172764198936056,
0.093523378677899, 0.0966298831466818, 0.0519375468929764,
0.184676539092511, 0.0738557771574706, 0.0610706031136215,
-0.00852528463886119, -0.035804039040301, 0.0096352117901668,
0.0521074840389658, 0.0364758835127577, 0.0267540711492766,
0.134747323978599, 0.109492562370398, 0.0133001291007968,
0.0732780309339287, 0.0303602020342369, 0.0149482630094048,
0.0273990078144707, 0.00299717221567407, 0.100866435181815,
0.114870729540125, 0.0567497200005222, 0.0912124964999035,
0.127694568835571, 0.0999233369545313, -0.0228848084434401,
-0.0199126446791925, 0.0455760222896934, 0.00550931423553266,
0.0169695986729115, 0.00547424764931202, 0.0360297252475284,
0.00940059675048105, 0.00779317291104235, 0.0292808745009825,
0.0146692995987833, 0.107736041982938, 0.0379549009324052,
-0.0245307848578086, 0.00373489473816007, -0.0277668840806,
0.00541292380436789, 0.0109495216225972, 0.0154749859461561,
0.517201286005927, 0.213654141088575, 0.105933886134205,
0.083967352311383, 0.0390362727543106, 0.0541759578921366,
0.0799491572647821, 0.0713475409375969, 0.0830535251532681,
0.0634176325142384, 0.0340343110430986, 0.0741195089506451,
0.0422227419333998, -0.00201868398387451, -0.102142939489265,
0.00990816502366215, -0.0723986947049853, 0.00438771610893309,
0.0614558366658632, 0.112141419906449, 0.206453004881553,
0.0458149211724056, 0.0370232303908095, 0.0104171190238791,
0.174352387892548, 0.0173287004770711, 0.0570322589545976,
0.0919422210688935, 0.206073359134817, 0.144707153996313,
0.0866876754204277, 0.139499693175196, 0.0501563760935562,
0.113150966215879, -0.0615705734418705, -0.0016999262242578,
-0.161924930685107, 0.0055290101159364, 0.106581585830427,
0.0128140174669214, -0.0703636721519287, -0.018257094814349,
-0.100875941503327, -0.0160441944317892, -0.00386698165039997,
-0.00342265706742182, -0.0322326223948505, 0.0280294325939845,
0.0984244986939011, 0.0512777173537761, 0.0352523410798283,
0.13292883383322, 0.0807717506474815, 0.0508596294487826),
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(-1.63043430696911,
1.90152795218119, -3.7539771243977, 1.03085630474974, 2.63433470610105,
3.6653922201862, 2.40162228131836, -0.814506548481021, -1.06341701895514,
3.47591078475207, -1.43205757717356, -5.51581126479104, 1.52609393009486,
0.23226385645368, -1.56716249691816, -3.61963220517785, -1.72427197710882,
-3.79146535741686, 4.10507116012102, -5.46973090002207, -6.96366394237768,
4.80475975925492, 0.162719836007062, -6.4700218474543, 0.550025046893605,
0.259583233135038, -4.08542140092089, 3.18384991817596, 2.34816662398371,
-3.86447293152949, -2.49880554038012, -6.7670124529409, -7.14314766872669,
-0.823662304342601, -4.84446208612616, -7.02134293822782,
-4.66908743718463, -1.90316430670692, -0.438316032811778,
-5.54183048300149, 5.96850608374351, 4.70146689878517, 0.397725098023318,
-6.54554231876607, -2.21555767139982, -0.511293648040455,
-4.81554113464696, -1.74361341251133, -0.684009601793095,
-1.58663411108907, -1.57968412486682, -5.01920983414714,
-3.80247878716975, 1.71308743582189, 8.03690271942929, 3.98515677498429,
3.25540610855334, 4.49097452184084, -5.05310719351909, 3.79360978286461,
2.66183241993435, 5.06323683664087, 6.15091911019143, 0.208049737379102,
0.567155984737416, 1.94353092029908, -5.97210176380931, -2.81470612936045,
-4.67367538666088, -0.939667320932661, -6.91783506268839,
-4.45261166774866, -5.24477385196537, -6.19078919714086,
-2.83832497664739, -2.04116892804232, -1.05607301946984,
-6.20887107667165, -1.57004054321228, -3.62365173896932,
-5.07126498609874, -6.09752942863009, -2.72896353578257,
-5.21853139457582, 8.59318191216903, 9.00871330158534, -2.11166284486924,
-2.74524943113182, -2.93913807439764, -3.4903555830589, -3.62913644718831,
-5.88788639290012, -1.19590659394104, 3.65817412418273, 3.368804083124,
0.715773964440957, 0.552354376375251, -2.57166120394098,
2.33242822790069, 4.67585062188463, -5.36634778544318, 7.23874394143661,
1.31521305884959, 1.83398814476682, 7.83194919664647, 3.65274575062554,
-5.93065927361594, -2.72319556226042, -3.91897469061421,
-1.1687724832959, -0.79873078913878, -6.65674394356368, -2.02567137153985,
-1.99804307236447, -5.05706944355335, -5.40424947528169,
-5.14870615952883, -3.52315234504154, -1.56376548199677,
-2.29720049345194, -4.16298012165971, -0.393679515429061,
-3.16724866427777, -3.68210077741043, -6.32459433060349,
-3.50674897408984, 2.32878867454675, 6.08880231131442, 3.57649498051523,
1.47892563787053, 9.02328415005142, 2.20120062219246, 4.0819925875773,
3.71593452037349, -5.74412509417193, -2.89944564130995, 0.971689966478527,
-7.1470559653621, -5.72231869642199, -5.67972109772494, 2.8539446575791,
-2.71662233198973, -6.1103007681231, -0.763747253307228,
2.87054648892738, 0.430922536570879, 3.77693890461838, -6.50274699717143,
-4.91618826741447, -6.42941204883235, 0.748910833398874,
-1.70023246253767, -5.61791916284656, 8.58346572108797, 7.63434201313085,
9.07436311632148, 3.59282303012629, 5.9556117321782, 4.26938428807087,
0.254017783801932, -2.91898903187407, -6.79130924645919,
-3.44450755141886, -2.95362541370008, 2.30636810656965, 0.133066085813731,
-1.86629336596673, -5.44811521866938, 2.96581392463054, -5.14540146724019,
-4.27705097606589, -4.60146383671481, -4.06649255708194,
-0.720361250431011, 3.02166727906391, 0.154186465719317,
-6.57791669570072, -6.02051198354564, -5.797507843288, 3.24850317060798
), 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")
Thank you very much for your help.
Do it like here (in aes fill should be the column NAME not the VECTOR):
plot <- ggplot(mockdata, aes(variable, Measurement, fill = plotval)) +
geom_tile(colour = "dark red") +
facet_grid(category~type, scales='free', space='free') +
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, face = "italic"),
strip.text.x=element_blank(),
strip.text.y=element_text(size=20, angle=0)) +
labs(title="", x="", y="", fill="")
Related
Question: why is geom_line() not connecting through geom_point()?
I have:
Written with
ggplot(a,
aes(x = month, color = year, fill = year)) +
scale_color_manual(values = colsze) +
scale_fill_manual(values = alpha(colsze, .2)) +
scale_x_discrete(labels = c("January", "February", "March", "April", "May",
"June", "July", "August", "Septemer",
"October", "November", "December")) +
geom_point(aes(y = n), size = 4, shape=19) +
geom_line(aes(y = n)) +
scale_y_continuous(breaks = seq(0, 120, 10), limits = c(0, 120)) +
facet_wrap(.~year)
I cannot figure out why this does not work? E.g. following tutorials like this
geom_line() seems to appear in the legend but not in plot.
a <- structure(list(month = structure(c(4L, 1L, 4L, 7L, 1L, 9L, 2L,
8L, 8L, 10L, 7L, 10L, 9L, 9L, 9L, 2L, 10L, 7L, 4L, 2L, 2L, 3L,
11L, 11L, 12L, 9L, 12L, 10L, 10L, 10L, 11L, 5L, 10L, 10L, 10L,
10L, 10L, 12L, 11L, 7L, 12L, 6L, 9L, 9L, 9L, 7L, 9L, 4L, 12L,
12L, 11L, 3L, 3L, 11L, 11L, 11L, 7L, 11L, 12L, 12L, 12L, 2L,
4L, 1L, 11L, 11L, 1L, 4L, 8L, 2L, 10L, 5L, 5L, 6L, 7L, 11L, 11L,
11L, 11L, 11L, 12L, 11L, 10L, 7L, 12L, 9L, 9L, 7L, 10L, 8L, 8L,
5L, 9L, 10L, 9L, 3L, 8L, 10L, 10L, 8L), .Label = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12"), class = "factor"),
year = structure(c(3L, 3L, 2L, 1L, 4L, 4L, 4L, 1L, 1L, 1L,
3L, 1L, 2L, 1L, 1L, 3L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 3L,
1L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 2L,
4L, 4L, 2L, 2L, 2L, 2L, 1L, 3L, 1L, 1L, 2L, 1L, 1L, 2L, 2L,
2L, 4L, 1L, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 1L, 2L, 2L, 1L, 4L,
2L, 1L, 1L, 4L, 4L, 2L, 2L, 4L, 4L, 2L, 3L, 3L, 3L, 4L, 4L,
1L, 1L, 3L, 4L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 1L, 1L, 1L
), .Label = c("2017", "2018", "2019", "2020"), class = "factor"),
n = c(92L, 95L, 83L, 95L, 70L, 88L, 94L, 103L, 103L, 98L,
95L, 98L, 90L, 89L, 89L, 76L, 98L, 97L, 79L, 103L, 103L,
111L, 104L, 104L, 73L, 89L, 73L, 107L, 107L, 107L, 88L, 111L,
107L, 107L, 107L, 107L, 107L, 73L, 104L, 78L, 87L, 92L, 90L,
90L, 90L, 78L, 89L, 92L, 98L, 98L, 85L, 111L, 111L, 85L,
85L, 85L, 97L, 104L, 73L, 73L, 73L, 71L, 92L, 99L, 85L, 104L,
99L, 83L, 103L, 94L, 90L, 90L, 90L, 92L, 97L, 85L, 85L, 88L,
88L, 85L, 73L, 89L, 107L, 97L, 87L, 89L, 89L, 95L, 96L, 103L,
103L, 75L, 90L, 90L, 90L, 88L, 87L, 98L, 98L, 103L)), row.names = c(NA,
-100L), groups = structure(list(month = structure(c(1L, 1L, 1L,
2L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 7L, 7L, 7L,
7L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L,
12L, 12L, 12L), .Label = c("1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "11", "12"), class = "factor"), year = structure(c(2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 3L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 3L, 4L), .Label = c("2017", "2018", "2019", "2020"), class = "factor"),
.rows = structure(list(c(64L, 67L), 2L, 5L, 20:21, 62L, 16L,
c(7L, 70L), c(22L, 52L, 53L), 96L, 19L, c(3L, 68L), c(1L,
48L, 63L), 72:73, 92L, 32L, c(42L, 74L), 4L, c(40L, 46L
), c(11L, 88L), c(18L, 57L, 75L, 84L), c(8L, 9L, 69L,
90L, 91L, 100L), 97L, c(14L, 15L, 26L, 47L, 86L, 87L),
c(13L, 43L, 44L, 45L, 93L, 95L), 6L, c(10L, 12L, 17L,
98L, 99L), c(71L, 94L), c(28L, 29L, 30L, 33L, 34L, 35L,
36L, 37L, 83L), 89L, c(23L, 24L, 39L, 58L, 66L), c(51L,
54L, 55L, 56L, 65L, 76L, 77L, 80L), 82L, c(31L, 78L,
79L), 49:50, c(25L, 27L, 38L, 59L, 60L, 61L, 81L), c(41L,
85L)), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 36L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
Try this:
ggplot(a,
aes(x = as.numeric(month), color = year, fill = year)) +
# scale_color_manual(values = colsze) +
# scale_fill_manual(values = alpha(colsze, .2)) +
scale_x_continuous(breaks = c(1,2,3,4,5,6,7,8,9,10,11,12),
labels = c("January", "February", "March", "April", "May",
"June", "July", "August", "Septemer",
"October", "November", "December")) +
geom_point(aes(y = n), size = 4, shape=19) +
geom_line(aes(y = n)) +
scale_y_continuous(breaks = seq(0, 120, 10), limits = c(0, 120)) +
facet_wrap(.~year)
I commented on those two lines because in your reproducible example there is no variable colsze.
The problem is that month is a factor and must first be converted to numeric. For a better visualization, evaluate whether to rotate the labels on the x axis by 45 °
I want to change some columns to integers (from the 4 column of my table to the last one). How can I do that?
My data
I already tried
databianca %>%
mutate_if(is.factor, ~as.integer(as.character(.)))
and
databianca %>%
mutate_at(vars(columbia:escrita.POS), ~as.integer(as.character(.)))
and with both of them I get these warnings:
Warning messages:
1: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
2: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
3: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
4: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
5: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
6: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
7: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
8: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
Isn't there an option to treat all columns from columbia to the last column (in matlab I would use 4:end to give the instructions from the 4th column until the last element) as integrals?
my data via dpasta:
databianca <- data.frame(
SEXO = as.factor(c("F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "M", "M", "M", "M",
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M",
"M", "M", "M", "M", "M", "M", "M", "M", "M",
"M", "M", "M", "M")),
GRUPOS = as.factor(c("C", "E", "E", "C", "C", "C", "E", "E", "E",
"C", "E", "E", "E", "C", "C", "C", "E", "C",
"E", "E", "C", "E", "C", "C", "C", "E", "E",
"E", "C", "E", "C", "C", "C", "E", "C", "E", "C",
"E", "E", "C", "C", "C", "E", "E", "E", "E",
"C", "C", "C", "C")),
SALAS = as.factor(c("B", "A", "C", "A", "A", "C", "A", "B", "C",
"C", "C", "B", "A", "C", "A", "C", "A", "C",
"C", "C", "B", "B", "B", "A", "B", "C", "A",
"B", "A", "A", "C", "B", "B", "A", "A", "B", "B",
"B", "C", "B", "B", "B", "B", "A", "A", "B",
"C", "C", "A", "B")),
token = c(100L, 95L, 106L, 112L, 84L, 125L, 108L, 114L, 112L,
117L, 84L, 96L, 124L, 118L, 99L, 84L, 95L, 113L, 123L,
135L, 100L, 92L, 101L, 129L, 93L, 106L, 101L, 84L, 84L,
128L, 113L, 95L, 109L, 127L, 112L, 108L, 108L, 118L, 97L,
112L, 116L, 88L, 92L, 111L, 123L, 112L, 116L, 104L, 90L,
89L),
hab.visuo = c(3L, 3L, 2L, 0L, 0L, 1L, 3L, 3L, 2L, 1L, 1L, 1L, 0L, 2L,
1L, 1L, 2L, 2L, 0L, 7L, 3L, 2L, 0L, 2L, 0L, 3L, 1L, 0L,
1L, 1L, 1L, 1L, 0L, 2L, 2L, 1L, 1L, 0L, 0L, 0L, 1L, 2L,
1L, 1L, 0L, 2L, 0L, 0L, 3L, 3L),
tamanho = c(7L, 4L, 8L, 7L, 7L, 8L, 8L, 8L, 8L, 7L, 7L, 7L, 8L, 8L,
8L, 6L, 7L, 7L, 8L, 8L, 8L, 7L, 7L, 8L, 8L, 8L, 7L, 7L,
7L, 7L, 8L, 8L, 7L, 8L, 7L, 8L, 8L, 7L, 7L, 7L, 8L, 8L,
8L, 8L, 8L, 7L, 8L, 8L, 7L, 7L),
forma = c(6L, 6L, 7L, 4L, 6L, 6L, 6L, 7L, 6L, 6L, 6L, 6L, 6L, 5L,
6L, 7L, 5L, 6L, 7L, 7L, 6L, 7L, 0L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 2L, 2L, 6L, 7L, 7L, 6L, 7L, 7L,
6L, 7L, 6L, 6L, 6L, 6L, 6L, 7L),
nomeou.cor = c(67L, 82L, 85L, 83L, 68L, 72L, 74L, 74L, 80L, 80L, 63L,
70L, 80L, 81L, 68L, 72L, 81L, 76L, 78L, 81L, 81L, 80L,
65L, 78L, 74L, 75L, 61L, 63L, 69L, 79L, 70L, 75L, 77L,
79L, 74L, 78L, 70L, 79L, 71L, 70L, 78L, 53L, 67L, 75L, 83L,
82L, 84L, 71L, 53L, 69L),
nomeou.incor = c(23L, 8L, 5L, 7L, 22L, 18L, 16L, 16L, 10L, 10L, 27L,
20L, 10L, 9L, 22L, 18L, 9L, 14L, 12L, 9L, 8L, 10L, 25L,
12L, 16L, 15L, 29L, 27L, 21L, 11L, 20L, 15L, 13L, 11L,
16L, 12L, 20L, 11L, 19L, 20L, 12L, 37L, 23L, 15L, 15L, 8L,
6L, 19L, 38L, 21L),
token.POS = c(120L, 120L, 136L, 117L, 122L, 118L, 135L, 122L, 124L,
112L, 98L, 127L, 130L, 119L, 116L, 94L, 122L, 125L,
130L, 123L, 113L, 116L, 105L, 109L, 113L, 120L, 99L, 87L,
0L, 123L, 0L, 94L, 136L, 125L, 118L, 124L, 123L, 128L,
101L, 124L, 131L, 87L, 116L, 120L, 117L, 119L, 123L, 127L,
91L, 105L),
hab.visuo.POS = c(1L, 2L, 2L, 1L, 2L, 5L, 2L, 1L, 3L, 0L, 2L, 2L, 1L, 2L,
5L, 1L, 0L, 0L, 0L, 5L, 0L, 2L, 0L, 1L, 0L, 2L, 3L, 1L,
0L, 1L, 0L, 1L, 0L, 3L, 1L, 4L, 1L, 0L, 2L, 0L, 1L, 0L,
0L, 2L, 3L, 2L, 7L, 1L, 2L, 3L),
tamanho.POS = c(8L, 8L, 8L, 7L, 8L, 7L, 7L, 8L, 8L, 8L, 7L, 8L, 8L, 8L,
8L, 7L, 8L, 8L, 8L, 7L, 8L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
0L, 7L, 0L, 7L, 8L, 8L, 7L, 7L, 7L, 8L, 7L, 8L, 7L, 7L,
8L, 6L, 8L, 7L, 8L, 7L, 8L, 7L),
forma.POS = c(6L, 7L, 7L, 7L, 6L, 7L, 7L, 7L, 6L, 5L, 7L, 6L, 6L, 7L,
5L, 5L, 7L, 5L, 7L, 8L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 6L,
0L, 7L, 0L, 5L, 7L, 7L, 7L, 7L, 2L, 7L, 5L, 5L, 5L, 7L,
6L, 7L, 7L, 6L, 6L, 7L, 2L, 6L),
nomeou.cor.POS = c(78L, 78L, 85L, 73L, 70L, 76L, 77L, 78L, 82L, 74L, 56L,
70L, 79L, 78L, 65L, 67L, 87L, 78L, 82L, 82L, 79L, 84L,
74L, 78L, 71L, 79L, 61L, 70L, 0L, 75L, 0L, 74L, 83L, 77L,
76L, 77L, 75L, 77L, 68L, 79L, 79L, 61L, 67L, 76L, 79L,
78L, 81L, 68L, 53L, 71L),
nomeou.incor.POS = c(12L, 12L, 5L, 17L, 20L, 14L, 13L, 12L, 8L, 16L, 34L,
20L, 11L, 12L, 25L, 23L, 3L, 12L, 8L, 8L, 11L, 6L, 16L,
12L, 19L, 11L, 29L, 20L, 0L, 15L, 0L, 16L, 7L, 13L, 14L,
13L, 15L, 13L, 22L, 11L, 11L, 29L, 23L, 14L, 11L, 12L,
9L, 22L, 37L, 19L),
columbia = as.factor(c("49,3", "55,38", "67,69", "53,33", "38,46",
"53,84", "63,07", "61,53", "63,07", "35,38",
"43,33", "38,33", "55,38", "67,69", "47,69",
"29,23", "60", "53,84", "61,66", "73,33", "47,
69", "46,66", "41,66", "56,66", "47,69", "61,66",
"33,33", "40", "60", "36,66", "51,66", "43,33",
"58,46", "60", "56,92", "38,33", "40", "60",
"53,33", "56,92", "61,53", "53,84", "44,61",
"47,69", "44,61", "66,15", "56,66", "58,33", "65",
"51,66")),
escrita = as.factor(c("2", "2", "2", "2", "2", "1,5", "2,25", "2",
"2", "2", "2", "2", "2", "2,75", "2", "2,5",
"1,75", "2", "3", "4,5", "1,75", "1,75", "2",
"2", "2", "2", "1", "1,5", "2", "2", "2", "2,5",
"2", "2", "2", "1,75", "1,75", "2", "2", "2,
25", "2", "2", "1,75", "1", "2", "0", "2,5", "2",
"1,25", "1,5")),
columbia.POS = as.factor(c("53,8", "66,7", "76,7", "31,7", "43,1", "58,
5", "56,7", "55", "70", "35,4", "28,3", "43,
3", "61,7", "68,3", "55,4", "28,3", "55", "56,7",
"68,3", "66,7", "41,5", "56,7", "50", "65",
"58,5", "75", "48,3", "31,7", "0", "53,3", "0",
"50", "65", "66,7", "73,8", "51,7", "45", "66,
7", "48,3", "69,2", "73,8", "43,3", "56,7", "47,
7", "51,7", "73,3", "66,7", "38,3", "43,3", "33,
3")),
escrita.POS = as.factor(c("2", "3", "3", "1,5", "1,5", "2", "3", "3",
"4", "2", "1,5", "3", "2", "4", "1,5", "3",
"2", "3", "4", "4", "1,5", "2", "3", "1,5", "2",
"2", "1,5", "1,5", "0", "1,5", "0", "2", "3",
"1,5", "1,5", "1,5", "1,5", "2", "1,5", "3",
"2", "2", "2", "1,5", "1,5", "1,5", "5", "3", "1,
5", "1,5"))
)
This happens when you use as.integer on non-integer variables.
It seems your numbers have "," in them (ex: 51,66 ) and also new line character which is creating problem so first make your factors "clean" with gsub() and then run as.integer()
databianca[,4:length(names(databianca))] <- apply(databianca[, 4:length(names(databianca))], 2, function(x) as.integer(gsub("[\n,]", "", x)))
This should also work
databianca[,4:length(names(databianca))] <- as.integer(gsub("[\n,]", "", as.matrix(databianca[,4:length(names(databianca))])))
I'm trying to re order my datas, I already found the code to use but it doesn't seem to work...
Can you help me please ?
This is my code :
#code for my boxplot
dat.m2 <- melt(H1,id.vars='fusion', measure.vars=c('FF','FM'))
dat.m2 <- melt(H1,id.vars='fusion', measure.vars=c('FF','FM'))
ggplot(dat.m2)+ geom_boxplot(aes(x=fusion, y=value, colour=variable))+ facet_wrap(~H1$Genotype)+
xlab(" ")+ ylab("Days after sowing")
#code tried to re order
levels(dat.m2$fusion)
dat.m2$fusion<-factor(dat.m2$fusion, levels=c("Control", "CK20", "CK100", "CK500", "GA20", "GA100", "GA500"))
I tried to run again the first code after re ordering but it didn't work...
You can also find attached the image of the boxplot that I'm trying to modify
Thanks
EDIT :
> dput(head(H1, 20))
structure(list(Genotype = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L), .Label = c("F1045",
"FF", "M1585", "M1610"), class = "factor"), X = structure(c(1L,
105L, 116L, 127L, 138L, 149L, 160L, 171L, 182L, 2L, 13L, 24L,
35L, 46L, 57L, 68L, 79L, 90L, 101L, 106L), .Label = c("H1", "H10",
"H100", "H101", "H102", "H103", "H104", "H105", "H106", "H107",
"H108", "H109", "H11", "H110", "H111", "H112", "H113", "H114",
"H115", "H116", "H117", "H118", "H119", "H12", "H120", "H121",
"H122", "H123", "H124", "H125", "H126", "H127", "H128", "H129",
"H13", "H130", "H131", "H132", "H133", "H134", "H135", "H136",
"H137", "H138", "H139", "H14", "H140", "H141", "H142", "H143",
"H144", "H145", "H146", "H147", "H148", "H149", "H15", "H150",
"H151", "H152", "H153", "H154", "H155", "H156", "H157", "H158",
"H159", "H16", "H160", "H161", "H162", "H163", "H164", "H165",
"H166", "H167", "H168", "H169", "H17", "H170", "H171", "H172",
"H173", "H174", "H175", "H176", "H177", "H178", "H179", "H18",
"H180", "H181", "H182", "H183", "H184", "H185", "H186", "H187",
"H188", "H189", "H19", "H190", "H191", "H192", "H2", "H20", "H21",
"H22", "H23", "H24", "H25", "H26", "H27", "H28", "H29", "H3",
"H30", "H31", "H32", "H33", "H34", "H35", "H36", "H37", "H38",
"H39", "H4", "H40", "H41", "H42", "H43", "H44", "H45", "H46",
"H47", "H48", "H49", "H5", "H50", "H51", "H52", "H53", "H54",
"H55", "H56", "H57", "H58", "H59", "H6", "H60", "H61", "H62",
"H63", "H64", "H65", "H66", "H67", "H68", "H69", "H7", "H70",
"H71", "H72", "H73", "H74", "H75", "H76", "H77", "H78", "H79",
"H8", "H80", "H81", "H82", "H83", "H84", "H85", "H86", "H87",
"H88", "H89", "H9", "H90", "H91", "H92", "H93", "H94", "H95",
"H96", "H97", "H98", "H99"), class = "factor"), Hormone = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), .Label = c("CK", "Control", "GA"), class = "factor"),
Hormone.quantity = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("100",
"20", "500", "Control"), class = "factor"), fusion = structure(c(4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L), .Label = c("CK 100", "CK 20", "CK 500",
"Control", "GA 100", "GA 20", "GA 500"), class = "factor"),
DL = c(16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L), LI = c(100L,
100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L,
100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L), Temperature = c(21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L), Sowing.date = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = "25-mrt", class = "factor"), BTD10 = structure(c(6L,
7L, 6L, 6L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
6L, 4L, 4L, 4L), .Label = c("16-apr", "17-apr", "18-apr",
"19-apr", "21-mei", "23-apr", "26-apr", "30-apr"), class = "factor"),
FFLDT = structure(c(13L, 18L, 4L, 9L, 18L, 3L, 2L, 13L, 8L,
10L, 18L, 10L, 8L, 8L, 8L, 10L, 11L, 11L, 1L, 11L), .Label = c("",
"10-mei", "14-apr", "14-mei", "17-mei", "18-jun", "21-mei",
"23-apr", "24-mei", "26-apr", "28-mei", "3-apr", "3-mei",
"30-apr", "31-mei", "4-jun", "7-jun", "7-mei"), class = "factor"),
FH = structure(c(42L, 62L, 67L, 18L, 59L, 7L, 5L, 52L, 53L,
62L, 65L, 58L, 53L, 42L, 52L, 58L, 24L, 55L, 1L, 54L), .Label = c("",
"10", "10,5", "11", "11,5", "11,7", "12", "12,3", "12,5",
"13", "13,5", "14", "14,3", "14,5", "15", "15,3", "15,5",
"16", "16-jan", "17", "18", "18,5", "19", "20", "20,5", "21",
"21,5", "22", "22,5", "23", "23,5", "24,5", "25", "25,5",
"26", "26,5", "27", "27,5", "29", "29-mei", "3", "3,5", "30",
"30,5", "31,5", "32", "32,5", "33", "35", "36", "37", "4",
"4,5", "40", "42", "43", "47", "5", "5,5", "53", "55", "6",
"6,5", "7", "8", "8,5", "9", "9,5"), class = "factor"), SRDT = structure(c(3L,
8L, 1L, 1L, 8L, 1L, 8L, NA, NA, NA, 4L, NA, 15L, 12L, 14L,
14L, 15L, 15L, 1L, 15L), .Label = c("", "10-mei", "11-jun",
"13-jun", "13-mei", "14-mei", "17-mei", "18-jun", "21-jun",
"21-mei", "24-mei", "28-mei", "3-mei", "31-mei", "4-jun",
"7-jun", "7-mei"), class = "factor"), MH = c(26, 50, NA,
NA, 46, NA, 61, NA, NA, NA, 40, NA, 68, 48, 47, 42, 26, 50,
NA, 48), SEEDT = structure(c(2L, 4L, 1L, 1L, 4L, 1L, 4L,
NA, NA, NA, 4L, NA, 9L, 8L, 8L, 8L, 4L, 3L, 1L, 4L), .Label = c("",
"11-jun", "13-jun", "18-jun", "20-mei", "21-jun", "28-mei",
"31-mei", "4-jun", "6-apr", "7-jun"), class = "factor"),
FERMK = c(7L, 8L, NA, NA, 8L, NA, 8L, NA, NA, NA, 5L, NA,
7L, 6L, 7L, 6L, NA, NA, NA, 4L), PLRMK = c(1L, 2L, NA, NA,
1L, NA, 1L, NA, NA, NA, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, NA,
1L), BT = structure(c(5L, 6L, 5L, 5L, 5L, 5L, 5L, 5L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 4L, 4L, 4L), .Label = c("",
"22", "23", "25", "29", "32", "bino"), class = "factor"),
FF = c(39L, 43L, 50L, 60L, 43L, 20L, 46L, 39L, 29L, 32L,
43L, 32L, 29L, 29L, 29L, 32L, 64L, 64L, NA, 64L), FM = c(78L,
85L, NA, NA, 85L, NA, 85L, NA, NA, NA, 80L, NA, 71L, 64L,
67L, 67L, 71L, 71L, NA, 71L), SEED = c(78L, 85L, NA, NA,
85L, NA, 85L, NA, NA, NA, 85L, NA, 71L, 67L, 67L, 67L, 85L,
80L, NA, 85L)), row.names = c(NA, 20L), class = "data.frame")
I have this plot which I've generated using ggplot2 in R.
The code which produces the plot is as follows:
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="")
As evident from both the plot and the code, the color scheme ranges from -20 to 20. plotval in my data actually represents -log10 of a P-value and so is always a positive number, but I've split it up into positive and negative by multiplying it with 1 or -1 depending of the direction of effect (the direction column in mockdata).
What I want to do is to split my gradient colorbar in two so that it ranges just from 0 to 20 but I would still like each tile to be red or blue and I would like the intensity of both the blue and the red can be seen side by side on the colorbar. I also want to increase the size of the colorbar but I guess that is strictly another question
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")
Thank you ever so much for your help
I have to plot bargraphs on a map using ggplot2.
library(ggplot2)
q=ggplot(data=mapindia, aes(long, lat, group=group,colour)) + geom_polygon(fill="#FF9999", colour="black") +coord_map()
q=q+geom_point(aes(x=LATITUDE, y=LONGITUDE,group=state_name),data=religion)+coord_map()
q <- q+geom_subplot2d(aes(long,lat,subplot = geom_bar(aes(value,fill=fact))), ref = NULL, width = rel(0.8), data = simdat)
print(q)
But got the following error:
## Using binwidth 2.12
## Using binwidth 2.13
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous
## Error in eval(expr, envir, enclos) : object 'group' not found
My data sets
dput(head(mapindia, 100))
structure(list(long = c(72.94823, 72.948775, 72.96202, 72.964302,
72.968211, 72.970708, 72.964092, 72.956172, 72.943367, 72.931651,
72.93708, 72.948365, 72.947281, 72.938496, 72.930794, 72.924501,
72.918209, 72.910397, 72.893036, 72.879257, 72.878837, 72.880537,
72.882884, 72.882518, 72.885103, 72.88869, 72.892337, 72.89675,
72.904402, 72.911528, 72.924481, 72.944418, 72.950934, 72.955821,
72.962662, 72.95561, 72.94823, 73.043316, 73.042348, 73.040185,
73.035095, 73.028379, 73.024913, 73.022964, 73.022209, 73.025029,
73.032294, 73.03815, 73.043791, 73.046396, 73.047159, 73.053124,
73.066142, 73.080464, 73.092181, 73.105632, 73.114977, 73.134375,
73.147935, 73.15217, 73.156084, 73.16586, 73.178224, 73.182115,
73.182861, 73.181982, 73.182834, 73.190533, 73.20268, 73.213414,
73.227186, 73.235096, 73.237463, 73.230614, 73.219987, 73.211622,
73.210741, 73.202164, 73.190022, 73.177, 73.165391, 73.150641,
73.1438, 73.13869, 73.133474, 73.13281, 73.136697, 73.160516,
73.178507, 73.192372, 73.206024, 73.208846, 73.214068, 73.225464,
73.236084, 73.258275, 73.268327, 73.272309, 73.269801, 73.265777
), lat = c(20.466602, 20.459483, 20.44602, 20.43615, 20.424953,
20.412235, 20.398937, 20.402725, 20.410493, 20.404022, 20.392923,
20.385058, 20.379266, 20.374414, 20.375353, 20.377813, 20.380272,
20.382634, 20.390107, 20.390654, 20.405481, 20.415684, 20.428257,
20.443762, 20.453246, 20.460464, 20.466909, 20.47418, 20.482435,
20.486774, 20.501579, 20.496404, 20.491097, 20.487117, 20.477538,
20.469934, 20.466602, 20.219381, 20.232198, 20.240647, 20.24748,
20.255641, 20.261145, 20.266745, 20.276713, 20.279753, 20.284505,
20.287736, 20.293816, 20.299704, 20.309768, 20.311575, 20.320982,
20.333331, 20.339792, 20.343499, 20.343211, 20.346733, 20.349013,
20.353571, 20.362402, 20.374462, 20.372372, 20.361166, 20.351196,
20.342554, 20.331159, 20.330208, 20.330965, 20.330202, 20.329626,
20.325824, 20.314522, 20.304079, 20.303419, 20.292882, 20.28424,
20.276552, 20.275796, 20.266398, 20.258519, 20.251874, 20.241429,
20.228231, 20.216457, 20.204968, 20.193763, 20.18094, 20.184923,
20.182923, 20.183772, 20.186809, 20.198582, 20.209307, 20.209965,
20.198458, 20.186199, 20.173564, 20.166254, 20.15885), order = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L,
58L, 59L, 60L, 61L, 62L, 63L), hole = c(FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), piece = structure(c(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, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("1", "2"), class = "factor"), group = structure(c(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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), .Label = c("0.1", "1.1", "2.1", "3.1", "4.1", "5.1",
"5.2", "6.1", "7.1", "8.1", "9.1", "10.1", "11.1", "12.1", "13.1",
"14.1", "15.1", "16.1", "17.1", "18.1", "19.1", "20.1", "21.1",
"22.1", "23.1", "24.1", "25.1", "26.1", "27.1", "28.1", "29.1",
"30.1", "31.1", "32.1", "33.1", "34.1", "35.1", "36.1", "37.1",
"38.1", "39.1", "40.1", "41.1", "42.1", "43.1", "44.1", "45.1",
"46.1", "47.1", "48.1", "49.1", "50.1", "51.1", "52.1", "53.1",
"54.1", "55.1", "56.1", "57.1", "58.1", "59.1", "60.1", "61.1",
"62.1", "63.1", "64.1", "65.1", "66.1", "67.1", "68.1", "69.1",
"70.1", "71.1", "71.2", "72.1", "73.1", "74.1", "75.1", "76.1",
"77.1", "78.1", "79.1", "80.1", "81.1", "82.1", "83.1", "84.1",
"85.1", "86.1", "87.1", "87.2", "88.1", "88.2", "89.1", "90.1",
"91.1", "92.1", "93.1", "94.1", "95.1", "96.1", "97.1", "98.1",
"99.1", "100.1", "101.1", "102.1", "103.1", "104.1", "105.1",
"106.1", "107.1", "108.1", "109.1", "110.1", "111.1", "112.1",
"113.1", "114.1", "115.1", "116.1", "117.1", "118.1", "119.1",
"120.1", "121.1", "122.1", "123.1", "124.1", "125.1", "126.1",
"127.1", "128.1", "129.1", "130.1", "131.1", "132.1", "133.1",
"134.1", "135.1", "136.1", "137.1", "138.1", "139.1", "140.1",
"141.1", "142.1", "143.1", "144.1", "145.1", "146.1", "147.1",
"148.1", "149.1", "150.1", "151.1", "152.1", "153.1", "154.1",
"155.1", "156.1", "157.1", "158.1", "159.1", "160.1", "161.1",
"162.1", "163.1", "164.1", "165.1", "166.1", "167.1", "168.1"
), class = "factor"), id = c("0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1")), .Names = c("long", "lat", "order", "hole", "piece",
"group", "id"), row.names = c(NA, 100L), class = "data.frame")
dput(head(simdat, 100))
structure(list(state = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 14L, 14L, 14L, 14L), .Label = c("", "Andhra Pradesh", "ASSAM",
"Bihar", "Chandigarh", "CHHATTISGARH", "DADRA & NAGAR HAVELI",
"DAMAN & DIU", "Delhi", "GOA", "GUJARAT", "Haryana", "Himachal",
"Jharkhand", "KARNATAKA", "KERALA", "MAHARASHTRA", "MANIPUR",
"MEGHALAYA", "MP", "Odissa", "PONDICHERRY", "Punjab", "Rajasthan",
"TAMIL NADU", "TRIPURA", "UP", "Uttrakhand", "WEST BENGAL"), class = "factor"),
long = c(78.30000305, 78.30000305, 78.30000305, 78.30000305,
78.30000305, 78.30000305, 78.30000305, 78.30000305, 91.5,
91.5, 91.5, 91.5, 91.5, 91.5, 91.5, 91.5, 85.12999725, 85.12999725,
85.12999725, 85.12999725, 85.12999725, 85.12999725, 85.12999725,
85.12999725, 76.798553, 76.798553, 76.798553, 76.798553,
76.798553, 76.798553, 76.798553, 76.798553, 81.62999725,
81.62999725, 81.62999725, 81.62999725, 81.62999725, 81.62999725,
81.62999725, 81.62999725, 72.96666718, 72.96666718, 72.96666718,
72.96666718, 72.96666718, 72.96666718, 72.96666718, 72.96666718,
72.806396, 72.806396, 72.806396, 72.806396, 72.806396, 72.806396,
72.806396, 72.806396, 72.806396, 72.806396, 72.806396, 72.806396,
72.806396, 72.806396, 72.806396, 72.806396, 73.96992109,
73.96992109, 73.96992109, 73.96992109, 73.96992109, 73.96992109,
73.96992109, 73.96992109, 72.40000153, 72.40000153, 72.40000153,
72.40000153, 72.40000153, 72.40000153, 72.40000153, 72.40000153,
75.959473, 75.959473, 75.959473, 75.959473, 75.959473, 75.959473,
75.959473, 75.959473, 75.959473, 75.959473, 75.959473, 75.959473,
75.959473, 75.959473, 75.959473, 75.959473, 85.33000183,
85.33000183, 85.33000183, 85.33000183), lat = c(17.20000076,
17.20000076, 17.20000076, 17.20000076, 17.20000076, 17.20000076,
17.20000076, 17.20000076, 26.09000015, 26.09000015, 26.09000015,
26.09000015, 26.09000015, 26.09000015, 26.09000015, 26.09000015,
25.37000084, 25.37000084, 25.37000084, 25.37000084, 25.37000084,
25.37000084, 25.37000084, 25.37000084, 30.744196, 30.744196,
30.744196, 30.744196, 30.744196, 30.744196, 30.744196, 30.744196,
21.22999954, 21.22999954, 21.22999954, 21.22999954, 21.22999954,
21.22999954, 21.22999954, 21.22999954, 20.26666641, 20.26666641,
20.26666641, 20.26666641, 20.26666641, 20.26666641, 20.26666641,
20.26666641, 20.25189, 20.25189, 20.25189, 20.25189, 20.25189,
20.25189, 20.25189, 20.25189, 20.25189, 20.25189, 20.25189,
20.25189, 20.25189, 20.25189, 20.25189, 20.25189, 15.38429276,
15.38429276, 15.38429276, 15.38429276, 15.38429276, 15.38429276,
15.38429276, 15.38429276, 23.03000069, 23.03000069, 23.03000069,
23.03000069, 23.03000069, 23.03000069, 23.03000069, 23.03000069,
29.017748, 29.017748, 29.017748, 29.017748, 29.017748, 29.017748,
29.017748, 29.017748, 29.017748, 29.017748, 29.017748, 29.017748,
29.017748, 29.017748, 29.017748, 29.017748, 23.35000038,
23.35000038, 23.35000038, 23.35000038), value = c(134L, 3L,
1098L, 16645L, 123L, 2070L, 37L, 66L, 2L, 4131L, 1L, 21L,
0L, 127L, 5L, 651L, 220L, 260L, 1827L, 17596L, 20L, 0L, 0L,
22L, 16L, 0L, 2L, 0L, 18L, 450L, 0L, 22L, 30L, 41L, 56L,
3L, 53L, 66L, 11L, 4674L, 0L, 28L, 0L, 1L, 328L, 3L, 0L,
33L, 0L, 34L, 5L, 5L, 11L, 0L, 440L, 0L, 0L, 318L, 16L, 122L,
2990L, 35L, 0L, 44L, 1L, 671L, 0L, 43L, 0L, 302L, 0L, 0L,
0L, 40L, 10054L, 379L, 1492L, 160L, 77L, 385L, 4348L, 0L,
157L, 59L, 0L, 12L, 334L, 159L, 2013L, 12L, 0L, 3L, 29L,
0L, 15L, 16L, 0L, 49L, 10L, 329L), variable = structure(list(
fact = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
4L, 7L, 8L, 2L, 3L, 5L, 6L, 3L, 1L, 6L, 4L, 8L, 2L, 7L,
5L, 8L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 7L, 2L, 3L, 1L,
6L, 5L, 4L, 5L, 1L, 7L, 8L, 4L, 6L, 2L, 3L, 7L, 6L, 8L,
1L, 5L, 3L, 4L, 2L, 7L, 6L, 5L, 8L, 4L, 1L, 2L, 3L, 5L,
4L, 8L, 6L, 7L, 3L, 2L, 1L, 2L, 7L, 4L, 5L, 6L, 3L, 8L,
1L, 4L, 7L, 6L, 3L, 2L, 5L, 1L, 8L, 4L, 3L, 7L, 5L, 6L,
2L, 8L, 1L, 2L, 1L, 5L, 3L), .Label = c("Buddhist", "Budhist",
"Christian", "Hindu", "Jain", "Muslim", "Others", "Sikh"
), class = "factor")), .Names = "fact", row.names = c(1L,
29L, 57L, 85L, 113L, 141L, 169L, 197L, 2L, 86L, 170L, 198L,
30L, 58L, 114L, 142L, 59L, 3L, 143L, 87L, 199L, 31L, 171L,
115L, 200L, 172L, 4L, 32L, 60L, 88L, 116L, 144L, 201L, 173L,
33L, 61L, 5L, 145L, 117L, 89L, 118L, 6L, 174L, 202L, 90L,
146L, 34L, 62L, 175L, 147L, 203L, 7L, 119L, 63L, 91L, 35L,
176L, 148L, 120L, 204L, 92L, 8L, 36L, 64L, 121L, 93L, 205L,
149L, 177L, 65L, 37L, 9L, 38L, 178L, 94L, 122L, 150L, 66L,
206L, 10L, 95L, 179L, 151L, 67L, 39L, 123L, 11L, 207L, 96L,
68L, 180L, 124L, 152L, 40L, 208L, 12L, 41L, 13L, 125L, 69L
), class = "data.frame")), .Names = c("state", "long", "lat",
"value", "variable"), row.names = c(NA, 100L), class = "data.frame")
There is an known issue with ggsubplot and R3.1.0. See more on this here and here.
I can plot your data with ggplot but not with ggsubplot
library(ggplot2)
df$fact <- df$variable$fact
df$state <- as.character(df$state)
df <- df[ ,-5]
ggplot(df, aes(x = fact, y = value, fill = fact)) +
geom_bar(stat = 'identity', position = 'dodge') +
facet_wrap(~state) +
theme(axis.text.x = element_blank()
,axis.title.x = element_blank()
,axis.ticks.x = element_blank()
,strip.text = element_text(size = 8))
If you want to represent your data with pies, you could try something like
library(dplyr)
df1 <- df %>%
group_by(state) %>%
mutate(tsum = sum(value),
prop = value/tsum)
ggplot(df1, aes(x=factor(1), y=prop, fill = fact1)) +
geom_bar(stat="identity") +
coord_polar(theta = 'y') +
facet_wrap(~ state, ncol = 4) +
theme(axis.text.x = element_blank()
,axis.title.x = element_blank()
,axis.text.y = element_blank()
,axis.title.y = element_blank()
,axis.ticks = element_blank()
,strip.text = element_text(size = 8))
For subplot, something like this should work:
library(maptools)
data(wrld_simpl)
india <- wrld_simpl[wrld_simpl$NAME == 'India', ]
dfindia <- fortify(india)
p <- ggplot() +
geom_polygon(data=dfindia, aes(x=long, y=lat,group=group)) +
geom_coord()
p + geom_subplot(data=simdat,
aes(long, lat, group=state,
subplot = geom_bar(aes(x=fact, y=value, fill = state),
stat="identity")), width = 10, height=10)