我使用晶格和ggplots包在R.中创建散点图时使用晶格包时,我可以使用以下代码控制整个图形大小(包括绘图区域和 Headers /脚注):

xyplot(
  test2 ~ test1, 
  data     = in.data, 
  groups   = SEX,
  #title
  main     = textGrob(c("This is the title", "\nThis is the sub-title"), 
                  x = unit(c(left.margin,left.margin), "inches"),
                  just  = "left", 
                  gp    = gpar(fontsize = c(title.font, title.font), lineheight=1.5)),
  #footnote
  sub = textGrob(paste(footnote1,Sys.time(),sep="\n"), 
             x = unit(c(left.margin), "inches"), 
             y = unit(c(bottom.margin), "inches"),
             just = c("left", "bottom"),
             gp = gpar(fontsize = footer.font, lineheight=0.9))
# Figure margins
lattice.options = list(layout.widths = leftright.margin, 
layout.heights = topbottome.margin)
)

您可以看到lattice.options可以控制整个图形大小,包括 Headers 和脚注 . 而使用ggplot2,我使用qplot(...)创建一个绘图然后控制绘图边距:

theme(plot.margin=unit(x=c(top.margin,right.margin,bottom.margin,left.margin),
units="inches"))

然后使用arrangeGrob添加 Headers 和脚注 .

g <- arrangeGrob(myplot, main=textGrob(c(title1, title2), 
                                   x = unit(c(left.margin,left.margin), "inches"),
                                   y = unit(c(-top.margin,-top.margin), "inches"),
                                   just  = "left", 
                                   gp    = gpar(fontsize = c(title.font, title.font), 
                                                fontface=c("plain","plain"),
                                                lineheight = 1.5)),
             sub = textGrob(paste(footnote1,Sys.time(),sep="\n"), 
                            x = unit(c(left.margin), "inches"), 
                            y = unit(c(bottom.margin), "inches"),
                            just = c("left", "bottom"),
                            gp = gpar(fontsize = footer.font,lineheight=0.9)))

g

这样,plot.margin只控制绘图区域,而不是控制 Headers 和脚注 . 那么我该如何控制ggplot2中的绘图区域和 Headers 区域(就像格子包一样)?如果答案不是,在这种情况下,我会更喜欢格子pakcage .