[Tex/LaTex] Can’t get images to work with .Rnw files on RStudio

graphicsknitrrnwrstudio

I've been trying to debug a .Rnw file I wrote in RStudio for about a week now. The most frustrating part is that it used to work perfectly in its current form but then seemed to inexplicably stop working.

I've tracked down the source of the error to graphics I'm trying to add to the pdf file. It seems that only certain image files break the script. Every graphic that is added to the pdf is created using another R script. Let me know if you need to see that for any reason and I'll make an edit.

Here's the basic format of my .Rnw file:

\documentclass[
  title
]{article}
\usepackage{graphicx}

\begin{document}

<<Setup, include=FALSE, cache=FALSE>>=
##Sets up variables in paragraphs and creates data frames.
@

\title{Report Title}
\maketitle

\setcounter{secnumdepth}{-1}
\section{Report Summary}
Section Paragraph
\vspace{1cm}

\begin{figure}[h!]
  \includegraphics{Fig1.png}
\end{figure}

\end{document}

When I compile the PDF in RStudio, I get an error Running pdflatex.exe on REPORT.tex...failed

When I look at the log file, I see this:

!pdfTeX error: pdflatex.exe (file C:/Users/myname/Documents/Report/Fig1.png): libpng: 
internal error ==> Fatal error occurred, no output PDF file produced!

Anyone have any ideas what has gone wrong?

I've tried changing the images from .png files to .jpg and .tiff with the same results. I've tried adding a \graphicspath to the preamble and tried adding the full path to the \includegraphics section.

EDIT: Here's how I generated the fig1.png image. This should allow you to recreate an example in RStudio. I had to manually copy this code so typos could exist. Also, it will, of course, look like crap because I just recreated the image format with the example mtcars dataset.

png("C:/Users/UserName/Documents/Report/Fig1.png", height = 7, width = 14, units = 'in', res = 350)
p<-ggplot(mtcars, aes(color = mpg, x = cyl, y disp, group = mpg)) + 
  geom_line() + geom_point(shape = 16, size = 2) + coord_cartesian(ylim = c(0,550)) +
  labs(x = "X Axis", y = "Y Axis") +
  ggtitle(expression(atop("Title", atop(italic("Subtext"), "")))) + 
  stat_summary(fun.y=sum, geom="line") +
  theme(plot.title = element_text(face = "bold", size = 16, vjust = 1.5)) + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) + 
  theme(axis.title.y = element_text(vjust = 1))
p
dev.off()

Best Answer

You output fig1.png, while your try to include Fig1.png. TeX is case sensitive.

The following works:

\documentclass[
  titlepage
]{article}
\usepackage{graphicx}

\begin{document}

<<Setup, include=FALSE, cache=FALSE>>=
library(ggplot2)
png("Fig1.png", height = 7, width = 14, units = 'in', res = 350)
p<-ggplot(mtcars, aes(color = mpg, x = cyl, y = disp, group = mpg)) + 
  geom_line() + geom_point(shape = 16, size = 2) + coord_cartesian(ylim = c(0,550)) +
  labs(x = "X Axis", y = "Y Axis") +
  ggtitle(expression(atop("Title", atop(italic("Subtext"), "")))) + 
  stat_summary(fun.y=sum, geom="line") +
  theme(plot.title = element_text(face = "bold", size = 16, vjust = 1.5)) + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) + 
  theme(axis.title.y = element_text(vjust = 1))
p
dev.off()
@

\title{Report Title}
\maketitle

\setcounter{secnumdepth}{-1}
\section{Report Summary}
Section Paragraph
\vspace{1cm}

\begin{figure}[h!]
  \includegraphics{Fig1.png}
\end{figure}

\end{document}

By the way, it is better to use vector graphics and generate the figure automatically. The following is, in my opinion, a better style:

\documentclass[
  titlepage
]{article}
\usepackage{graphicx}

\begin{document}

<<Setup, include=FALSE, cache=FALSE>>=
library(ggplot2)
@

\title{Report Title}
\maketitle

\setcounter{secnumdepth}{-1}
\section{Report Summary}
Section Paragraph
\vspace{1cm}

\begin{figure}[h!]
<<Fig1, echo=FALSE>>=
p<-ggplot(mtcars, aes(color = mpg, x = cyl, y = disp, group = mpg)) + 
  geom_line() + geom_point(shape = 16, size = 2) + 
  coord_cartesian(ylim = c(0,550)) +
  labs(x = "X Axis", y = "Y Axis") +
  ggtitle(expression(atop("Title", atop(italic("Subtext"), "")))) + 
  stat_summary(fun.y=sum, geom="line") +
  theme(plot.title = element_text(face = "bold", size = 16, vjust = 1.5)) + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) + 
  theme(axis.title.y = element_text(vjust = 1))
p
@ 
\end{figure}

\end{document}