How to use a “R-generated” plot as a semi-transparent background in an HTML5 slide made by knitr?

做~自己de王妃 提交于 2019-12-04 12:10:11

You can use the chunk option dev.args to achieve this. You will need to size the image correctly to fit your slide.

```{r dev.args = list(bg = 'transparent')}
 plot(1:10, 1:10)
```

This chunk will generate a transparent png. Please read the documentation ? png to get a list of arguments you can pass. Here is one useful piece onf info from the docs.

png supports transparent backgrounds: use bg = "transparent". (Not all PNG viewers render files with transparency correctly.)

UPDATE. For plots using ggplot2, the alpha parameter can be used to control transparency.

```{r dev.args = list(bg = 'transparent'), echo = F, comment = NA, message = F}
library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
  geom_point(alpha = 0.1) + 
  geom_smooth(alpha = 0.1)
```

UPDATE2. To use a dynamic image as background, you need to set the background of the slide dynamically. This can be easily achieved in slidify [Disclosure: I am the author]

Here is a gist with an Rmd file which when slidified will give a transparent background.

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