How to keep R code and figure on the same page in RMarkdown when producing a pdf using knitr

核能气质少年 提交于 2019-12-06 02:10:39

问题


Is there a way in RMarkdown to force R code and the figure that it produces to appear on the same page please? I am using 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 is not reproducible) produces code on one page and a plot on the next page. I have tried setting the figure width and height globally, 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 prevent all bad page breaks. Thank you.

来源:https://stackoverflow.com/questions/33328890/how-to-keep-r-code-and-figure-on-the-same-page-in-rmarkdown-when-producing-a-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!