Skip to content

Instantly share code, notes, and snippets.

@EconomiCurtis
Last active August 29, 2017 21:17
Show Gist options
  • Save EconomiCurtis/ad83b68e0828df3a80db597c1fb5b884 to your computer and use it in GitHub Desktop.
Save EconomiCurtis/ad83b68e0828df3a80db597c1fb5b884 to your computer and use it in GitHub Desktop.
R Heatmap Notes
# Instructions
# `d` is the example datastructure
# `n` in kde2d is the granularity of the heatmap. It's how many pixels appears on the vertical and horizontal axes (total pixels is n^2)
# `n` in colours / rainbow controls the colors of the heatmap rainbow.
# `cuts` in levelplot also controls the smoothness of the color bleeding between pixels.
# - I don't think `cuts` can be set to more than the integer in `col.regions=rgb.palette(150)`.
# package
require(lattice)
require(MASS)
# original data
d <- structure(list(
X = c(-60L, 60L, 7L, -22L, 59L, 29L, -58L, 60L,
7L, -21L, 61L, 29L, -57L, -22L, 59L, 29L, -56L, 61L, 8L, -20L,
62L, 30L),
Y = c(-18L, -62L, 14L, -60L, 58L, 22L, -18L, -61L,
14L, -59L, 59L, 22L, -18L, -59L, 60L, 24L, -17L, -60L, 16L, -58L,
60L, 23L)),
.Names = c("X", "Y"), class = "data.frame", row.names = c(NA, -22L))
# use mass' kde2d to convert x,y to matrix
dens <- kde2d(d$X, d$Y, h=75, n=50) #overrode default bandwidth
# insert notes on smoothing matrix.....
# Colors
n=10
colours = rainbow(n, s = 1, v = 1, start = 0, end = max(1, n - 1)/n, alpha = 1)
rgb.palette <- colorRampPalette(colours, space = "Lab")
# The use of levelplot is really easy then:
levelplot(dens$z, col.regions=rgb.palette(150), scales=list(x=list(cex=.3), y=list(cex=.3)),
main="Main Title",
xlab="Samples",
ylab="Samples" ,
cuts = 150)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment