Why Doesn't My knitr Example Produce Graphics?
At a glance
- 1. Run the plotting code
Make sure the chunk is evaluated and contains a plotting command that creates a graphic.
- 2. Check chunk options
Look for options such as eval=FALSE, include=FALSE, fig.show='hide', or fig.keep='none'.
- 3. Render the document
Use the document's renderer to view the output; knitting alone may leave plots in separate files.
- 4. Print nested plots
Explicitly print ggplot or grid graphics created inside a function, loop, or other non-top-level context.
A knitr document can run without showing a plot if the chunk never draws one, its options suppress figures, or you are viewing an intermediate file rather than the rendered document. Start with a minimal working example, then check how the plot is created and which chunk options are in effect.
A minimal working example
Save this as example.Rmd:
---
title: "A plot made with knitr"
output: html_document
---
```{r}
plot(1:10, (1:10)^2,
xlab = "x", ylab = "x squared",
main = "A base R plot")
```
In RStudio, click Knit. Or render it from R with:
rmarkdown::render("example.Rmd")
The resulting HTML document should contain the plot. A basic plot() call is useful for troubleshooting because it does not require an additional plotting package.
Check whether the chunk runs and the plot is allowed
Chunk options can prevent a graphic from appearing. Check both the chunk header and any global options set earlier in the document, such as in a setup chunk.
| Option | Effect on the graphic |
|---|---|
eval=FALSE | The chunk is not run, so it cannot create a plot. |
include=FALSE | The chunk runs, but its output—including figures—is omitted from the document. |
fig.show='hide' | The figure is created but not displayed in the document. |
fig.keep='none' | Figures from the chunk are discarded. |
For a first test, remove these options or set the chunk to a plain header such as ```{r}. If you use knitr::opts_chunk$set() in a setup chunk, check whether it applies a figure-hiding option to every chunk. A plot can be generated successfully and still be absent from the final document because of those settings.
Make sure the code actually draws a plot
Creating a data frame, defining a plotting function, or assigning a graphic to a variable does not always display anything by itself. The chunk needs to execute a plotting expression. For example, this base R code draws a plot:
x <- 1:10
plot(x, x^2)
With ggplot2, a top-level plotting expression is normally displayed automatically:
library(ggplot2)
ggplot(data.frame(x = 1:10), aes(x, x^2)) +
geom_point()
But a plot created inside a function, loop, or other context may need to be printed explicitly. For example:
library(ggplot2)
make_plot <- function() {
p <- ggplot(data.frame(x = 1:10), aes(x, x^2)) +
geom_point()
print(p)
}
make_plot()
The same rule applies to grid-based graphics: explicitly call print() when automatic display does not happen in a nested context. Base R plotting commands such as plot() draw directly, so they usually do not need print().
Render the right output
knitr evaluates chunks and records their results, including graphics. In an R Markdown workflow, rmarkdown::render() (or RStudio’s Knit button) then creates the requested output document. If you only run the R code, or inspect intermediate Markdown without opening it in a viewer, you may not see the plot where you expect it. Knitr commonly saves graphics as separate image files and inserts references to those files in the document.
If the output contains a broken-image placeholder, check that the generated image file exists and that you are opening the rendered document in its expected location. When moving the document or copying only the HTML file, include any accompanying figure directory as well.
Common mistakes
- Using
include=FALSEto hide code: It also hides the chunk’s figure. Useecho=FALSEif you want to hide the source code but keep the plot. - Confusing text and figure output: Hiding printed text is not the same as hiding graphics. Inspect figure-related options such as
fig.showandfig.keep. - Expecting an assignment to display a plot: Assigning a graphic to an object may not render it in a nested context; print the object explicitly when needed.
- Checking only the source file: The plot appears in rendered output, not in the R Markdown source itself.
- Assuming every chunk must produce a figure: A chunk that computes values or defines functions will not make a graphic unless it calls plotting code.
If the minimal example works but your document does not, add your plotting code back a piece at a time and check the chunk options around it. That usually identifies whether the issue is figure suppression, an unevaluated chunk, or how the plot is being printed.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.