设置:
- 我有一个 .Rwn 文件
- 我使用 beamer 文档类
- 我使用 knitr 编织 .Rnw 文件
问题:我想调整以下代码生成的图的大小以适合整个页面(最好不是完全适合,但至少留出约 0.5 厘米的边距)。这是 MWE
\documentclass{beamer}
% Knitr setup
<<setup, include=FALSE>>=
library(knitr)
opts_chunk$set(fig.align="center", fig.height=3.7, out.width="\\textwidth")
opts_knit$set(out.format = "latex")
@
\begin{document}
\begin{frame}[fragile]
<<include=FALSE>>=
if(!require(ggplot2)) install.packages("ggplot2")
require(ggplot2)
dat <- data.frame("x" = rnorm(n = 100),
"y" = runif(n = 100, min = -1, max = 1))
@
<<echo = FALSE>>=
ggplot(dat, aes(x = x, y = y)) +
geom_point() +
labs(title = "<A plot>",
subtitle = "<A subtitle>",
caption = "Source: <A source>")
@
\end{frame}
\end{document}
得出的结果为:
问题:如何使输出图适合整个页面?
答案1
不需要设置
out.width
:默认设置为最大允许宽度。您需要正确设置
fig.height
。fig.width
日志显示,在 beamer 中- \文本宽度=307.28987pt
- \textheight=244.6939pt
因此,让我们以英寸为单位设置knitr
相应的输出尺寸
\documentclass{beamer}
% Knitr setup
<<setup, include=FALSE>>=
library(knitr)
opts_chunk$set(fig.align="center", fig.height=3.3, fig.width=4.2)
opts_knit$set(out.format = "latex")
@
\begin{document}
\begin{frame}[fragile]
<<include=FALSE>>=
if(!require(ggplot2)) install.packages("ggplot2")
require(ggplot2)
dat <- data.frame("x" = rnorm(n = 100),
"y" = runif(n = 100, min = -1, max = 1))
@
<<echo = FALSE>>=
ggplot(dat, aes(x = x, y = y)) +
geom_point() +
labs(title = "<A plot>",
subtitle = "<A subtitle>",
caption = "Source: <A source>")
@
\end{frame}
\end{document}
得到的图形占据了幻灯片的边缘: