如何在knitr pdf输出中调整绘图的大小
我正在尝试使用knitr将一些ggplot输出发布到pdf文件 我使用的命令是:如何在knitr pdf输出中调整绘图的大小,r,pdf,ggplot2,knitr,R,Pdf,Ggplot2,Knitr,我正在尝试使用knitr将一些ggplot输出发布到pdf文件 我使用的命令是: ```{r, results = 'asis', echo=FALSE, warning=FALSE,tidy=FALSE} ggplot(data,aes(datetime, usedmem, group=machine, colour=machine))+geom_line()+ geom_smooth(method="lm", se=T, colour="blue")+
```{r, results = 'asis', echo=FALSE, warning=FALSE,tidy=FALSE}
ggplot(data,aes(datetime, usedmem, group=machine, colour=machine))+geom_line()+
geom_smooth(method="lm", se=T, colour="blue")+
facet_wrap(~machine)+theme_bw()
```
要回答第二个问题,请尝试使用找到的multiplot函数。正如user2706569在评论中所说,这需要使用knitr(只需将.rmw文件保存为.Rnw)并添加以下代码。然后,您可以在用于生成绘图的任何块中调用multiplot
<<loadfunctions>>=
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
require(grid)
plots <- c(list(...), plotlist)
numPlots = length(plots)
if (is.null(layout)) {
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
for (i in 1:numPlots) {
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, layout.pos.col = matchidx$col))
}
}
}
@
<<myplots, results='asis', fig.width=8, fig.height=6>>==
multiplot(someplot_1, someplot_2, cols = 2)
@
=
multiplot要回答第二个问题,请尝试使用找到的multiplot函数。正如user2706569在评论中所说,这需要使用knitr(只需将.rmw文件保存为.Rnw)并添加以下代码。然后,您可以在用于生成绘图的任何块中调用multiplot
<<loadfunctions>>=
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
require(grid)
plots <- c(list(...), plotlist)
numPlots = length(plots)
if (is.null(layout)) {
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
for (i in 1:numPlots) {
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, layout.pos.col = matchidx$col))
}
}
}
@
<<myplots, results='asis', fig.width=8, fig.height=6>>==
multiplot(someplot_1, someplot_2, cols = 2)
@
=
multiplot构建一个.Rnw文件(如下所示)将有所帮助。通过调整out.width
、fig.height
和fig.width
可以控制“浪费空间”问题
\documentclass{article}
\begin{document}
Create several graphics
<<>>=
library(ggplot2)
plots <- lapply(unique(mpg$manufacturer),
function(m) {
ggplot(mpg[mpg$manufacturer == m, ]) +
aes(x = model, y = cty) +
geom_boxplot() +
ggtitle(m)
})
@
<<echo = FALSE, results = "hide", out.width = "0.5\\textwidth">>=
plots
@
\end{document}
\documentclass{article}
\开始{document}
创建几个图形
=
图书馆(GG2)
plots构建一个.Rnw文件,如下所示将有所帮助。通过调整out.width
、fig.height
和fig.width
可以控制“浪费空间”问题
\documentclass{article}
\begin{document}
Create several graphics
<<>>=
library(ggplot2)
plots <- lapply(unique(mpg$manufacturer),
function(m) {
ggplot(mpg[mpg$manufacturer == m, ]) +
aes(x = model, y = cty) +
geom_boxplot() +
ggtitle(m)
})
@
<<echo = FALSE, results = "hide", out.width = "0.5\\textwidth">>=
plots
@
\end{document}
\documentclass{article}
\开始{document}
创建几个图形
=
图书馆(GG2)
绘图请先阅读并玩转fig.width
,fig.height
,out.width
,out.height
。关于第二个问题,您应该考虑编写代码> RNW ,而不是<代码> RMARKOPEN/<代码>,因为这简化了最终PDF的控制。如果这不是一个选项或没有帮助,请将您的示例扩展到生成“浪费空间的图表”的可复制内容。请先阅读并使用fig.width
,fig.height
,out.width
,out.height
。关于第二个问题,您应该考虑编写代码> RNW ,而不是<代码> RMARKOPEN/<代码>,因为这简化了最终PDF的控制。如果这不是一个选项或没有帮助,请将您的示例扩展到生成“浪费空间的图表”的可复制内容。