Center the chart to the middle of the page with Knitr

I would like to align the plot with the center of the page of the created knitr pdf document. I can horizontally align the graph with the center using fig.align='center' , but I cannot figure out how to get the graph vertically aligned in the center.

I used the following code:

 --- header-includes: \usepackage{graphicx} output: pdf_document geometry: left=1in,right=1in,top=1in,bottom=1in --- ```{r,fig.align='center',out.extra='angle=90', echo=FALSE} library(ggplot2) ggplot(diamonds, aes(y=carat, x=price, colour=clarity))+geom_point()+ facet_wrap(~cut) ``` 
+5
source share
1 answer

On the LaTeX side, a figure with a vertical orientation should be a figure with position p . How this can be achieved using knitr depends on:

  • If a drawing has a signature, it is placed in the figure environment (see fig.env ). Then only the extra option fig.pos = 'p' is required.
  • If there is no title in the picture (which is usually bad), you can manually add the figure environment:

     \begin{figure}[p] ```{r,fig.align='center',out.extra='angle=90', echo=FALSE} library(ggplot2) ggplot(diamonds, aes(y=carat, x=price, colour=clarity))+geom_point()+ facet_wrap(~cut) ``` \end{figure} 

Note that this works when compiling to PDF, but limits you to PDF as output.

+5
source

Source: https://habr.com/ru/post/1236833/


All Articles