2

I've just found out about this tool knitr. Fist thing I want it use for is to produce simple raport appendix consisting of one page with content description and series of pages with plots, one plot per page, plots generated in a loop. However, I've encountered a problem with size of single plot - it doesn't fill whole available space on page. I've tried different settings of fig.width, fig.height , ang googled around a little, but nothing works so far. Here is how it looks like now:

enter image description here

The red rectangle is the approximation of desired size of a plot. Here the code:

\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}

Any suggestions will be much appreciated!

ltw
  • 265
  • 2
  • 17
  • 3
    In this case, you're simply underestimating the default margins in LaTeX. Add some text to your document and you will see that this figure is already as wide as the normal text width. The LaTeX package `geometry` is a handy way to change this. In general, take a look at the [option](http://yihui.name/knitr/options/) `out.width` and understand how it's different from `fig.width`. Besides, note that white margins around figures also add to the figure width. [`hook_pdfcrop`](http://stackoverflow.com/a/34729574/2706569) can help you to get rid of them. – CL. Feb 24 '16 at 12:54
  • Wow, my LaTeX skills are indeed very rusty... I added some text and you were right! – ltw Feb 24 '16 at 13:27
  • Add your solution as an answer, so people know this Q is resolved. – Axeman Feb 24 '16 at 14:11

1 Answers1

3

The LaTeX Package geometry did the trick - as @CL pointed out, the problem was that default LaTeX margins were bigger than I thought they were. It was sufficient to add this line:

\usepackage[a4paper, total={6in, 8in}]{geometry}
mrub
  • 501
  • 2
  • 12
  • 30
ltw
  • 265
  • 2
  • 17