2
votes

When I generate plot in Rmarkdown, it shows up under Plots in Rstudio but does not include it when I knit it. How can I include the plot in the markdown?

I also have "Create a standalone HTML document" checked and similarly plots don't show up in that standalone HTML document.

e.g. I run this code in Rmarkdown file but plot does not stay in the knitted document.

boxplot(bwt ~ smoke, data=birthwt,
        xlab = "drivetrain (0 = don't smoke, 1 = smoke)",
        ylab = "birth wt. in g",
        main = "birth weight vs smoking status",
        pch = 20,
        cex = 2,
        col = "darkslategray",
        border = "goldenrod3")

Please advise.

1
The code for the plot is as it is and I just have text above and below the code snippet. After I get the plot on the doc, I will enclose the code in ``` ``` to make it into a block. - user15300490
I'm thinking one option is to save the plot from Plots separately and try to import it back into Rmarkdown? - user15300490
If you are wrapping it with r code chunk, it should work - akrun
Can you explain what the initial r code chunk does or how it works? {r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) - user15300490
That is just a template I had in one of my previous rmd files. I think it is not needed for this to work here. It is just a global declaration to set the echo = TRUE for all chunks if not overridden by echo = FALSE in each chunk - akrun

1 Answers

2
votes

It does work in the following small snippet of RMD

---
title: "testing"
author: "akrun"
date: "10/05/2021"
output: html_document
---

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

```{r}
library(MASS)
data(birthwt)
boxplot(bwt ~ smoke, data=birthwt,
        xlab = "drivetrain (0 = don't smoke, 1 = smoke)",
        ylab = "birth wt. in g",
        main = "birth weight vs smoking status",
        pch = 20,
        cex = 2,
        col = "darkslategray",
        border = "goldenrod3")

```

-output

enter image description here