Good morning,
I have decided to try RMarkdown to create short white papers that I will be updating regularly. The rmd code extracts Fed data, organizes it, and then creates graphs that are placed alongside some short commentary about them.
My question is about the output. The figures created within Rstudio are crisp and what I wanted; the pdf output should the lines as thicker and much less crisp.
My code chunk is below. I've tried changing the dpi at the top of the chunk, but that has not changed the pdf output.
Any ideas about getting the same crisp lines within RStudio onto pdf using Rmarkdown?
Thanks!
```{r echo=FALSE,dpi=600,message=FALSE}
# Create caption
mycaption<- "Source: FredII - Federal Reserve Bank of St. Louis"
# Wrap caption 120 characters:
mycaption <- paste0(strwrap(mycaption, 120), sep="", collapse="\n")
# Create Plot
ggplot(data=dt2,aes(x=date,y=value,color=name,linetype=name))+
geom_line(size=0.7)+
labs(x="",y="Interest Rates",
title="Comparing Interest Rates by Source/Maturity",
caption=mycaption ) +
guides(title="New Legend Title")
```
An easy solution to have higher quality plotting output when compiling to PDF is to use the tikz graphics device by setting the chunkk option dev="tikz" (or globally using opts_chunk$set(dev = "tikz")).
Tikz is a TeX package that allows to create vector graphics using an easy syntax. Using vector graphics has the advantage that they are scalable, annotations in your plot will use the same main font that is used in the document and you can easily add math symbols to your plot using the TeX syntax:
plot(..., main = "$\\bar{\\mu}$")
I'm working on APA tables with the package papaja and r markdown together with Latex and I want to adjust the vertical space between rows in a relatively big table so it will fit on one page. Additionally, if needed, i also want to reduce font size and column width. Is this possible with papaja's apa_table() function? Just using the small argument to TRUE did not do the trick.
As mentioned in the papaja manual you can adjust line spacing in tables by adding some lines to the document preamble. The following will result in single-spaced tables with double-spaced caption and table note:
header-includes:
- \usepackage{setspace}
- \AtBeginEnvironment{tabular}{\singlespacing}
- \AtBeginEnvironment{lltable}{\singlespacing}
- \AtBeginEnvironment{tablenotes}{\doublespacing}
- \captionsetup[table]{font={stretch=1.5}}
- \captionsetup[figure]{font={stretch=1.5}}
The column width is currently not controllable with apa_table() but you can use landscape = TRUE if the table doesn't fit in portrait mode. Font size can currently only be adjusted with small = TRUE.
I've used knitr to produce a PDF. This is a screenshot from 1 page of the PDF:
After a couple of hours of fiddling various knitr options (fig.width, fig.height, out.height, out.width), the page of the PDF looks half decent. But...the axis text, the annotations and the points in the plot are still too small relative to the text in the PDF.
Is there is a way to have knitr automatically size the text/points in the plot so fit well inside the PDF?
EDIT: with the suggested fig.width="3.7" and out.width = "\\columnwidth"
I am using knitr to write my thesis and compiling it as a latex document. I have successfully included figures with the appropriately labeled caption; however, in addition to the caption I would like to include additional lines of text directly below the caption and associated with the figure. For example, below the caption I would like to hove: "NOTE: These data are distributed with mu=0 and sd=1" and "SOURCE: These data were randomly generated in R."
EDIT 1 to question: I also need to have a list of figures page with just the caption information included not any of the NOTE or SOURCE information. In addition the caption under the figure must be centered, while the NOTE and SOURCE are left hand justified.
I believe that many publications have figures with notes about the figure or information about the source of the data that is separate from the caption title; so, I believe this question is not just applicable to me.
EDIT 1 to code. Here is a synthesis of the code that #user29020 provided and the link they suggested, as well as the .pdf work around I have assembled from other posts. The advantage of the "pure" knitr method provided by #user29020 is that the plot is beautiful with the text scaled consistently with the body of the text. The disadvantage is a lack of control of the caption information: I would like to center the "Caption" element and left justify the and sub-caption lines and I do not know how to accomplish this; so, if anybody knows how to do this that would be great. The advantage of the pdf work around is that I have greater control over the captions. In addition to not taking full advantage of knitr's ability to create and track figures, the disadvantage is that while the font matches, by using "Ghostscript" the scaling appears a little off and needs to be adjusted in an ad-hoc fashion in the R code.
\documentclass{article}
\usepackage{caption}
\usepackage{tikz}%Draw Graphs
\begin{document}
\listoffigures
\newpage
%THIS IS A SYNTHESIS OF user29020 CODE AND THE LINK THEY PROVIDED IN THEIR COMMENTS
<<cap-setup1,include=FALSE>>=
scap="This is a Caption."
NOTE="NOTE: Here is a note with one linebreak from above."
SOURCE="SOURCE: Here is my source with an extra linebreak to separate it."
EXTRA="{\\tiny EXTRA: Here is smaller text. You could explore more text sizing in \\LaTeX.} And bigger again."
hspace="\\\\\\hspace{\\textwidth}"
cap<- paste(
scap,
hspace,
NOTE,
hspace,
SOURCE,
hspace,
EXTRA)
#
In Figure \ref {fig:fig1}, we see a scatter plot.
<<fig1, echo=FALSE,fig.cap=cap, fig.scap=scap,fig.pos="!h", >>=
r<-rnorm(100)
plot(r)
#
%THIS IS THE PDF WORK AROUND
%Loading R Packages
<<loading,include=FALSE>>=
require(extrafont)
require(graphicx)
font_install("fontcm") #this gets the cm roman family from package extrafont for pdf output in r to match latex
#
<<fig-pdf,echo=FALSE, include=FALSE,fig.cap=paste("This is a Caption"),fig.pos="!h", >>=
Sys.setenv(R_GSCMD="C:/Program Files/gs/gs9.15/bin/gswin64c.exe") #this needs to go inside every r code chunk to call GhostScript for embedding latex font "computer modern roman" (so R font in pdfs matches latex document font)
pdf("tikz-plot-pdf.pdf", family="CM Roman")
set.seed(2015)
r<-rnorm(100)
plot(r)
dev.off()
embed_fonts("tikz-plot-pdf.pdf") #this embeds the pdf font so that when graphicx calls the pdf the fonts from R to latex match
#
\newpage
In Figure \ref{fig:scatter}, we see a scatter plot.
\begin{figure}[!h]
\centering
\includegraphics[width=\textwidth]{tikz-plot-pdf}
\caption{This is also a Caption}\label{fig:scatter}
\begin{minipage}{\textwidth}
NOTE: Here is a note with one linebreak from above.\\
SOURCE: Here is my source with an extra linebreak to separate it.\\
EXTRA:{\tiny EXTRA: Here is smaller text. You could explore more text sizing in \LaTeX.} And bigger again.
\end{minipage}
\end{figure}
\end{document}
I would like to have the "NOTE" and "SOURCE" lines below the caption line, EDIT 1: only the caption included in the \listoffigures and the caption centered, while the NOTE and SOURCE are left justified (like the output from the pdf workaround):
I have not asked a question on this forum before, although I have found many answers, so any meta-feedback on my question EDIT 1: or how I am editing is also appreciated.
Thank You
This answer assumes that multiline captions are sufficient for your purposes. I used your method of passing an R variable to the Knitr fig.cap= argument. This lets us compose the caption ahead of time in other code chunks before use.
The tricks were that A) we need to find out how to add line break to caption without using caption package, and B) know how to pass literal "\" (backslash) characters to the resulting Latex document.
The reference about adding captions suggests adding \\\hspace{\textwidth} to start a new line within the caption (in the Latex file). So, we escape it in the Knitr file like this \\\\\\hspace{\\textwidth}.
Indeed, almost any Latex code you want to end up in the Latex file will have to be escaped this way. When writing your thesis, you'll want to refer to your Figures, no doubt. You can use the Latex references by putting the figure \label into the caption and then using a \ref{} elsewhere.
See the modified version of your file below for multiline captions and figure references:
\documentclass[12pt]{article}%12 Pt font, article document class
\usepackage{caption}
\usepackage{tikz}%Draw Graphs
\begin{document}
<<>>=
x <- paste(
"This is a Caption.",
"\\\\\\hspace{\\textwidth}",
"NOTE: Here is a note with one linebreak from above.",
"\\\\\\hspace{\\textwidth}",
"\\\\\\hspace{\\textwidth}",
"SOURCE: Here is my source with an extra linebreak to separate it.",
"\\\\\\hspace{\\textwidth}",
"{\\tiny EXTRA: Here is smaller text. You could explore more text sizing in \\LaTeX.} And bigger again.",
"\\label{figure:demo-of-multiline-captions}",
"\n" )
cat(x)
#
We can also show the variable "x" in the "asis" formate here:
<<results="asis">>=
cat(x)
#
This text right here (now) is a new paragraph, so it is indented.
Below, in \textbf{Figure~\ref{figure:demo-of-multiline-captions}}, we use the "x" variable in the "fig.cap=" argument to Knitr.
<<fig1, echo=FALSE,fig.cap=x,fig.pos="!h", >>=
r<-rnorm(100)
plot(r)
#
\end{document}
Results give this:
I want to plot a qplot() + geom_smooth() using a knitr code chunk in an .Rmd document that I am compiling as an ioslides presentation, and I want less pixelation. I've played with ggsave() and inserting the resulting .png using ![image][id], but of course the image is massive. Can I change the image size within the RMarkdown image tag? Is there some more optimal solution? Thank you.
Set your dev to something scalable:
opts_chunk$set(
dev = c('png', 'postscript') # produce both png and ps versions
);