并排图形和knitr中的局部图像

时间:2015-12-22 22:18:27

标签: r latex png knitr

我正在尝试在knitr LaTeX文档中并排包含图形和本地PNG,但PNG最终会出现在图表的顶部。拿这个。例子:

\documentclass{article}
\begin{document}

<<makeplot, echo=FALSE, message=FALSE, include=FALSE>>=
opts_chunk$set(echo=FALSE, warning = FALSE, message = FALSE, cache = FALSE, error = FALSE)

library("ggplot2")
library("png")
library("grid")
data = data.frame(x = c(1:5), y = c(1:5))
myplot = ggplot(data = data) + 
  geom_point(aes(x = x, y = y))

@

<<these_plots, echo = FALSE, fig.height = 3, fig.width = 8, results = "asis", fig.show = "hold">>==
par(mar=c(4,4,.1,.1),cex.lab=.95,cex.axis=.9,mgp=c(2,.7,0),tcl=-.3)
myplot
grid.raster(readPNG(system.file("img", "Rlogo.png", package="png")), height = .3, width = .3)
@

产生这个:

enter image description here

我怎样才能让它们并排?我还尝试了here中的multiplot()函数,但PNG不会从图表上方的行中移开,即使将cols指定为2。

multiplot(myplot,
grid.raster(readPNG(system.file("img", "Rlogo.png", package="png")), height = .3, width = .3), cols = 2)

1 个答案:

答案 0 :(得分:0)

假设你已经保存了你的.png图像(并称之为“Rimage”),这样的东西应该排在左边的.png和右边的图(“myplot”),每个占用大约一半页面的宽度。

\begin{figure}[ht]      
\begin{minipage}[c]{.49\linewidth}                 
\includegraphics[width = 1\linewidth]{Rimage}   
\caption{R image}
\label{Rimage}
\end{minipage}
% \quad                                       
\begin{minipage}[c]{.49\linewidth}
\includegraphics[width = 1\linewidth]{myplot}
\caption{myplot}          
\label{myplot}
\end{minipage}
\end{figure}