【问题标题】:Side by side graph and local image in knitrknitr中的并排图和局部图像
【发布时间】:2016-03-29 06:45:02
【问题描述】:

我试图在 knitr LaTeX 文档中并排包含一个图形和一个本地 PNG,但 PNG 最终位于图形的顶部。以这个 .Rnw 为例:

\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)
@

生成这个:

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

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

【问题讨论】:

  • 我认为 mfrow 参数应该为您实现这一点。在此处查看可能的重复项:stackoverflow.com/questions/1249548/…
  • 我没有问题将两个图并排放置......当其中一个是本地 PNG 时,行为不同。这些是不同的问题——所以希望这不会因为重复而被关闭!不过谢谢!
  • mfrow 参数和绘制本地 PNG 的运气好吗?
  • @Badger 不,par(mfrow = c(1, 2)) 没有做任何事情。我认为这个问题与 grid.raster 有关,但我不确定。
  • 有趣。在 grid.raster 之前尝试grid.newpage()。看起来它可能会调用一个新的情节,因为grid.raster 正在转储到开放的“x11”套件中。

标签: r latex png knitr


【解决方案1】:

假设您已经保存了 .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}

【讨论】:

    猜你喜欢
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 2021-05-15
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    相关资源
    最近更新 更多