0

I created a Rmarkdown document and want to number my figures. What I am struggling with is if a plot gives me several output like plot("glm Object")?

Here is a minimal example:

---
documentclass: article
output: pdf_document
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(fig.path = 'figures/', fig.pos = 'htb!', echo = TRUE)
```

```{r, fig.cap = "Test"}
data(warpbreaks)
daten <- as.data.frame(warpbreaks)
test <- glm(breaks ~ tension + wool , family = "poisson", data = daten)
plot(test)
```
Michael Harper
  • 14,721
  • 2
  • 60
  • 84
Dima Ku
  • 243
  • 2
  • 11

1 Answers1

0

You can use a lesser-known feature of Rmarkdown and add captions to subfigures: https://stackoverflow.com/a/49086985/7347699

For your example:

---
documentclass: article
output: pdf_document
header-includes:
   - \usepackage{subfig}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(fig.path = 'figures/', fig.pos = 'htb!', echo = TRUE)
```

```{r, fig.cap = "Test",  fig.subcap=c('sub 1', 'sub 2', 'sub 3', 'sub 4'), out.width='.49\\linewidth', fig.asp=1, fig.ncol = 2}
data(warpbreaks)
daten <- as.data.frame(warpbreaks)
test <- glm(breaks ~ tension + wool , family = "poisson", data = daten)
plot(test)
```

enter image description here

Alternative Approach

A potentially easier way plotting multiple R plots is to use the par function before plotting. The benefit of this is its simplicity, and that you do not need to load addition LaTeX packages in the header. However, it is not possible to add a sub-caption to each plot.

---
documentclass: article
output: pdf_document
---

```{r, fig.cap = "Test", fig.asp = 1}
data(warpbreaks)
daten <- as.data.frame(warpbreaks)
test <- glm(breaks ~ tension + wool , family = "poisson", data = daten)

par(mfrow = c(2, 2))
plot(test)
```

enter image description here

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
  • i tried your code but dont get the same result as you, please check my edit – Dima Ku Mar 20 '18 at 20:20
  • It looks like you haven't got the settings right in the code chunk. As you haven't supplied the code, it is You want to make sure that the subfigure captions are in here: `fig.subcap=c('sub 1', 'sub 2', 'sub 3', 'sub 4'), out.width='.49\\linewidth', fig.asp=1, fig.ncol = 2`. Also looks like you have changed `fig.ncol` to 4. – Michael Harper Mar 20 '18 at 20:57
  • I removed the edit. It doesn't relate to the code posted in the question so makes things confusing. Here is a link to your figure though: https://i.stack.imgur.com/6yO7V.png – Michael Harper Mar 20 '18 at 20:59
  • it still doesnt work, mabye the reason is because i have text before and after the chunk? – Dima Ku Mar 20 '18 at 21:04
  • You seem to be combining the plots into pairs? P.S. As your first post, I am happy to help out a bit. But generally speaking, it is not for those to answer to troubleshoot the code when you try and implement it yourself. If you discover another question separate to the original one, it can often be better to start a new question – Michael Harper Mar 20 '18 at 21:10
  • no i just create a plot function that give me the four plots i posted. And now i want to have the same layout like in your answer but i allways get my plots in a rown and don't 2 rows with 2 plots like you. I think i will post a new question – Dima Ku Mar 20 '18 at 21:18
  • Okay, that sounds strange. Without looking at your code it is impossible to diagnose. Worth making another question, and please make sure it is fully reproducible. Post the link here so that I can help answer it :) Also would appreciate if you could upvote my answer :) – Michael Harper Mar 20 '18 at 21:20
  • https://stackoverflow.com/questions/49395974/place-figures-right-in-r-markdown here is the new question . i cant upvote because this account is new. dont enough reputations sry – Dima Ku Mar 20 '18 at 23:54