Gordon Smyth
Gordon Smyth

Reputation: 763

knitr: add to previous plot in new code chunk

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?

Upvotes: 7

Views: 1979

Answers (2)

Yihui Xie
Yihui Xie

Reputation: 30114

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}

Upvotes: 5

eipi10
eipi10

Reputation: 93811

You can show code without evaluating it by adding the chunk option eval=FALSE. If you only want to show the final version of the plot with the regression line added, then use eval=FALSE for the first plot(x,y).

Then we add two chunks for the regression line: One is the complete code needed to render the plot, but we don't want to display this code, because we don't want to repeat the plot(x,y) call. So we add a second chunk that we echo, to display the code, but don't evaluate.

---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r data}
set.seed(10)
x=rnorm(10)
y=rnorm(10)
```

First we plot y vs x with a scatterplot:

```{r scatterplot, eval=FALSE}
plot(x, y)
```

Then we regress y on x and add the least squares line to the plot:

```{r addline, eval=FALSE}
fit <- lm(y~x)
abline(fit)
```

```{r echo=FALSE}
plot(x,y)
fit <- lm(y~x)
abline(fit)
```

Here's what the output document looks like:

enter image description here

Upvotes: 3

Related Questions