0

My problem is different from this Side-by-side plots with ggplot2. Two reasons: 1) With aheatmap() function, the heatmap by aheatmap() and barplots by other functions cann't save in one picture when i use gridExtra. 2) I want "barplots correspond to the rows/columns of the heatmap". I have tried with gridExtra, but the picture changed with the different data sets.

I want to merge some barplots with a heatmap, such that a left/right barplot correspond to the rows of the heatmap, and further that a top/bottom barplot correspond to the columns of the heatmap. Like this picture (and a barplot in the bottom) MATLAB Bar graph + HeatMap/Imagesc

Now the aheatmap() in NMF package is used and the code as followings (but I cann't add barplots):

n <- 50; p <- 20
x <- abs(rmatrix(n, p, rnorm, mean=4, sd=1))
x[1:10, seq(1, 10, 2)] <- x[1:10, seq(1, 10, 2)] + 3
x[11:20, seq(2, 10, 2)] <- x[11:20, seq(2, 10, 2)] + 2
rownames(x) <- paste("ROW", 1:n)
colnames(x) <- paste("COL", 1:p) 
annotation = data.frame(Var1 = factor(1:p %% 2 == 0, labels = c("Class1", "Class2")), Var2 = 1:10)
aheatmap(x, annCol = annotation, Rowv=FALSE)

Any suggestions to add barplots using aheatmap() or even using other packages?

Community
  • 1
  • 1
chare
  • 11
  • 5
  • possible duplicate of [Side-by-side plots with ggplot2 in R](http://stackoverflow.com/questions/1249548/side-by-side-plots-with-ggplot2-in-r) – pogibas Apr 27 '15 at 06:40
  • They are different! I tried their methods and they cann't fulfill my requirements. . Note that "barplots correspond to the rows/columns of the heatmap", – chare Apr 27 '15 at 06:48
  • [This](http://stackoverflow.com/questions/24147500/r-heatmap-type-plot-with-frequency-plot/24148288#24148288) and [this](http://stackoverflow.com/questions/24932381/ggplot2-adding-stacked-barchart-to-heatmap) might help – user20650 Apr 27 '15 at 09:48
  • Thanks! http://stackoverflow.com/questions/24188986/r-ggplot2-make-two-geom-tile-plots-have-equal-height/24189239#24189239 is very helpful – chare Apr 27 '15 at 12:08

1 Answers1

0

What about something along these lines to begin with?

m <- matrix(abs(rnorm(25 * 10)), ncol = 25)

plotMyHeat <- function(x) {
  x <- t(x)
  l <- matrix(c(1,2), ncol = 2)
  layout(l)
  op <- par(mar = c(2,2,1,0))  
  image(x)
  par(mar = c(2,1,1,1))
  barplot(rowMeans(x),horiz = TRUE, yaxs = "i")
  par(op)
}

plotMyHeat(m)
ddiez
  • 1,087
  • 11
  • 26
  • It is very helpful. And http://stackoverflow.com/questions/24188986/r-ggplot2-make-two-geom-tile-plots-have-equal-height/24189239#24189239 is very intesting. – chare Apr 27 '15 at 12:07