1

I'm trying to include a graph and a local PNG side by side in a knitr LaTeX document, but the PNG ends up on top of the graph. Take this .Rnw example:

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

Which generates this:

enter image description here

How can I make them side by side? I've also tried the multiplot() function from here but the PNG wouldn't budge from the row above the graph, even when cols was specified as 2.

multiplot(myplot,
grid.raster(readPNG(system.file("img", "Rlogo.png", package="png")), height = .3, width = .3), cols = 2)
Nancy
  • 3,989
  • 5
  • 31
  • 49
  • I would think the mfrow parameter should achieve this for you. See potential duplicate here: http://stackoverflow.com/questions/1249548/side-by-side-plots-with-ggplot2 – Badger Dec 22 '15 at 22:32
  • I have no issue putting two plots side by side... it's when one of them is a local PNG that the behavior is different. These are different questions-- so hopefully this doesn't get closed as a duplicate! Thanks though! – Nancy Dec 22 '15 at 22:34
  • Any luck with the `mfrow` parameter and plotting the local PNG? – Badger Dec 22 '15 at 22:36
  • @Badger Nope, ```par(mfrow = c(1, 2))``` didn't do anything. I think the issue is something to do with grid.raster, but I'm not sure. – Nancy Dec 22 '15 at 22:39
  • Interesting. Try `grid.newpage()` prior to grid.raster. Looks like it may invoke a new plot as `grid.raster` is dumping into the open "x11" suite. – Badger Dec 22 '15 at 22:42
  • That does keep them from overlapping, but they're still not side by side. Progress though! – Nancy Dec 22 '15 at 22:46
  • Check out plotting an empty plot, that may achieve your result. I just left my machine otherwise I would link it! – Badger Dec 22 '15 at 22:48

1 Answers1

0

Assuming you have saved your .png image (and called it "Rimage"), something like this should array the .png on the left and the plot ("myplot") on the right of it, each taking up about half the width of your page.

\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}
lawyeR
  • 7,488
  • 5
  • 33
  • 63