add table corresponding to y axis outside the plot

Viewed 554

I've a huge plot, I want to add a table on the right side corresponding to the y axis.

I tried grid arrange, gtable, tableGrob but none worked.(the table does not juxtapose with the y-axis lable) (original data has 35 rows, it sometimes exceeds the plot area).

plt <- ggplot(aaaa, aes(col, seq, label = ltr, fill = fill_calc2)) +  
         geom_text(size = 2.5, family = "mono", 
                   colour=factor(aaaa$colr), fontface ="bold") + 
         theme_minimal() + 
         labs(x=NULL, y=NULL) + 
         theme(panel.grid.major=element_blank(),
               panel.grid.minor=element_blank(), 
               axis.text.y = element_text(size = 8, family = "mono",color ="black",hjust = 0),
               axis.text.x  = element_text(face= "bold", size = 5,color ="black"), 
               plot.margin = (unit(c(.5, .5, 2, .5), "cm")), 
               plot.background = element_rect(fill = "white"), 
               axis.ticks.x  = element_line(color = "black")) 
grid.arrange(plt, table, ncol = 2)

The plot and table should align (heights and labels must match). plot should be of a greater width than table.

Thank you!

1 Answers

Make the ggplot you want

plt <- ggplot(dat, aes(col, seq, label = ltr, fill = fill_calc2)) +  
  geom_text(size = 2.5, family = "mono", 
            colour=factor(dat$colr), fontface ="bold") + 
  theme_minimal() + 
  labs(x=NULL, y=NULL) + 
  theme(panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(), 
        axis.text.y = element_text(size = 8, family = "mono",color ="black",hjust = 0),
        axis.text.x  = element_text(face= "bold", size = 5,color ="black"), 
        plot.margin = (unit(c(.5, .5, 2, .5), "cm")), 
        plot.background = element_rect(fill = "white"), 
        axis.ticks.x  = element_line(color = "black")) 

Adjust the table theme

core : text in the table colhead : text iin the colname rowhead : text infront of the row

adjustTheme <- ttheme_default(
  core = list(fg_params=list(cex = 0.8)),
  colhead = list(fg_params=list(cex = 0.7)),
  rowhead = list(fg_params=list(cex = 0.7)))

plt2 <- ggplot()+
  theme_minimal() + annotation_custom(tableGrob(table,theme= adjustTheme))

Compose the grid

grid.arrange(arrangeGrob(plt, plt2, ncol = 2,widths = c(4/5,1/5)))

enter image description here

Added


adjustTheme <- ttheme_default(
  core = list(fg_params=list(cex = 0.8)),
  colhead = list(fg_params=list(cex = 0.7)),
  rowhead = list(fg_params=list(cex = 0.7)))

tableObject = tableGrob(table,theme= adjustTheme)
tableObject$widths[-1] <- rep(unit(1/2,"null"), 3)
tableObject$heights <- rep(unit(1/nrow(tableObject),"null"), nrow(tableObject))

grid.arrange(arrangeGrob(plt, tableObject, ncol = 2,widths = c(4/5,1/5)))

enter image description here

Related