knitr - 如何使用pdf填充整个A4页面空间?

时间:2016-02-24 12:02:23

标签: r plot ggplot2 knitr

我刚刚发现了这个工具编织者。我希望它使用的第一个东西是生成简单的raport附录,其中包含一页内容描述和一系列带有绘图的页面,每页一个绘图,循环中生成的绘图。 但是,我遇到了单个图块大小的问题 - 它没有填满页面上的整个可用空间。我已尝试过fig.width的不同设置,图高度,用谷歌搜索一下,但到目前为止没有任何作用。 现在看来是这样的:

enter image description here

红色矩形是图表所需大小的近似值。 代码如下:

\documentclass{article}

\begin{document}

<< echo =FALSE >>=
suppressMessages(suppressWarnings(library("ggplot2")))
suppressMessages(suppressWarnings(library("RColorBrewer")))
colours <- brewer.pal(11, "RdYlGn")[3:9]
@

<< echo=FALSE, fig.width = 8.3, fig.height = 11.7, fig.align = 'center'  >>=

name.percentage <- data.frame(name = paste0(LETTERS[1:30], letters[1:30], sample(LETTERS[1:30], size = 30, replace = TRUE )), 
                          percentage = 0.85 + runif(30, 0, 0.15))

name.percentage <- rbind( 
  transform(name.percentage, type = 1, fill = cut(percentage, breaks = c(-Inf,(1:6 * 3 + 81)/100, Inf), right = T, labels = colours)),
  transform(name.percentage, percentage = 1 - percentage, type = 2, fill = "#EEEEEE")
  )

  plot <- ggplot(data = name.percentage, 
                 aes( x = name, y = percentage, fill = fill)) +
    geom_bar(stat = "identity", position = "stack", width = 0.75) + 
    scale_fill_identity(guide = "none")  + 
    labs(x = NULL, y = NULL) + 
    scale_y_continuous(expand = c(0,0)) +
    scale_x_discrete(expand = c(0,0)) +
    coord_flip() +
    theme_classic() +
    theme(axis.ticks.y = element_blank(),
      axis.text.y = element_text(size = 11, colour = "black" ),
      axis.text.x = element_text(size = 11, colour = "black" ),
      axis.line = element_blank(),
      plot.margin = unit(c(0,5,0,0),"mm"),
      aspect.ratio = 1.45)

  print(plot)
@

\end{document}

任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:3)

LaTeX Package geometry成功了 - 正如@CL指出的那样,问题是默认的LaTeX边距比我想象的要大。添加这一行就足够了:

\usepackage[a4paper, total={6in, 8in}]{geometry}