Why can't i use fig.cap - Latex through Sweave + Knitr - r

I'm very new to Latex, and I'm trying my way on creating a graph with a figure caption.
Now when I try to add fig.cap in the chunk heading (second chunk) I get the error
Latex error: Not in outer par mode
My code
<<echo = FALSE>>=
source("analysis.R")
repoData <- readRDS("data/repoData.rds")
a4width<- 8.3
a4height<- 11.7
#
\begin{figure}[h]
<<echo = FALSE, fig.width= a4width, fig.height=0.35*a4height>>=
G2(repoData)
#
\end{figure}
## ---- G2 ----
G2 <- function(df) {
# For inflation graph
plot <- ggplot(df, aes(x = Month, y = Percent)) +
geom_line(colour = "firebrick") +
xlab("") +
ylab("Repo rate") +
theme_classic() +
theme(axis.title.y = element_text(vjust = 1))
return(plot)
}
Why does this happen and how can this be resolved?

You should omit the \begin{figure} (not shown in your MWE) and \end{figure} from your Sweave file; when you specify fig.cap they are generated automatically by knitr (and redundantly, in the case of your MWE, leading to the error).
If you need to specify other LaTeX figure options, see the "Plot" section of the knitr chunk options documentation: in particular, if you want to use position "h", use fig.pos="h" in your chunk options, as indicated by
fig.pos: (''; character) a character string for the figure position arrangement to be used in \begin{figure}[fig.pos]

Related

Line break between table and plot with pdf_document output in R Markdown

I'm creating a document with PDF output in R Markdown.
My code creates a number of tables and figures in a "for" loop, within the same code chunk. All the tricks I've seen to create a line break in a PDF document - with all sorts of combinations and permutations of preceding slashes and spaces (e.g., newline, linebreak, , &nbsp, etc) are not working. So, I decided to cut some corners and just increase the plot space in my ggplot2 margins to create the illusion of more white space between my table and my graph.
Ideally, I'd love to know how to actually insert white space ( a line, or several lines) properly within a single code chunk, without getting an error.
However, I'd also settle for figuring out why the following code "works" to produce extra space in my ggplot2 plot when I have some background color (first plot) but doesn't work when the background color is white!!? This is truly bizarre behavior folks!
See my screenshot below and the code, with two identical code chunks - except the background color of the plot - where one plot with a light beige background shows the desired "white space" between the table and the plot title, and the plot following (white background) does not show the same "white space".
---
title: "I need a line break"
output: pdf_document
---
```{r setup, include=FALSE}
library(ggplot2)
library(knitr)
library(ggplot2)
knitr::opts_chunk$set(echo = FALSE,
include = TRUE,
message = FALSE,
warning = FALSE,
error = FALSE)
```
```{r, results = "asis", fig.height = 2}
for (i in 1) {
mytable <- kable((iris[c(1,2),]), booktabs = TRUE)
print(mytable)
myplot <- ggplot(iris) +
labs(title = "Gorgeous plot") +
geom_point(aes(Sepal.Length, Sepal.Width)) +
theme_void() +
theme(plot.background = element_rect(fill = "#FFFEEB", color = "white"),
plot.margin = unit(c(1, 0, 0, 0), "cm"))
print(myplot)
}
```
```{r, results = "asis", fig.height = 2}
for (i in 1) {
mytable <- kable((iris[c(1,2),]), booktabs = TRUE)
print(mytable)
myplot <- ggplot(iris) +
labs(title = "Gorgeous plot") +
geom_point(aes(Sepal.Length, Sepal.Width)) +
theme_void() +
theme(plot.background = element_rect(fill = "white", color = "white"),
plot.margin = unit(c(1, 0, 0, 0), "cm"))
print(myplot)
}
```
Can you add linebreaks with cat to solve your problem? E.g.
---
title: "I need a line break"
output: pdf_document
---
```{r setup, include=FALSE}
library(ggplot2)
library(knitr)
library(ggplot2)
knitr::opts_chunk$set(echo = FALSE,
include = TRUE,
message = FALSE,
warning = FALSE,
error = FALSE)
```
```{r, results = "asis", fig.height = 2}
for (i in 1:2) {
mytable <- kable((iris[c(1,i),]), booktabs = TRUE)
print(mytable)
cat("\\ ")
cat("\\linebreak")
cat("\\linebreak")
cat("\\linebreak")
myplot <- ggplot(iris) +
labs(title = "Gorgeous plot") +
geom_point(aes(Sepal.Length, Sepal.Width)) +
theme_void()
print(myplot)
cat("\\linebreak")
cat("\\linebreak")
}
```
(The cat("\\ ") is needed so that there is a line to end)

