r - How to put legend right next to heatmap.2 color key -


i have following data frame:

dat <- structure(list(go = structure(c(1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l,  2l, 2l, 3l, 3l, 3l, 4l, 5l, 5l, 5l, 5l, 5l, 5l), .label = c("apoptotic process",  "metabolic process", "negative regulation of apoptotic process",  "positive regulation of apoptotic process", "signal transduction" ), class = "factor"), probegene = structure(c(14l, 15l, 2l, 12l,  7l, 11l, 16l, 8l, 19l, 13l, 3l, 1l, 18l, 4l, 10l, 5l, 9l, 17l,  20l, 6l), .label = c("1416787_at acvr1", "1418835_at phlda1",  "1419282_at ccl12", "1423240_at src", "1424896_at gpr85", "1434186_at lpar4",  "1434670_at kif5a", "1440374_at pde1c", "1440681_at chrna7",  "1440803_x_at tacr3", "1442017_at loc101056574", "1448815_at ogg1",  "1448821_at tyr", "1451338_at nisch", "1454721_at arel1", "1456300_at ilvbl",  "1456989_at oxgr1", "1457580_at chd8", "1457827_at arsj", "1460657_at wnt10a" ), class = "factor"), foo = c(1.412475312, 1.413647397, 1.41297239,  -0.707106781, -0.707106781, -0.707106781, -0.707106781, -0.707106781,  -0.707106781, -0.707106781, -0.707106781, -0.707106781, -0.707106781,  -0.707106781, -0.707106781, -0.707106781, -0.707106781, -0.707106781,  -0.707106781, -0.707106781), bar = c(-0.645532476, -0.741475951,  -0.655185417, -0.707106781, -0.707106781, -0.707106781, -0.707106781,  -0.707106781, -0.707106781, -0.707106781, -0.707106781, -0.707106781,  -0.707106781, -0.707106781, -0.707106781, -0.707106781, -0.707106781,  -0.707106781, -0.707106781, -0.707106781), aux = c(-0.766942837,  -0.672171445, -0.757786973, 1.414213562, 1.414213562, 1.414213562,  1.414213562, 1.414213562, 1.414213562, 1.414213562, 1.414213562,  1.414213562, 1.414213562, 1.414213562, 1.414213562, 1.414213562,  1.414213562, 1.414213562, 1.414213562, 1.414213562)), .names = c("go",  "probegene", "foo", "bar", "aux"), row.names = c(50l, 35l, 45l,  74l, 61l, 101l, 96l, 68l, 69l, 75l, 113l, 127l, 109l, 135l, 150l,  152l, 183l, 190l, 197l, 191l), class = "data.frame") 

and following code:

library(gplots) dat.tmp <- dat dat.tmp$go <- null rownames(dat.tmp) <- dat.tmp$probegene dat.tmp$probegene <- null  pdf("output.pdf",width=10,height=20) heatmap.2(as.matrix(dat.tmp),margin=c(5,15),dendrogram="none",trace="none",scale="row",           rowv=false, rowsidecolors=as.character(as.numeric(dat$go)))  legend("topright",           legend = unique(dat$go),     col = unique(as.numeric(dat$go)),      lty= 1,                  lwd = 5,                cex=.7     ) dev.off() 

i can following picture. enter image description here

my question how can put legend right next color key shown above. note size of pdf paper can varied according user specification.

i guess bottom line don't know how exact coordinate legend. please advice.

you can find coordinates of points on graph using locator() function. in case can try this:

heatmap.2(as.matrix(dat.tmp),margin=c(5,15),dendrogram="none",trace="none",scale="row",           rowv=false, rowsidecolors=as.character(as.numeric(dat$go))) coords <- locator(1) # click on graph want legend  pdf("output.pdf",width=10,height=20) heatmap.2(as.matrix(dat.tmp),margin=c(5,15),dendrogram="none",trace="none",scale="row",           rowv=false, rowsidecolors=as.character(as.numeric(dat$go)))  legend(coords,           legend = unique(dat$go),     col = unique(as.numeric(dat$go)),      lty= 1,                  lwd = 5,                cex=.7     ) dev.off() 

the coordinates in range of 0 1.

to plot outside range of 0 1, need use par(xpd=true). xpd=true can set in call legend, well:

pdf("output.pdf",width=10,height=20) heatmap.2(as.matrix(dat.tmp),margin=c(5,15),dendrogram="none",trace="none",scale="row",           rowv=false, rowsidecolors=as.character(as.numeric(dat$go)))  legend(y=1.1, x=.25, xpd=true,          legend = unique(dat$go),     col = unique(as.numeric(dat$go)),      lty= 1,                  lwd = 5,                cex=.7     ) dev.off() 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -