If a pdf document is created using the following Sweave (R/LaTeX) code, then in the top left of the figure there will be text pdf 2; note this is not embedded in the png but is actually text you can highlight.
In the .Rnw:
\begin{figure}[htb]
\centering
<<fig=FALSE,echo=FALSE>>=
png("test0.png",width=4,height=4,units='in',res=1200)
plot(1)
dev.off()
#
\includegraphics{test0.png}
\caption{Demonstration}
\end{figure}
Then in R:
Sweave("report.Rnw") ; texi2pdf("report.tex")
How do I fix this?
I am using a very recent version of R on Ubuntu.
With the following file
\documentclass{article}
\begin{document}
\begin{figure}[htb]
\centering
<<fig=FALSE,echo=FALSE>>=
png("test0.png",width=4,height=4,units='in',res=1200)
plot(1)
dev.off()
#
\includegraphics{test0.png}
\caption{Demonstration}
\end{figure}
there is a null device 1 message that appears before the plot
It is the output of the R commands that generate the plot.
You can suppress it by adding results=hide.
<<fig=FALSE,echo=FALSE,results=hide>>=
Your message is slightly different because dev.off() returns the name of the current device:
there was none in my case (it was a fresh session),
and a previously opened (but not closed) PDF file in your case.
Related
Consider the following code in Rmarkdown, which produces a graph as below:
\begin{figure}
\centering
\captionsetup{skip=0pt}
\includegraphics[width=6cm]{example-image-a}
\caption{A test caption}
\end{figure}
Output:
Now, instead of just using a graph, I would like to create my own plot in Rmarkdown, for example, using this code:
\begin{figure}
\centering
\captionsetup{skip=0pt}
\includegraphics[width=6cm]{plot(mtcars$mpg)}
\caption{A test caption}
\end{figure}
but the following error is produced instead:
! LaTeX Error: File `plot(mtcars$mpg)' not found.
Is there any way to run r codes within latex?
R Markdown doesn't work this way. If you are using RStudio, it is pretty easy. In the R Markdown file, type
plot(mtcars$mpg)
In other words, you need to start your code with three back-ticks and {r}, and stop your code with three back-ticks again. Your R code written this way will produce the plot you require. After that, click on "knit" button, or use rmarkdown::render(your_file_name).
What you have written in the question means you have produced this chart, and saved that chart as a file in your working directory. If you have saved your plot in the working directory, your code will still work. Try it.
you could try plotting to a file device in an R code chunk e.g.
png(filename="mtcars_mpg.png")
plot(mtcars$mpg)
dev.off()
You can hide this chunk by using include = FALSE in the chunk options.
Then just bring the file in with your LaTeX code:
\begin{figure}
\centering
\captionsetup{skip=0pt}
\includegraphics[width=6cm]{mtcars_mpg.png}
\caption{A test caption}
\end{figure}
I like to generate both pdf and png image files when I create a (latex) document using knitr. This can be done using dev = c("pdf", "png").
However, I don't seem to be able to choose (on a per-figure) basis which of the two is picked in my latex figure environment. Currently the only way in which I can get, say, a png input file for Fig. 1 and a pdf input file for Fig. 2 is to generate only the required format (by using dev = "png", fig.ext = "png").
Is there a way in which I can still generate both, but at the latex level can select which one is shown? It could be solved easily by allowing an extension in the \includegraphics command, I suppose.
Any input appreciated...
Ron
Minimal example:
\documentclass[a4paper,12pt]{article}
%\VignetteEngine{knitr::knitr}
\DeclareGraphicsExtensions{.pdf,.png}
\begin{document}
%\maketitle
<<knitrInitialization,echo=FALSE>>=
require("knitr", quietly=TRUE)
opts_chunk$set(comment=NA,background='transparent',size='small',fig.width=6,fig.height=6,out.width='\\textwidth',dev=c('pdf','png'))
#
%% this one generates two figures, and the pdf version is shown
%% because of the order in DeclareGraphicsExtensions
\begin{figure}[tb]
\centering
<<testPDF,echo=FALSE>>=
plot(1:10)
#
\caption{PDF figure}
\label{fig:pdf}
\end{figure}
%% if I want to show a png (e.g., because the pdf is too large) I can
%% only do that by not generating a pdf in the first place
\begin{figure}[tb]
\centering
<<testPNG,echo=FALSE,dev='png',fig.ext='png'>>=
plot(1:10)
#
\caption{PNG figure}
\label{fig:png}
\end{figure}
\end{document}
You can simply put
\DeclareGraphicsExtensions{.png,.pdf}
in your document when you want PNG to have precedence, and then
\DeclareGraphicsExtensions{.pdf,.png}
later if you want to go back to the PDF preference. This works in the body of a LaTeX document, not just in the header.
I want to apply the fig.pos= 'h' chunk option like so:
```{r echo=FALSE , fig.pos='h'}
ggplot()
```
so that when I knit to pdf latex positions my plots (the float) at the precise location in the LaTeX code. It is my understanding that this should make my latex code look like:
\begin{figure}[h]
\centering
\includegraphics{files/figure-latex/unnamed-chunk-3-1.pdf}
\end{figure}
but instead when I open the generated Tex document I see:
\begin{figure}[htbp]
\centering
\includegraphics{files/figure-latex/unnamed-chunk-3-1.pdf}
\end{figure}
I am ending up with my plots in random postions in the PDF document instead of where they are located in the latex code, sometimes taking up an entire page for one plot. I have tried fig.pos='h', fig.pos='H',fig.pos='h',fig.pos='h!'and fig.pos='H! based on the table on this page.
I am attempting this based on this link where it states under plot chunk options: fig.pos: (''; character) a character string for the figure position arrangement to be used in \begin{figure}[fig.pos]
What am I missing or how else can I ensure my plots are displayed where the text is located within the Rmd file or latex code?
I am making my first steps with knitr, trying to generate a raport. In the raport, I include R code which generates a ggplot2 object that I want to be included directly below some text. To make it more detailed, the graphic is a pair of two separated plots, which I want to be placed parallelly, one next to another.
So far, I have been dealing with by using the R code: producing and saving a .pdf picture, and then reading this picture from file and including it in the report by \includegraphics command. However, it is no more a solution for me - I want the plot to be generated simultaneously with the report by the R code (in particular: not to be saved anywhere as a .pdf)
However, the code I tried to use did not work properly - it generates the 2 plots, but they are however:
1) incorrectly placed - 2 pages below (which is even not the end of the document!)
2) I don't know how to place them in one row, with the defined size.
Please be of some help! Thank you in advance!! [below my not working porperly R code]
\textit{Pic 1 title} Some pic description
\begin{figure}[h]
\subfigure[pic1 name]{
<<echo = F, eval = T, message=F, fig=TRUE>>=
# a function returning a ggplot2 object (with a proper parameters instead of "...")
plot.matrix.from.file(...)
#
% below there is a fragment of the code I used before (which includes a graphics directly from a .pdf file)
%\includegraphics[scale=0.4]{data/simulated.data/obs_pred_mean_Gini_r.pdf}
\label{pic1 label}
}
\subfigure[pic2 name]{
<<echo = F, eval = T, message=F>>=
# a function returning a ggplot2 object (with a proper parameters instead of "...")
plot.matrix.from.file(...)
#
% below there is a fragment of the code I used before (which includes a graphics directly from a .pdf file)
%\includegraphics[scale=0.4]{data/simulated.data/obs_pred_var_Gini_r.pdf}
\label{pic2 label}
}
\caption{caption for the pair of plots}
\end{figure}
I do not see any problems using the subcaption package. See example 104.
\documentclass{article}
\usepackage{subcaption}
\begin{document}
You can include sub-figures using the \textbf{subcaption} package. For example,
Figure \ref{fig:test} contains \ref{fig:test-a} and \ref{fig:test-b}.
\begin{figure}
\begin{subfigure}{.5\textwidth}
<<test-a, echo=FALSE, results='asis', fig.width=5, fig.height=5>>=
plot(1:10)
#
\caption{This is Figure a. \label{fig:test-a}}
\end{subfigure}
\begin{subfigure}{.5\textwidth}
<<test-b, echo=FALSE, results='asis', fig.width=5, fig.height=5>>=
plot(rnorm(100))
#
\caption{This is Figure b. \label{fig:test-b}}
\end{subfigure}
\caption{This figure contains two subfigures. \label{fig:test}}
\end{figure}
\end{document}
Output as expected:
My converting the R script (.R) into an RMarkdown file (.rmd) in RStudio and then pressing “knit html” result in two output files (that is, .html and .md files). I am confronting 2 issues:
The html file shows that the title of the ggplot graph gets chopped. I have changed the original width of 11 to a new width of 15:
ggsave(file=outFile, width=15, height=7)
How would I resolve the issue ? And how would I convert the .md file into a PDF file ?
Your question is not exactly clear. I'm not sure, for example, why you are using ggsave() to begin with. You can directly create a "ggplot" image in your file to knit and set your figure width and height in your input file.
In a ".Rmd" file, your code might look something like:
```{r fig.width=7, fig.height=4, echo=FALSE}
library(ggplot2)
qplot(mpg, wt, data=mtcars)
```
The echo=FALSE will make it so that the code doesn't display, but the resulting plot will. The figure width and height have been set with the relevant arguments.
If you wanted to convert your resulting markdown file to PDF, I would recommend looking at Pandoc which will allow you to do something like the following to convert your file to a PDF:
pandoc infile.md -o outfile.pdf
Alternatively, you can use R Sweave instead of R Markdown in R/RStudio. For instance, if you create a new "Rnw" file in RStudio and paste the following in, you'll have the option to directly compile a PDF instead of compile an HTML.
\documentclass{article}
\begin{document}
<<fig.width=5, fig.height=3, echo=FALSE>>=
library(ggplot2)
qplot(mpg, wt, data=mtcars)
#
\end{document}