如何使用“R-生成”?在knitr制作的HTML5幻灯片中绘制半透明背景? [英] How to use a "R-generated" plot as a semi-transparent background in an HTML5 slide made by knitr?
问题描述
我想在(HTML5)幻灯片的第一页上添加一个情节。我能以动态方式实现这一目标吗?比如,背景图像将由R代码生成,而不是插入半透明的PNG图像。谢谢。
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.
更新:我想要的是
推荐答案
你可以使用块选项 dev.args
来实现此目的。您需要正确调整图像大小以适合幻灯片。
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)
```
此块将生成透明的png。请阅读文档? png
获取可以传递的参数列表。以下是来自文档的一个有用的文章。
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支持透明背景:使用bg =transparent。 (并非所有PNG查看器都能正确呈现透明文件。)
png supports transparent backgrounds: use bg = "transparent". (Not all PNG viewers render files with transparency correctly.)
更新。对于使用 ggplot2
的图表, alpha
参数可用于控制透明度。
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。要将动态图像用作背景,您需要动态设置幻灯片的背景。这可以通过 slidify 轻松实现[披露:我是作者]
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]
这是一个带有Rmd文件的要点,当滑动时会提供透明背景。
Here is a gist with an Rmd file which when slidified will give a transparent background.
这篇关于如何使用“R-生成”?在knitr制作的HTML5幻灯片中绘制半透明背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!