I am trying to make a geom_tile graph with ggplot2, where I color each cell based on a value. However, the colors in my graph keep getting messed up, where cells overlap each other, which looks terrible. How do I properly assign colors to groups of values ("perc") in my data, which is below. Or, maybe I'm not producing the graph correctly - I don't know. Yes, it's quite a bit of data, but the range of values needs to be there to produce the error. My code looks like this:
ggplot(OMO_df) +
geom_tile(data = OMO_df %>% filter(perc < -10),
aes(x = index, y = station), fill = 'blue', color='black') +
geom_tile(data = OMO_df %>% filter(perc >= -10 & perc <= -5),
aes(x = index, y = station), fill = 'lightblue', color= 'black') +
geom_tile(data = OMO_df %>% filter(perc > -5 & perc < 5),
aes(x = index, y = station), fill = 'white', color= 'black') +
geom_tile(data = OMO_df %>% filter(perc >= 5 & perc < 10),
aes(x = index, y = station), fill = 'orange', color= 'black') +
geom_tile(data = OMO_df %>% filter(perc >= 10 & perc < 15),
aes(x = index, y = station), fill = 'orangered', color= 'black') +
geom_tile(data = OMO_df %>% filter(perc >= 15 & perc < 20),
aes(x = index, y = station), fill = 'red', color= 'black') +
geom_tile(data = OMO_df %>% filter(perc >= 20 & perc < 25),
aes(x = index, y = station), fill = 'maroon', color= 'black') +
geom_tile(data = OMO_df %>% filter(perc >= 25),
aes(x = index, y = station), fill = 'purple', color= 'black') +
geom_text(aes(label = OMO_df$Date,1,10), size = 2.5)
OMO_df <- structure(list(index = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L,
5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"BKMS2",
"BKMS2", "BWLS2", "BWLS2", "BWLS2", "BWLS2", "BWLS2", "BWLS2", "BWLS2", "BWLS2", "BWLS2",
"BWLS2", "CPTS2", "CPTS2", "CPTS2", "CPTS2", "CPTS2", "CPTS2", "CPTS2", "CPTS2", "CPTS2",
"CPTS2", "CTWS2", "CTWS2", "CTWS2", "CTWS2", "CTWS2", "CTWS2", "CTWS2", "CTWS2", "CTWS2",
"CTWS2", "EDTS2", "EDTS2", "EDTS2", "EDTS2", "EDTS2", "EDTS2", "EDTS2", "EDTS2", "EDTS2",
"EDTS2", "FLNS2", "FLNS2", "FLNS2", "FLNS2", "FLNS2", "FLNS2", "FLNS2", "FLNS2", "FLNS2",
"FLNS2", "GOTS2", "GOTS2", "GOTS2", "GOTS2", "GOTS2",
"GOTS2", "GOTS2", "GOTS2", "GOTS2", "GOTS2", "HAMS2", "HAMS2", "HAMS2", "HAMS2", "HAMS2",
"HAMS2", "HAMS2", "HAMS2", "HAMS2", "HAMS2", "HIHS2", "HIHS2", "HIHS2", "HIHS2", "HIHS2",
"HIHS2", "HIHS2", "HIHS2", "HIHS2", "HIHS2", "HMLS2", "HMLS2", "HMLS2", "HMLS2", "HMLS2",
"HMLS2", "HMLS2", "HMLS2", "HMLS2", "HMLS2", "LCMS2", "LCMS2", "LCMS2", "LCMS2", "LCMS2",
"LCMS2", "LCMS2", "LCMS2", "LCMS2", "LCMS2", "LLAS2", "LLAS2", "LLAS2", "LLAS2", "LLAS2",
"LLAS2", "LLAS2", "LLAS2", "LLAS2", "LLAS2", "MVMS2", "MVMS2", "MVMS2", "MVMS2", "MVMS2",
"MVMS2", "MVMS2", "MVMS2", "MVMS2", "MVMS2", "NISS2", "NISS2", "NISS2", "NISS2", "NISS2",
"NISS2", "NISS2", "NISS2", "NISS2", "NISS2", "NOMS2", "NOMS2",
"NOMS2", "NOMS2", "NOMS2", "NOMS2", "NOMS2", "NOMS2", "NOMS2", "NOMS2", "OCMS2", "OCMS2",
"OCMS2", "OCMS2", "OCMS2", "OCMS2", "OCMS2", "OCMS2", "OCMS2", "OCMS2", "ONMS2", "ONMS2",
"ONMS2", "ONMS2", "ONMS2", "ONMS2", "ONMS2", "ONMS2", "ONMS2", "ONMS2", "PVMS2", "PVMS2",
"PVMS2", "PVMS2", "PVMS2", "PVMS2", "PVMS2", "PVMS2", "PVMS2", "PVMS2", "RAMS2", "RAMS2",
"RAMS2", "RAMS2", "RAMS2", "RAMS2", "RAMS2", "RAMS2", "RAMS2", "RAMS2", "RBMS2", "RBMS2",
"RBMS2", "RBMS2", "RBMS2", "RBMS2", "RBMS2", "RBMS2", "RBMS2", "RBMS2", "SFSS2", "SFSS2",
"SFSS2", "SFSS2", "SFSS2", "SFSS2", "SFSS2", "SFSS2", "SFSS2", "SFSS2", "SHSS2", "SHSS2",
"SHSS2", "SHSS2", "SHSS2", "SHSS2", "SHSS2", "SHSS2", "SHSS2", "SHSS2", "STMS2", "STMS2",
"STMS2", "STMS2", "STMS2", "STMS2", "STMS2", "STMS2", "STMS2", "STMS2", "TIMS2", "TIMS2",
"TIMS2", "TIMS2", "TIMS2", "TIMS2", "TIMS2", "TIMS2", "TIMS2", "TIMS2", "UNIS2", "UNIS2",
"UNIS2", "UNIS2", "UNIS2", "UNIS2", "UNIS2", "UNIS2", "UNIS2", "UNIS2", "WRIS2", "WRIS2",
"WRIS2", "WRIS2", "WRIS2", "WRIS2", "WRIS2", "WRIS2", "WRIS2", "WRIS2", "WTES2", "WTES2",
"WTES2", "WTES2", "WTES2", "WTES2", "WTES2", "WTES2", "WTES2", "WTES2"), perc =
c(10.8596952027158, 12.6793955120162, 14.3688090043584, 20.1107800589202, 15.758568028833,
16.8172644385968, 15.1634140494197, 13.6454410759518, 12.9732362476061, 12.7977031808738,
4.91475842307557, 3.14677821877055, 8.18509978450546, 6.31434526071209, 8.42934475735039,
6.37271757879791, 4.91747853948507, 4.72551310862487, 9.94415002188186, 8.29551611749681,
4.23197080167067, 0.83869401159656, -0.491609668530869, 6.86226788871961, 8.60860320209319,
9.54126935437905, 8.27797076678211, 5.8476705058074, 5.60388372993436, 3.47315934341273,
6.59914261348369, 4.72377771635368, 4.55359703790187, 3.44591781297188, 9.40184973265457,
12.2012623265628, 7.21196795717602, 3.31050024699002, 4.93430400166571, 1.4593312590188,
13.7434971222042, 13.2500950933581, 12.22887475556, 15.954141648121, 16.8316359830251,
19.9118773075993, 17.1179852907012, 16.2949217622434, 18.8077864483272, 16.2478672171616,
3.94614246557396, 3.86982505936741, 10.6418623374362, 10.7347802417981, 10.8975729281418,
10.15398412869, 10.6197196863103, 9.38814641985167, 8.38894703186657, 9.62904542796762,
2.6344639313473, 3.46767510734253, 3.76561046723973, 3.11232118522747, 5.21537933505268,
4.88969228292394, 6.9135515910086, 4.45975654515689, 12.2649213347622, 10.5953184367168,
3.78143049512363, 2.2679576398937, 6.36147719204595, 2.00740230321336, 7.23521119357272,
9.48334940840314, 7.48967100757635, 6.94736706690368, 5.0958211344347, 8.3687212279182,
20.1043303036155, 17.8895973338967, 18.6727813247115, 22.682219562774, 23.2723465063075,
23.2511551332253, 26.1030749694413, 26.8661261047938, 23.9460765510034, 21.9492965129951,
8.26626048323328, 8.3673215215017, 9.61824385560677, 9.20979044046568, 9.43391704304401,
12.3926128270403, 8.67573263001082, 7.76939635116526, 8.27994812310088, 8.61090658906444,
16.0465771897875, 10.5782172071081, 16.9775599035995, 11.1465450005033, 16.9687145454526,
15.0143455329244, 20.7653860465877, 23.3001888725306, 16.0730331407032, 21.6430508421449,
10.5661685012682, 14.0548807187931, 13.238662004141, 12.559642112063, 9.5016160894903,
9.40844336960341, 8.69858741703188, 9.3021149540046, 13.0797505527071, 10.0048063287966,
3.31009373898424, -0.706868866832397, 10.8619105424419, 6.43359616654329, 9.96595432446051,
9.43292282237402, 9.8828004220611, 8.36244815578808, 7.13155393625116, 9.8918195990283,
9.05035786015605, 11.1039445467929, 6.73057175361838, 7.70380061435731, 10.3002241806604,
11.9515920831048, 13.096964816505, 11.991420556226, 10.8538751609936, 13.115403408827,
6.85866779201151, 12.9512177956148, 16.6218259834491, 15.7795027970622, 13.7300837343986,
15.3292745351912, 12.5300529974221, 12.4545215728066, 13.5061379302962, 9.89262368885911,
4.44755969036254, 7.67156605739487, 5.20191839699681, 6.01959540904796, 4.82852067978065,
3.69996599294462, 6.97339478479843, 7.70775227662809, 7.34184942147805, 3.43231807132849,
12.7226136749203, 11.8869480637653, 10.2290023726425, 11.4880797133438, 12.4158050084454,
9.34674799743204, 9.6041768922445, 9.91520189719971, 9.60894341142629, 10.7247392283182,
6.38424591514884, 9.05246256156325, 7.21527338251725, 8.35401552902228, 9.64811871475658,
9.71254445129234, 7.33383443972371, 6.39722193747251, 5.80527455748918, 10.5337956148182,
2.83074840432274, 8.64796880719929, 6.77100572465171, 8.09518920699475, 12.1687384901832,
10.6967910936064, 10.6010243279077, 11.724306974949, 7.67036252319477, 10.1553153075534,
5.62495998359209, 1.78572009291791, 5.6895738971779, 10.3448864977775, 11.8130816321482,
9.80583032415623, 13.2873574732078, 8.40746236329292, 11.3702098180058, 7.73027452952645,
1.26051178505845, 5.53961595325578, 8.10575494460526, 7.19259895151663, 11.9738144462778,
9.07651633537401, 10.1763642628432, 8.73947558381513, 7.85501371196549, 6.11322978319915,
9.07139843232933, 7.91106567601277, 4.83622419073259, 5.29607154544455, 5.10499240222777,
5.30998109369067, 6.03717567026125, 6.58331493414485,
8.02023315043135, 6.21584397048659, 7.96000482149377, 8.2195513563331,
9.44206586168124, 5.83366909561393, 11.4426172139855, 11.4633500622465,
8.43411298153575, 5.76386804014485, 9.95672148471038, 8.46185655707611,
14.1857366706651, 13.2137148900627, 13.1167255989471, 16.1815890156908,
10.9153026190539, 14.7467506881468, 12.6973725499409, 16.3639757656968,
20.8247705374255, 17.2875553276592, 18.0996314906739, 15.6023561661069,
14.9722381516285, 17.181802636042, 14.1127408707532, 16.2209116492806,
16.5112085510865, 16.5922226037249, 14.1009174587318, 11.9398994451681,
19.7389901281987, 24.3963626062697, 25.6158961512722, 25.4941076688579,
25.487097698273, 25.512551940601, 25.2803362038124, 21.7100148939458,
25.6016783976186, 21.5744327513095, 3.86811376311238, 6.58120311763713,
3.96800166399017, 9.16664169792036, 11.4724318870742, 6.73422340655196,
10.9647385547807, 8.50289010459322, 9.00864613346506, 9.17592074549703,
6.62881774659143, 7.54946826341028, 7.845273212469, 9.64115288004238,
11.3607044012122, 10.808363351469, 10.461604700833, 8.67454392924193,
8.2468787595503, 8.68989050692306, 6.26907137349154, 9.38454840333148,
7.81478594749248, 10.9604422076999, 9.9429680587109, 7.210394567122,
5.50935298877285, 5.82237172162489, 5.80474363729558, 7.81091357446821
)), class = "data.frame", row.names = c(NA, -290L))
Thank you in advance.
One option to fix your issue and to simplify your code considerably would be to first bin your perc
column using e.g. cut
and to use a manual color scale:
library(ggplot2)
breaks <- c(-Inf, -10, -5, 5, 10, 15, 20, 25, Inf)
pal_color <- c("blue", "lightblue", "white", "orange", "orangered", "red", "maroon", "purple")
OMO_df$perc_cut <- cut(OMO_df$perc, breaks = breaks, right = FALSE)
names(pal_color) <- levels(OMO_df$perc_cut)
ggplot(OMO_df) +
geom_tile(aes(x = index, y = station, fill = perc_cut), color='black') +
scale_fill_manual(values = pal_color, limits = names(pal_color))