Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8750325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:50:49+00:00 2026-06-13T12:50:49+00:00

I want to visualize a 42×42 matrix as 28 separate heatmaps, each heatmap being

  • 0

I want to visualize a 42×42 matrix as 28 separate heatmaps, each heatmap being 6×6 matrix with the values plotted on the top of colours. I only need lower half of he matrix, I don’t want to plot anything that has been excluded. The subsequent 6×6 matrixes shouldn’t overlap, as in the example below:

d = as.matrix(read.table("http://dl.dropbox.com/u/2505196/matrix_posthoc_tukey.dat"))
d[upper.tri(d)] <- NA
d1 <- d[1:6, 1:6]
d2 <- d[1:6, 7:12]
d3 <- d[1:6, 13:18]
d4 <- d[1:6, 18:24]
#...etc, up to d28 <- d[37:42,37:42] 

Code I used to create a single heatmap looks like this:

#baseline to create a separated space for all 28 plots
par(mfrow=c(4,7), mar=c(2,2,4,1), oma=c(2,4,2,2))

#using `image` to create heatmap, with color breaks defined by specific values
#the code below create just single heatmap
image(x=1:6, y=1:6, axes = FALSE, ylab="", xlab="", d1, 
  breaks=c(min(d1,na.rm=TRUE), -5.45, -4.65, 4.65, 5.45, max(d1,na.rm=TRUE)),
  col=c("red","orange","white","orange","red"))
axis(2, 1:6, cex.axis = 0.7, las=1, tick=F)
axis(3, 1:6, cex.axis = 0.7, tick=F)
#create vertical and forizontal lines
abline(h=seq(0.5,6.5,1), v=seq(0.5,6.5,1))
#plot values from the specific matrix subset
for (i in 1:6)
   {
     for (j in 1:6)
       {
         txt <- sprintf("%0.1f", d1[i,j])
         text(i, j, txt, cex=0.7)
        }
   }

Three such heatmaps look like this:

enter image description here

That’s where I’m stuck. I have to manually change d value every time I add another image to my single-page, multiple heatmap collection. I don’t know how to create a nice loop to plot those specific subsets of matrix at the same time using the code above.

Alternative solutions with ggplot2, lattice are also welcomed, although I believe the main question here is a good loop to make this series of heatmaps.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:50:50+00:00Added an answer on June 13, 2026 at 12:50 pm

    This is quite a complex plot, but it can be readily produced by the standard graphics library in R. It is more or less only a matter of keeping track of what indices goes into which panel. The way you extract the d1 to d28 matrices can be automated so you don’t have to write out each and every line.

    # Get the submatrices
    I <- unlist(lapply(0:6, function(a) a:6))
    J <- rep(0:6, 7:1)
    d2 <- mapply(function(i,j) d[1:6+6*i, 1:6+6*j], I, J, SIMPLIFY=FALSE)
    
    # Setup the layout and add an outer margin for the title and axis labels
    layout(matrix(c(1:28, 0, 0), 5, 6))
    par(oma=c(3,3,3,1), mar=c(2,2,1,1))
    
    # Plot all the matrices oriented the same way they appear in text
    # i.e. the first (vertical) dimension is plotted along the Y-axis
    for(k in 1:length(d2)){
        x <- 1:6+6*J[k]
        y <- 1:6+6*I[k]
    
        # Heatmap & grid
        image(x, y, t(d2[[k]][nrow(d2[[k]]):1,]), las=1, axes=FALSE,
              breaks=c(-1e10, -5.45, -4.65, 4.65, 5.45, 1e10),
              col=c("red","orange","white","orange","red"))
        xg <- apply(!is.na(d2[[k]]), 2, sum)
        yg <- rev(apply(!is.na(d2[[k]]), 1, sum))
        segments(c(x[1]-1, x)+.5, min(y)-.5,
                 c(x[1]-1, x)+.5, min(y)+c(6, yg)-.5, xpd=TRUE)
        segments(min(x)-.5,         c(y[1]-1, y)+.5,
                 min(x)+c(6,xg)-.5, c(y[1]-1, y)+.5, xpd=TRUE)
    
        # X & Y-axis values
        mtext(x, 1, .1, at=x, cex=.5)
        mtext(rev(y), 2, .2, at=y, las=1, cex=.5)
    
        # Values of each cell
        text(rep(x, each=6), rep(rev(y), 6),
         sub("NA", "", sprintf("%.2f", d2[[k]])), cex=.3)
    }
    
    # Add title and axis labels
    title("All 28 submatrices", outer=TRUE)
    mtext("Columns", outer=TRUE, 1, 1)
    mtext("Rows", outer=TRUE, 2, 1)
    

    The numbers in each cell may be tiny, but if you plot it to a pdf and zoom in they can be read. The xpd parameter of the segments functions supressess R from clipping the lines to the plot area (otherwise the outer lines would appear slightly thinner).

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application in which users interact with each-other. I want to visualize
Possible Duplicate: How do I visualize a matrix with colors and values displayed? I
I want to visualize some of my statistical caluclations in Ruby. My problem is,
I have vales with very small difference like... 0.000001. I want to visualize them
I have a large tree. I want to be able to visualize it using
I want to visualize components and connections of a HVAC system with .NET/C#. The
I am using BlazeDS and I want to visualize it in a presentation. Therefore
I am doing some kind of composition of automata. So I want to visualize/
I want to visualize some data about cars in a table via Prefuse Java.
I want to visualize the relationships between all of my JPA annotated classes. Given

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.