[R] Adding legend

John Wasige johnwasige at gmail.com
Wed Jun 3 18:32:35 CEST 2015


​Hello community,

Could somebody help on how I can a legend to my heatmap plot. I need to
know what values from the data do colours ( "red","yellow",
"yellowgreen","lightblue4") represent from the data. The script is here
below and attached is the test dataset. Thanks for your help.

Hohn

########## script
​

​library(wq)

setwd("D:/data")
Pmid <- read.csv('D:/data/MyData1.csv')

Pmid.ts = ts(Pmid,start=c(1981,1),end=c(1995,12),frequency = 12)

# convert time series into vector
Pmid.ts.df = ts2df(Pmid.ts, mon1 = 1, addYr = F, omit = FALSE)
# convert vector into data matrix
Pmid.ts.dm = data.matrix(Pmid.ts.df)
# covnert data matrix into heatmap
Pmid_heatmap <- heatmap(Pmid.ts.dm, Rowv=NA, Colv=NA, col =
c("red","yellow", "yellowgreen","lightblue4"), scale="row")
# you can transpose the dataframe if you want to flip the image
Pmid_heatmap <- heatmap(t(Pmid.ts.dm), Rowv=NA, Colv=NA, col =
c("red","yellow", "yellowgreen","lightblue4"), scale="column")

##############

# example plot
jpeg(filename = "heatMap01.jpg",
     width = 800, height = 400, units = "px", pointsize = 6,
     quality = 100, bg = "white", res = 144, type = "cairo")
heatmap(t(Pmid.ts.dm), Rowv=NA, Colv=NA, col = c("red","yellow",
"yellowgreen","lightblue4"), scale="column", legend(40,
2,legend=c("Title","","Group1","Group2"), fill=c("white", "white",
"green","black"), border=FALSE, bty="n", y.intersp = 0.7, cex=0.7))
dev.off()​


More information about the R-help mailing list