Adding ggplot code chunk to RMarkdown breaks HTML tabset

Newish R programmer here and new to SO...
I have an RMarkdown doc where I've created tabs.
For some reason, when I try to knit, the chunks with ggplot seem to break the tabset code, because when I omit those chunks, the doc appears as it should.
When the ggplot code include = FALSE, the tabs are included, but the below code appears to break the tabs. Any ideas for workarounds? Is this a bug, or my novice-ness? Thanks in advance...
## Options {.tabset .tabset-pills}
```{r Barplot, include=TRUE,echo=FALSE}
barplot_group_count <- ggplot(data=group_count, aes(x=Group, y=n)) +
geom_bar(stat = "identity") +
labs(title = "Count of Groups According to PSA Attributes") +
coord_flip()
barplot_group_count
```
I found that if I wrapped the call to ggplotly(), the graphs are displayed.
barplot_group_count <- ggplot(data=group_count, aes(x=Group, y=n)) +
geom_bar(stat = "identity") +
labs(title = "Count of Groups According to PSA Attributes") +
theme(plot.margin=unit(c(2, 1.5, 1.5, 1.5),"cm"))+
coord_flip()
ggplotly(barplot_group_count)

Rmarkdown automatically use function on plots

I'm producing an RMarkdown document where each chunk produces a plot.
For each of these plots, I'd like to apply a special formatting function that adjusts the way the title appears.
Is there a way to tell knitr/rmarkdown to apply this special function to each chunk's plot? For example, maybe there's a chunk option like {r, fig.function = adjust_title_position}?
The motivation is that I don't want to have to retype the function call separately for each plot (e.g. adjust_title_position(plot_42)) and, at the same time, I don't want to use something like lapply(my_plots, adjust_title_position) which would require all the plots to be defined in one place.
Below is a minimal example of the RMarkdown file to which this could be applied.
---
title: "RMD_Example"
output: html_document
---
```{r setup, include=FALSE}
# Load ggplot2
library(ggplot2)
# Define helper function
adjust_title_position <- function(p) {
# Shift the horizontal position of the plot title
p_built <- invisible(ggplot2::ggplot_build(p))
gt <- invisible(ggplot2::ggplot_gtable(p_built))
gt$layout[which(gt$layout$name == "title"), c("l", "r")] <- c(2, max(gt$layout$r))
# Prints the plot to the current graphical device
gridExtra::grid.arrange(gt, newpage = TRUE)
# Invisibly return the gtable object
invisible(gt)
}
```
```{r plot_1}
ggplot(mtcars) + geom_point(aes(x = wt, y = mpg)) +
labs(title = "Weights and miles-per-gallon")
```
```{r plot_2}
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point() +
labs(title = "Sepal length and width")
```
You could reregister the print-method for ggplot-objects.
# Define helper function
adjusted_title_position <- function(p) {
# Shift the horizontal position of the plot title
p_built <- invisible(ggplot2::ggplot_build(p))
gt <- invisible(ggplot2::ggplot_gtable(p_built))
gt$layout[which(gt$layout$name == "title"), c("l", "r")] <- c(2, max(gt$layout$r))
# Prints the plot to the current graphical device
gridExtra::grid.arrange(gt, newpage = TRUE)
}
# Reregister print.ggplot
assignInNamespace("print.ggplot", adjusted_title_position, asNamespace("ggplot2"))

subfigures stacked vertically with knitr

