Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knitr: add to previous plot in new code chunk

Tags:

r

knitr

I am using the knitr package for R to produce a LaTeX document combining text with embedded R plots and output.

It is common to write something like this:

We plot y vs x in a scatter plot and add the least squares line:
<<scatterplot>>=
plot(x, y)
fit <- lm(y~x)
abline(fit)
@

which works fine. (For those not familiar with knitr or Sweave, this echos the code and output in a LaTeX verbatim environment and also adds the completed plot as a figure in the LaTeX document.)

But now I would like to write more detailed line-by-line commentary like:

First we plot y vs x with a scatterplot:
<<scatterplot>>=
plot(x, y)
@
Then we regress y on x and add the least squares line to the plot:
<<addline>>=
fit <- lm(y~x)
abline(fit)
@

The problem is that there are now two knitr code chunks for the same plot. The second code chunk addline fails because the plot frame created in the first code chunk scatterplot is not visible to the code in the second code chunk. The plotting window doesn't seem to be persistent from one code chunk to the next.

Is there any way that I can tell knit() to keep the plot window created by plot() active for the second code chunk?

If that is not possible, how else might I achieve LaTeX-style commentary on code lines that add to existing plots?

One Day Later

I can now see that essentially the same question has been asked before, see: How to build a layered plot step by step using grid in knitr? from 2013 and Splitting a plot call over multiple chunks from 2016. Another question from 2013 is also very similar: How to add elements to a plot using a knitr chunk without original markdown output?

like image 580
Gordon Smyth Avatar asked Dec 10 '17 06:12

Gordon Smyth


People also ask

How do I add code chunks in R Markdown?

You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).

How does adding code chunks improve the usability of your R Markdown file?

Code Chunk Options You can add options to each code chunk. These options allow you to customize how or if you want code to be processed or appear on the rendered output (pdf document, html document, etc). Code chunk options are added on the first line of a code chunk after the name, within the curly brackets.

How do I add output in R Markdown?

If you prefer to use the console by default for all your R Markdown documents (restoring the behavior in previous versions of RStudio), you can make Chunk Output in Console the default: Tools -> Options -> R Markdown -> Show output inline for all R Markdown documents .


1 Answers

You can set knitr::opts_knit$set(global.device = TRUE), which means all code chunks share the same global graphical device. A full example:

\documentclass{article}

\begin{document}
<<setup, include=FALSE>>=
knitr::opts_knit$set(global.device = TRUE)
@

First we plot y vs x with a scatterplot:
<<scatterplot>>=
x = rnorm(10); y = rnorm(10)
plot(x, y)
@
Then we regression y and x and add the least square line to the plot:
<<addline>>=
fit <- lm(y~x)
abline(fit)
@

\end{document}
like image 117
Yihui Xie Avatar answered Oct 03 '22 11:10

Yihui Xie