How to use a “R-generated” plot as a semi-transpar

2020-07-23 04:06发布

问题:

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.