I am trying to put two subfigures stacked vertically rather than side-by-side using knitr in a Latex document. In Latex, I usually do this by putting a '\par' command between two subfloats that need to be stacked op top of one another, but I don't know how to pass that command on to knitr.
I've only been able to find one topic on this, Subfigures or Subcaptions with knitr?. The example given by Yihui puts the figures side by side. The final answer in that thread achieves my goal, but using quite a bit of hacking, so I'm wondering whether there's a simple way to pass this command to knitr directly?
MWE:
\documentclass[a4paper]{article}
\usepackage[font=footnotesize]{subfig}
\begin{document}
<<GeneratePlot>>=
library(ggplot2)
library(tikzDevice)
P1 <- ggplot(data=mpg, aes(x=displ, y=hwy, colour=factor(cyl)))+
geom_line()
P2 <- ggplot(data=mpg, aes(x=displ, y=hwy, colour=factor(cyl)))+
geom_point()
#
<<fig1,eval=TRUE,echo=FALSE,dev='tikz', fig.width=6, fig.height=3, out.width='1\\textwidth', fig.cap='Two figures', fig.subcap=c('Top','Bottom'), fig.show='asis', fig.pos='!htpb', fig.align='center', dependson='GeneratePlot'>>=
P1
P2
#
\end{document}
I've also tried creating the plot object in the same chunk as the output, but that yields the same result and in my original document the plot objects are generated as part of a larger chunk, so it will have the same structure as the MWE.
You can do that with the non-CRAN package oaReporting.
\documentclass[a4paper]{article}
\usepackage{float}
\usepackage{subcaption}
\begin{document}
<<setup, include=FALSE>>=
knitr::opts_chunk$set(fig.path="figures/")
library(oaReporting)
#
<<GeneratePlot>>=
library(ggplot2)
P1 <- ggplot(data=mpg, aes(x=displ, y=hwy, colour=factor(cyl)))+
geom_line()
P2 <- ggplot(data=mpg, aes(x=displ, y=hwy, colour=factor(cyl)))+
geom_point()
path1 <- createPlot(P1, file="./figures/P1.pdf")
path2 <- createPlot(P2, file="./figures/P2.pdf")
captions <- c("P1", "P2")
#
<<results="asis", fig.keep="none">>=
insertFigures(paths = c(path1, path2), captions=captions,
generalCaption = "P1 and P2",
generalLabel = "fig:P1andP2",
posMultipleFig = "H",
nCol = 1,
width = 0.7)
#
\end{document}
This has been officially supported in knitr 1.17.19 (current on Github). To add \par between the two subfigures, you can use the chunk option fig.sep = '\\par', e.g.,
\documentclass[a4paper]{article}
\usepackage[font=footnotesize]{subfig}
\begin{document}
<<GeneratePlot>>=
library(ggplot2)
library(tikzDevice)
P1 <- ggplot(data=mpg, aes(x=displ, y=hwy, colour=factor(cyl)))+
geom_line()
P2 <- ggplot(data=mpg, aes(x=displ, y=hwy, colour=factor(cyl)))+
geom_point()
#
<<fig1, echo=FALSE,dev='tikz', fig.width=6, fig.height=3, out.width='1\\textwidth', fig.cap='Two figures', fig.subcap=c('Top','Bottom'), fig.show='asis', fig.pos='!htpb', fig.sep='\\par'>>=
P1
P2
#
\end{document}
Output:

Formatting output with Knitr, ggplot2 and xtable

