[R] overlaying a set of 'grouping' lines on a plot from image()

Rajarshi Guha rajarshi.guha at gmail.com
Fri Jul 27 17:14:59 CEST 2012


Hi, I have a matrix that I am displaying via image. I would like to
obtain an image where the columns are 'grouped' (I have a grouping
variable). But I am not sure how how I can draw the lines to indicate
the grouping in the margin. I realize this is essentially waht heatmap
does but as I have a set of custom breaks and colors I wanted to
generate this via image. I can see that it is a combination of layout
& plot, but am stumped.

An example of what I'd like to end up with is at http://i.imgur.com/FbcYe.png.

The code to reproduce the image (minus the grouping lines) is given below.

Any pointers would be greatly appreciated

m <- structure(c(-5999, -5999, -5999, -5999, -5999, -5999, -5999,
-5999, -5999, -5999, -5999, -5999, -5999, -5999, -5999, -5999,
-5999, -5999, -5999, -5999, -5999, -5999, -5999, -5999, -5999,
-5999, -5999, -5999, -5999, -5999, -5999, -5999, -5999, -5999,
-5999, -5999, -5999, -5999, -5999, -5999, -5999, -5999, -5999,
-5999, -5999, -5999, -5999, -5999, -5999, -5999, -5999, -5999,
-5999, -5999, -5999, -5999, -5999, -11.8733599070146, -5999,
-5999, -5999, -18.8180072882681, -5999, -5999, -5999, -13.3221289302394,
-5999, -0.0149476745103187, -0.047268697175449, -0.334636748986896,
-0.265811418096633, -11.8733599070146, -1.49476745103187, -9.43134501518495,
-0.59507764056576, -0.265811418096633, -5.9507764056576, -14.9476745103187,
-18.8180072882681, -5999, -0.84056950926466, -0.00084056950926466,
-3.75468607850911, -0.0211141514506257, -0.000943134501518495,
-0.0084056950926466, -0.00334636748986895, -0.0053036350541665,
-1.08594949377388, -14.9476745103187, -2.36904675745286, -4.7268697175449,
-4.7268697175449, -14.9476745103187, -5999, -14.9476745103187,
-5999, -5999, -5999, -5999, -0.0118733599070146, -0.0334636748986896,
-8.4056950926466, -6.67688094457253, -5999, -13.3221289302394,
-5999, -16.7715666494766, -5.9507764056576, -5999, -1.86547984241505,
-1.32065859029132, -1.04903640669956, -13.2065859029132, -5999,
-0.934954681607439, -5999, -13.2065859029132, -4.17628915678771,
-0.0833279237095168, -18.6547984241505, -26.350603161482, -0.132065859029132,
-0.166261065962201, -0.934954681607439, -2.34849997880608, -5999,
-3.31734439360934, -5999, -1.32065859029132, -5999, -5999, -0.234849997880608,
-5999, -5999, -5999, -20.93102809318, -5999, -5999, -5999, -0.0742660901788439,
-0.148180331013928, -0.00934954681607437, -5999, -5999, -5999,
-5999, -5999, -5999, -5999, -5999, -5999, -5999, -5999, -5999,
-0.00132065859029132, -0.0331734439360934, -5999, -0.0331734439360934,
-5999, -5.89916523116354, -0.148180331013928, -5999, -0.0234849997880608,
-5999, -5999, -5.25763654648048, -0.04685873504417, -5999, -11.7703820755153,
-4.685873504417, -1.04903640669956, -5999, -0.295658630291705,
-0.295658630291705, -5999, -18.6547984241505, -0.0589916523116354,
-0.104903640669956, -1.66261065962201, -0.742660901788439, -5999,
-5999, -5.89916523116354, -7.42660901788439, -5999, -5999, -3.31734439360934,
-20.93102809318, -5999, -5999, -5999, -5999, -0.0833279237095168,
-5999, -0.0742660901788439, -0.0117703820755153, -0.372212162890483,
-1.70618671242265, -5999, -5999, -5999, -5999, -5999, -5999,
-6.618972254342, -5999, -20.93102809318, -5999, -5999, -5999,
-0.0148180331013928, -5999, -0.132065859029132, -5999, -5999,
-0.117703820755153, -0.026350603161482, -18.6547984241505, -1.70618671242265,
-0.186547984241505, -0.0295658630291705, -1.32065859029132, -0.26350603161482,
-0.0331734439360934, -0.372212162890483, -0.331734439360934,
-0.589916523116354, -0.4685873504417, -0.0234849997880608, -1.48180331013928,
-14.8180331013928, -0.04685873504417, -0.0934954681607439, -0.0833279237095168,
-0.4685873504417, -18.6547984241505, -0.132065859029132, -7.42660901788439,
-0.417628915678771, -7.42660901788439, -2.34849997880608, -0.4685873504417,
-20.93102809318, -3.72212162890483, -0.166261065962201, -0.004685873504417,
-37.2212162890483, -0.0331734439360934, -5999, -0.0104903640669956,
-5999, -5999, -0.270412370558627, -5999, -33.1734439360934, -20.93102809318,
-20.93102809318, -5999, -0.2093102809318, -0.4685873504417, -18.6547984241505,
-18.6547984241505, -5999, -5999, -5999, -0.00934954681607437,
-5999, -0.0166261065962201, -23.4849997880608, -0.0234849997880608,
-0.00589916523116354, -0.0117703820755153, -0.00372212162890484,
-5999, -5.89916523116354, -0.0589916523116354, -0.0934954681607439,
-5999, -372.212162890483, -0.417628915678771, -1.66261065962201,
-3.31734439360934, -2.34849997880608, -0.0417628915678771, -58.9916523116354,
-5999, -0.0417628915678771, -0.0525763654648048, -0.117703820755153,
-0.166261065962201, -8.33279237095168, -0.0833279237095168, -11.7703820755153,
-0.417628915678771, -20.93102809318, -3.72212162890483, -0.234849997880608,
-4.17628915678771, -5999, -0.833279237095168, -0.0589916523116354,
-37.2212162890483, -0.0934954681607439, -5999, -0.00833279237095168,
-5999, -5999, -0.0605377890099972, -2.6350603161482, -5999, -5999,
-5999, -5999, -0.0186547984241505, -0.589916523116354, -0.00372212162890484,
-5999, -5999, -23.4849997880608, -5999, -5999, -5999, -5999,
-5999, -1.86547984241505, -0.026350603161482, -0.02093102809318,
-0.104903640669956, -5999), .Dim = c(55L, 6L), .Dimnames = list(
    c("170", "150", "283", "118", "292", "293", "307", "374",
    "443", "151", "18", "44", "276", "387", "411", "388", "259",
    "458", "261", "146", "108", "433", "95", "99", "301", "80",
    "193", "77", "233", "234", "241", "242", "217", "122", "254",
    "414", "184", "184.1", "13", "28", "64", "213", "215", "216",
    "86", "79", "88", "128", "29", "218", "263", "329", "123",
    "3", "121"), c("Col1", "Col2", "Col3", "Col4", "Col5", "Col6"
    )))

cols <- c("#000000", "#190000", "#330000", "#4C0000", "#660000", "#7F0000",
"#990000", "#B20000", "#CC0000", "#E50000", "#FF0000", "#00FF00",
"#00AA00", "#005500", "#000000")

br <- c(-6000, -10, -1, -0.888888888888889, -0.777777777777778,
-0.666666666666667,
-0.555555555555556, -0.444444444444444, -0.333333333333333, -0.222222222222222,
-0.111111111111111, 0, 0, 1, 10, 6000)


m <- t(m[nrow(m):1,ncol(m):1])
par(las=2, mar=c(8,8,1,1))
image(t(m), ylab='', xlab='', axes=FALSE, col=cols, breaks=br, useRaster=FALSE)
axis(2, at=seq(0,1,length=6), labels=rownames(m), font=2)
axis(1, labels = FALSE, tick=FALSE)
labels <- sprintf("Compound%d", 1:ncol(m))
text(seq(0, 1, length=ncol(m)), par("usr")[3] - 0.1, srt = 45, adj = 1,
          labels = labels, xpd = TRUE, col='#666666', cex=0.8)



-- 
Rajarshi Guha | http://blog.rguha.net
NIH Center for Advancing Translational Science



More information about the R-help mailing list