在带有tufte主题的
Rmarkdown
中,当我使用ggplot2
时,我看到背景是白色的,并且不与html文件的背景颜色混合。见下文。在 ggplot2
中使用透明没有帮助。
有没有办法使
ggplot2
的背景与带有 tufte 主题的 Rmarkdow
中 html 文件的背景混合。见下文
我没有找到将 ggplot 背景设置为 100% 透明度或 NULL 的方法。但是您可以在 ggplot 对象的
plot.background
中定义 legend.background
和 theme
,以便与 knit'ed html 的样式兼容。例如在此 ggplot 中,结果为 :
```{r fig-margin, fig.margin = TRUE, fig.cap = "MPG vs horsepower, colored by transmission.", fig.width=3.5, fig.height=3.5, cache=TRUE, message=FALSE}
library(ggplot2)
mtcars2 <- mtcars
mtcars2$am <- factor(
mtcars$am, labels = c('automatic', 'manual')
)
ggplot(mtcars2, aes(hp, mpg, color = am)) +
geom_point() + geom_smooth() +
theme(legend.position = 'bottom',
plot.background = element_rect(fill = "#fffff8"),
legend.background = element_rect(fill = "#fffff8"))
```
我从这里
获取了.Rmd代码我也遇到了完全相同的问题。这是一个两步解决方案:
ggplot()
的背景需要是透明的。在 plot.background = element_blank()
函数内设置 panel.background = element_blank()
和 theme()
。bg = transparent
:{r dev.args = list(bg = 'transparent')}
data %>%
ggplot() +
geom_line() +
theme(
plot.background = element_blank(),
panel.background = element_blank()
)