Wednesday, April 17, 2013

RG#73: triple (three) heatmap plot - one center (XY) and other two at X and Y axis direction


 library(ggplot2);
 library(reshape2)
 library (grid)

#X axis quantitaive ggplot data
datfx <- data.frame(indv=factor(paste("ID", 1:20, sep = ""),
     levels =rev(paste("ID", 1:20, sep = ""))), matrix(sample(LETTERS[1:7],80, T), ncol = 4))
 # converting data to long form for ggplot2 use
  datf1x <- melt(datfx, id.var = 'indv')
plotx <-  ggplot(datf1x, aes(indv, variable)) + geom_tile(aes(fill = value),
 colour = "white")  +   scale_fill_manual(values= terrain.colors(7))+ scale_x_discrete(expand=c(0,0))
px <- plotx

#Y axis quantitaive ggplot data
datfy <- data.frame(indv=factor(paste("ID", 21:40, sep = ""),
     levels =rev(paste("ID",21:40, sep = ""))), matrix(sample(LETTERS[7:10],100, T), ncol = 5))
 # converting data to long form for ggplot2 use
  datf1y <- melt(datfy, id.var = 'indv')
ploty <-  ggplot(datf1y, aes( variable, indv)) + geom_tile(aes(fill = value),
 colour = "white")  +   scale_fill_manual(values= c("cyan4", "midnightblue", "green2", "lightgreen")) + scale_x_discrete(expand=c(0,0))
py <- ploty  +  theme(legend.position="left",  axis.title=element_blank())
 )

# plot XY quantative fill
datfxy <- data.frame(indv=factor(paste("ID", 1:20, sep = ""),
     levels =rev(paste("ID", 1:20, sep = ""))), matrix(rnorm (400, 50, 10), ncol = 20))
names (datfxy) <- c("indv",paste("ID", 21:40, sep = ""))
 datfxy <- melt(datfxy, id.var = 'indv')
  levels (datfxy$ variable) <- rev(paste("ID", 21:40, sep = ""))
pxy <- plotxy <-  ggplot(datfxy, aes(indv, variable)) + geom_tile(aes(fill = value),
 colour = "white")  + scale_fill_gradient(low="red", high="yellow") + theme(
        axis.title=element_blank())



#Define layout for the plots (2 rows, 2 columns)
layt<-grid.layout(nrow=2,ncol=2,heights=c(6/8,2/8),widths=c(2/8,6/8),default.units=c('null','null'))
#View the layout of plots
grid.show.layout(layt)

#Draw plots one by one in their positions
grid.newpage()
pushViewport(viewport(layout=layt))
print(py,vp=viewport(layout.pos.row=1,layout.pos.col=1))
print(pxy,vp=viewport(layout.pos.row=1,layout.pos.col=2))
print(px,vp=viewport(layout.pos.row=2,layout.pos.col=2))






No comments:

Post a Comment

Note: Only a member of this blog may post a comment.