0

I am trying to create a plot in R and use knitr and latex to print that plot to a pdf. I think I am taking the correct steps: 1. save plot as .png in same folder as .tex file 2. write this into the latex file as includegraphics 3. run the knitr

However, the program is not recognizing/finding my recently created plot .png even though I checked that it is there in explorer.

This is my code in R to create the .png that seems to successfully create the .png file in my working directory (which is also where the .tex file is):

png("hab_pie.png", height=400, width=400)
bp <- ggplot(aoi_HabTerr2, aes(x="", y=acres, fill=habitat)) + geom_bar(width=1, stat="identity")
bp + coord_polar("y", start=0)
dev.off()

This is the relevant code that is in my .Rnw document:

\documentclass{article}
\usepackage{graphicx}
\usepackage{geometry}
\usepackage{fancyhdr}     %for headers,footers
\usepackage{underscore}  %needed if any text has underscores
\usepackage[utf8]{inputenc} 
\usepackage[singlelinecheck=false]{caption}

\geometry{letterpaper, top=0.45in, bottom=0.75in, left=0.75in, right=0.75in}

\begin{document}

\includegraphics[width=\linewidth]{hab_pie}

\end{document}

I am getting this error:

LaTeX Error: File `hab_pie' not found.

And when I look in the folder, the file is present under the same name: enter image description here

mmoore
  • 103
  • Can you provide a wider MWE, with preamble ? – sztruks Sep 11 '17 at 21:02
  • 2
    No, no, no! knitr does not work in that way at all. Try these steps: (1) Make a working LaTex "Hello" document (2) Save it with .Rnw extension (2) insert after "Hello" some working R code (e.g. 2+3) delimited between lines with <<>>= and @ (4) Open it with Rstudio and push the "Compile" button. Finish. You should have already a PDF with the R ouput, in this case, with "Hello" and the result of the sum (5). The same if the R code is a plot. BTW, better use PDF formats (not PNG) for the R graphs in a LaTeX document. – Fran Sep 11 '17 at 23:24
  • @Fran - my R script is long and complicated with many variables/actions. It is my understanding that I should not be writing all of this into the LaTex document. But, instead, that if I have my R script call my LaTex document with knitr, then this handles it better. Eventually (it works right now, but not with all steps yet) it will be a fully functional script tool in ArcGIS which is meant to pull in layers from ArcMap, do a bunch of stuff, and output a PDF report. So I also need the code to be in R language so that ArcMap can handle it. – mmoore Sep 12 '17 at 12:34
  • @sztruks I added the preamble and a bit more of the code. I'm not sure that anything else is relevant. I have other bits of code that incorporate variables from my R script and a table from my R script, as well as a chunk that pulls in an already saved .png file as a figure. All of these actions are working fine. – mmoore Sep 12 '17 at 12:38
  • 1
    Can you try to give the image a name without special characters like _? Which tex engine do you use to compile? pdflatex or something else? – samcarter_is_at_topanswers.xyz Sep 12 '17 at 12:50
  • @samcarter - thank you! it was a the special character that was causing the issue. – mmoore Sep 12 '17 at 13:05
  • @mmoore Why not? The main purposes of knitr are (1) literate programing: the same file is able to R programming and typesetting the results and discuss them. It can manage a entire book about statistics without problem (2) reproducible research: with the source file, anyone can trace how everything was exactly done, and more important (3) save your time: free you from include/update manually the results and make mistakes doing this. ;) – Fran Sep 12 '17 at 17:44

0 Answers0