How to save R code and number on one page in RMarkdown when creating pdf using knitr

Is there a way in RMarkdown to make the R-code and the figure that it creates appear on the same page, please? I use knit pdf. For example,

```{r}
ggplot(df, aes(x = x, y = y, col = sex_f)) + geom_point() +
  ggtitle("Data from Children") +
  labs(x = "Age (months)", y = "Height (cms)", col = "Child Gender") +
  geom_smooth(method = "lm", se = FALSE) + 
  facet_grid(sex_f ~ town_f)
```

(which does not play) creates a code on one page and a graph on the next page. I tried to set the width and height of the shape everywhere, for example

```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.width = 4, fig.height = 3, 
                       fig.path = 'Intro_Figs/',
                       fig.show = 'asis',
                       fig.align = 'center',
                       warning = FALSE, message = FALSE)
```

with very small values, but this does not interfere with all the bad page breaks. Thanks.

+4
source share

Source: https://habr.com/ru/post/1613085/


All Articles