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

风流意气都作罢 提交于 2019-12-06 07:12:16

问题


I want to add a plot to my first page of the (HTML5) slide. Can I achieve this in a dynamically way? Say, the background image will be generated by R code, rather than insert a semi-transparent PNG image. Thank you.

Update: What I want is


回答1:


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.



来源:https://stackoverflow.com/questions/13813471/how-to-use-a-r-generated-plot-as-a-semi-transparent-background-in-an-html5-sli

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