Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

vertical alignment of gridExtra tableGrob (R grid graphics/grob)

Having some trouble aligning a grid graphics object -- have read all the docs I can find, including the Murrell book, but no success. I think what I'm trying to do is pretty straightforward, so hopefully I'm missing simple.

Here's a reproducible example that will make a PDF of all the air carriers by destination in Hadley's hflights package (mirrors what I am trying to do on a different data set).

require(hflights)
require(gridExtra)
require(Cairo)

make_table <- function(df) {
  p <- tableGrob(
    df
   ,padding.h=unit(.25, "mm")
   ,show.rownames=FALSE
   ,gpar.coretext = gpar(fontsize=8, lineheight=0)
   #this doesn't seem to justify the table
   ,just = c("bottom")
   ,show.box = T
  )
  return(p)
}

dests <- unique(hflights$Dest)

#list to hold the plots
plot_list <- list()

#loop over destinations and make a simple report
for (i in dests) {
  #just this destination
  this_dest <- hflights[hflights$Dest == i, ]

  #the title
  title <- textGrob(label = i, gp = gpar(fontsize=72, fontface = 'bold'))

  #a table of carriers
  carriers <- unique(this_dest$UniqueCarrier)
  carriers <- data.frame(
    carrier=carriers  
  )
  carrier_table <- make_table(carriers)

  #put them together
  p <- arrangeGrob(
    title, carrier_table
   ,nrow=2
  )

  plot_list[[i]] <- p
}


#print the report
Cairo(
  width = 11, height = 8.5 
 ,file = paste('destinations.pdf', sep = ''), type="pdf"
 ,units = "in"
)

  print(plot_list)

dev.off()

I want the entire table produced by tableGrob (in the make_table function) to justify to the top of the grob. Right now it is centered vertically and horizontally inside the grob. Do I need to do that in the call to tableGrob, or is it in the arrangeGrob call? To ask it another way, in case the above is not clear, how can I make the whole table (not the text inside of it) justify to the top/bottom/left/right of its container?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this,

enter image description here

library(gridExtra)
justify <- function(x, hjust="center", vjust="center", draw=TRUE){
  w <- sum(x$widths)
  h <- sum(x$heights)
  xj <- switch(hjust,
               center = 0.5,
               left = 0.5*w,
               right=unit(1,"npc") - 0.5*w)
  yj <- switch(vjust,
               center = 0.5,
               bottom = 0.5*h,
               top=unit(1,"npc") - 0.5*h)
  x$vp <- viewport(x=xj, y=yj)
  if(draw) grid.draw(x)
  return(x)
}

g <- tableGrob(iris[1:3,1:2])
grid.newpage()
justify(g,"right", "top")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...