Skip to content

Instantly share code, notes, and snippets.

@TonyLadson
Last active December 3, 2018 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TonyLadson/40405ff618fc67d522a5c3397bd89bed to your computer and use it in GitHub Desktop.
Save TonyLadson/40405ff618fc67d522a5c3397bd89bed to your computer and use it in GitHub Desktop.
library(gplots)
library(RColorBrewer)
library(tidyverse)
library(readxl)
# Read in Example data set
x_heatmap <-
structure(c(13.9502940367395, 7.39569293125532, 6.15475001138169,
-1.53331909123808, 21.6456648266083, -4.98804838513024, 19.8466741702892,
-6.44002503636293, 14.1588900525682, 61.6400164560741, 0.784434268958867,
-3.28879917282611, 32.8051784357987, -8.86192271824926, -83.9835081820656,
-30.8005519677419, 17.5031339497212, 5.14611178200692, 8.38567275410518,
0.958791676852852, 7.78913933229633, -11.0589507112093, -36.2760980721563,
9.62281417072285, -13.5850909170695, 8.85527173387818, -25.4071145031834,
-7.76912651421502, -13.5880785859609, 1.86546582617797, NA, 11.5323998475261,
17.3767046619952, 3.28696203924716, -12.9426137195807, 14.3268296600319,
3.96673202544451, 3.4243386751879, 4.71081253094599, -10.2879481241573,
-8.718336443156, 35.8017749096267, 9.74934629727155, -6.83619707175531,
-15.6810813153535, -3.35411306256428, -14.5791834979225, -5.97203855618835,
4.38603910766542, -7.30666756868363, -15.6278424889594, -2.1198092925828,
-0.0964944122778252, 12.4921374646761, -12.0219641341409, -7.26842138575856,
27.0572765221214, -30.4879995788541, 14.0256650914438, 0.959137403327041,
-9.89464399519377, 4.06672165969852, -11.0910108792968, -23.1777194399294,
-3.88103792236187, -8.44736272362061, 4.00674646220636, 5.09133370663039,
-5.75149603324011, -54.8641366821528, 4.48976010317914, -42.2288215345424,
-3.8845107563911, 8.19819589080289, NA, -19.0360950085893, -18.0866381490603,
2.40825676602777, -9.44641941100359, -7.09979832664132, -14.7052752462309,
-5.76590772338677, -5.757502195118, -31.6813535975106, 3.06272368228994,
9.01463740805164, -25.9468056376278, 12.1454156580474, 18.6368788440758,
22.3321261882782, 6.87869223535061, NA, -2.0277420639433, -13.0463509578165,
-1.53288496024907, 1.78218296695501, NA, -7.14508345839102, -4.7002600567136,
-7.9046472414583, -39.2770282735722, -1.17893081693444, -9.75426942110062,
-2.40834974159952, 29.9521364361048, -1.5508740228368, -20.3062281343527,
1.19439534105826, -15.969897025153, 12.9297293107957, -20.4215624584351,
9.78763260840438, 10.3915907159448, -7.22812802646775, -29.067723986553,
-2.94263822101057, NA, -49.7033833488962, -11.2488803648856,
-27.0731444200873, 2.42749864491634, -20.510688228542, -27.6026869288273,
13.0924537151866, 13.7791623247601, -8.28674682845362, -14.1270229464211,
-27.6858277225029, -8.67652709774673, 1.9740800251253, -0.759669456938282,
-38.2768833635375, -26.461547044497, -10.7297602787055, -20.5232824373292,
-56.1515600208007, -19.4300647956599, -19.7543981516734, -22.0711002564244,
-28.3578422877193, -25.1968224477302, -25.7530601119855, -12.9184670776129,
-10.7260548649589, -19.484543184978, -4.35137430473696, -19.8103751286492,
19.7147440080298, -2.41187708266079, 32.1495291795535, -51.3464536828827,
-9.45189119556919, 10.2824283615919, 5.64197351418435, -0.370531297512352,
-13.3444960027467, 5.47646291251294, -10.7195454601524, 23.3831262757257,
-3.09433986331802, 8.02258797872812, -2.69097540177405, -4.58219458369538,
66.296106967465, 8.40251361353323, 0, 0.869594596959651, 2.14440438875463,
9.49653664240614, 20.717976805754, 8.81436144587584, 26.3561988448724,
-3.9584973235894, -38.1460538399359, -10.7028392008133, NA, 8.37632292732597,
24.9601398383128, NA, 2.93176620677114, -18.5449097687379, 4.68551209708676,
1.53152237751056, -5.64017869531643, -4.65148914545774, -3.36040477737784,
-50.839454568103, 15.3330467030266, -0.753178681163117, -6.72806442684028,
8.13427241970785, -3.06703614875674, 18.7296103105694, -8.57197375729215,
-51.8875066573219, 55.3193448656239, -5.26444438445382, 25.6798654656671,
-0.0918370967265218, -1.3608565715421, -37.3026829709299, -3.89410855807364,
-43.4630676939897, -34.4371045324253, -9.32469971891958, -5.65960305716842,
-10.7260915101506, -13.6616510049021, 1.07429663403891, -9.12284462704323,
15.2988116218802, -18.6200854527857, 2.3401807664521, -4.05125400619581,
-5.71409829691052, -26.3568105489016, -8.49885824769735, -7.46157363476232,
0.601456465842202, NA, -0.0989437575638294, NA, -42.9533264295012,
1.09241600342095, -9.8306808836339, -3.46855304083787, -23.4186584881973,
15.6208881082432, -28.4351161509147, -12.9123996743001, -16.3801739466377,
1.00737802985124, -30.1949222749472, -29.448516168613, 0.419845598880202,
21.3914003732614, -33.0279120313469, -11.4557259012479, 10.7308924691845,
15.4260552014923, -28.7311940484587, 54.2052209219523, 2.01198382548988,
-17.9034530249191, -86.2563302343898, -54.5695097724348, -2.45424612504896,
1.24732661794871, -5.73815166465938, -42.601716024382, -50.3902720021782,
-3.1682867890317), .Dim = c(21L, 12L), .Dimnames = list(NULL,
c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec")))
# Select colors -----------------------------------------------------------
# Suitable colours. Palette that goes from negative to positive with zero in the middle
# See Rcolourbrewer
# https://www.rdocumentation.org/packages/RColorBrewer/versions/1.1-2/topics/RColorBrewer
coul = colorRampPalette(brewer.pal(8, "PRGn"))(11) # Expand to an uneven number so there is a zero in the middle
# Base R heat map ---------------------------------------------------------
heatmap(x_heatmap, Rowv = NA, Colv = NA, col=coul, labRow = 1954:1974)
# Heatmap from gplots -----------------------------------------------------
gplots::heatmap.2(x_heatmap, scale="none", col = coul, Colv=FALSE, trace = "none", dendrogram = 'none', Rowv = FALSE,
labRow = 1954:1974,
key = TRUE,
keysize = 2,
key.xlab = "Difference in monthly rainfall (mm)",
key.ylab = "Count",
key.title = "Colour Key and Histogram",
xlab = 'Month',
ylab = 'Year')
# Heatmap in ggplot -------------------------------------------------------
# Convert to datafream and ddd a column of years
x1 <- as_data_frame(x_heatmap) %>% mutate(Year = 1954:1974)
p <- x1 %>%
tidyr::gather(-Year, key = 'Month', value = 'Difference') %>%
ggplot(aes(x=as_factor(Month), y=Year, fill = Difference)) +
scale_fill_distiller(name = 'Difference (mm)',
type = 'div', palette = "PRGn", limits = c(NA, 80)) +
scale_x_discrete(name = "Month", expand = expand_scale(mult = 0, add = 0)) +
scale_y_continuous(breaks = 1954:1974, expand = expand_scale(mult = 0, add = 0)) +
geom_tile() +
theme(legend.position = 'bottom', text = element_text(size = 8))
p
# Save in a form that look ok in powerpoint
# see https://minimaxir.com/2017/08/ggplot2-web/
ggsave("../figures/heatmap.png", p, width=4, height=3)
# Values over heatmap -----------------------------------------------------
p2 <- x1 %>%
tidyr::gather(-Year, key = 'Month', value = 'Difference') %>%
ggplot(aes(x=as_factor(Month), y=Year, fill = Difference)) +
scale_fill_distiller(name = 'Difference (mm)',
type = 'div', palette = "PRGn", limits = c(NA, 80)) +
scale_x_discrete(name = "Month", expand = expand_scale(mult = 0, add = 0)) +
scale_y_continuous(breaks = 1954:1974, expand = expand_scale(mult = 0, add = 0)) +
geom_tile() +
geom_text(aes(label = round(Difference)), colour = 'grey60', size = 2) +
theme(legend.position = 'bottom', text = element_text(size = 8))
p2
ggsave("../figures/heatmap_table.png", p2, width=4, height=3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment