rgridextra

How to combine 3 tableGrob together without making a mess?


I have a matrix with rowhead (sequence) and colhead (sequence) that must be displayed. Here is the code for such matrix and the output :

sim matrix

  m <- tableGrob(sim_df[1:10, 1:10], theme=ttheme_default(
                  core=list(fg_params=list(col=fg_cols), bg_params = list(fill=bg_cols, col="black")),
                  colhead=list(bg_params=list(fill="white", col="black")), 
                  rowhead=list(fg_params=list(fontface="bold"), bg_params=list(fill="white", 
col="black"))))

sim matrix

Now, I want to add extra labels (name, year) in the headers. Here are the outputs :

  # rows' labels
  m1 <- tableGrob(df_r[1:10, 1:2], theme=ttheme_default(
                  core=list(fg_params=list(col="black"), bg_params = list(fill="white", col="black")),
                  colhead=list(fg_params=list(col="white"), bg_params=list(fill="white", col="white"))),
                  rows=NULL)

m1 - row labels

   # columns' labels
  m2 <- tableGrob(df_c[1:2, 1:10], theme=ttheme_default(
                  core=list(fg_params=list(col="black"), bg_params = list(fill="white", col="black")),
                  rowhead=list(fg_params=list(col="white"), bg_params=list(fill="white", col="white"))),
                  cols=NULL)

m2 - column labels

I have managed to combine the tableGrobs m1 + m and m2 + m. Here are the two outputs :

r_align <- gtable_combine(m1, m, along=1)

r_align - extra labels for rownames

c_align <- gtable_combine(m2, m, along=2)

c_align - extra labels for colnames

That being said, once I want to combine the extra labels on the rownames and the colnames, it simply wont align and it removes two columns in the process.

As mentionned earlier, by doing :

r_align <- gtable_combine(m1, m, along=1)

we get the matrix with extra labels for the rows. Once we try to combine this r_align with the m2 labels for the colnames extras :

final_align <- gtable_combine(m2, r_align, along=2)

we get the following product

The expected result is : expectations


Solution

  • Fixed the issue by using cbind() instead of gtable_combine() for the rows label alignment to the similarity matrix.