I am trying to achieve the following task with Knitr, ggplot2 and xtables:
Generate several annotated plots of beta-distributions with ggplot2
Write the output in a layout such that I have a plot, and a corresponding summary Stats table following it, for every plot.
Write the code such that both PDF and HTML reports can be a generated in a presentable way
Here is my attempt at this task (Rnw file):
\documentclass{article}
\begin{document}
Test for ggplot2 with Knitr
<<Initialize, echo=FALSE>>=
library(ggplot2)
library(ggthemes)
library(data.table)
library(grid)
library(xtable)
library (plyr)
pltlist <- list()
statlist <- list()
#
The libraries are loaded. Now run the main loop
<<plotloop, echo=FALSE>>=
for (k in seq(1,7)){
x <- data.table(rbeta(100000,1.6,14+k))
xmean <- mean(x$V1, na.rm=T)
xqtl <- quantile(x$V1, probs = c(0.995), names=F)
xdiff <- xqtl - xmean
dens <- density(x$V1)
xscale <- (max(dens$x, na.rm=T) - min(dens$x, na.rm=T))/100
yscale <- (max(dens$y, na.rm=T))/100
y_max <- max(dens$y, na.rm=T)
y_intercept <- y_max-(10*yscale)
data <- data.frame(x)
y <- ggplot(data, aes(x=V1)) + geom_density(colour="darkgreen", size=2, fill="green",alpha=.3) +
geom_vline(xintercept = xmean, colour="blue", linetype = "longdash") +
geom_vline(xintercept = xqtl, colour="red", linetype = "longdash") +
geom_segment(aes(x=xmean, xend=xqtl, y=y_intercept, yend=y_intercept), colour="red", linetype = "solid", arrow = arrow(length = unit(0.2, "cm"), ends = "both", type = "closed")) +
annotate("text", x = xmean+xscale, y = y_max, label = paste("Val1:",round(xmean,4)), hjust=0) +
annotate("text", x = xqtl+xscale, y = y_max, label = paste("Val2:",round(xqtl,4))) +
annotate("text", x = xmean+10*xscale, y = y_max-15*yscale, label = paste("Val3:",round(xdiff,4))) +
xlim(min(dens$x, na.rm=T), xqtl + 9*xscale) +
xlab("Values") +
ggtitle("Beta Distribution") +
theme_bw() +
theme(plot.title = element_text(hjust = 0, vjust=2))
pltlist[[k]] <- y
statlist[[k]] <- list(mean=xmean, quantile=xqtl)
}
stats <- ldply(statlist, data.frame)
#
Plots are ready. Now Plot them
<<PrintPlots, warning=FALSE, results='asis', echo=FALSE, cache=TRUE, fig.height=3.5>>=
for (k in seq(1,7)){
print(pltlist[[k]])
print(xtable(stats[k,], caption="Summary Statistics", digits=6))
}
#
Plotting Finished.
\end{document}
I am faced with several issues after running this code.
When I run this code just as R code, Once I try to print the plots in the list, the horizontal line from the geom_segment part starts to move all over the place. However if I plot the figures individually, without putting them in a list, the figures are fine, as I would expect them to be.
Only the last plot is as I would expect the output to be, in all the other plots, the geom_segment line moves around randomly.
I am also unable to put a separate caption for the Plots as I can for the Tables.
Points to note :
I am storing the beta-random numbers in data.table since in our actual code, we are using data.table. However for the purposes of testing ggplot2 in this way, I convert the data.table into a data.frame, as ggplot2 requires.
I also need to generate the random numbers within the loop and generate the plots per iteration (so something like first generating the random numbers and then using melt would not work here), since generating the random numbers is emulating a complex database call per iteration of the loop.
I am using RStudio Version 0.98.1091 and
R version 3.1.2 (2014-10-31) on Windows 8.1
This is the expected Plot:
This is the plot I am getting when plotting from the list:
My output in PDF form :
PDF Output
Please advice if there are any ideas for solutions.
Thank you,
SG
I don't know why the horizontal line in geom_segment is "moving around" from plot to plot, rather than spanning xmean to xqtl. However, I was able to get the horizontal line in the correct location by getting the value from the stats data frame, rather than from direct calculation of the mean and quantile. You just have to create the stats data frame before the loop, rather than after, so that you can use it in the loop.
stats <- ldply(statlist, data.frame)
for (k in seq(1,7)){
...
y <- ggplot(data, aes(x=V1)) +
...
geom_segment(aes(x=stats[k,1], xend=stats[k,2], y=y_intercept, yend=y_intercept),
colour="red", linetype = "solid",
arrow = arrow(length = unit(0.2, "cm"), ends = "both", type = "closed")) +
...
pltlist[[k]] <- y
statlist[[k]] <- list(mean=xmean, quantile=xqtl)
}
Hopefully, someone else will be able to explain the anomalous behavior, but at least this seems to fix the problem.
For the figure caption, you can add a fig.cap argument to the chunk where you plot the figures, although this results in the same caption for each figure and causes the figures and tables to be plotted in separate groups, rather than interleaved:
<<PrintPlots, warning=FALSE, results='asis', echo=FALSE, cache=TRUE, fig.cap="Caption", fig.height=3.5>>=
for (k in seq(1,7)){
print(pltlist[[k]])
print(xtable(stats[k,], caption="Summary Statistics", digits=6))
}
You might want to use R Markdown and knitr which is easier than using LaTeX and R (as also zhaoy suggested).
You might also want to check out the ReporteRs package. I think it is actually easier to use than knitr. However, you cannot generate PDFs with it. But you can use pandoc to convert them into PDFs.

Resources