首页 > 解决方案 > rmarkdown ggplot tufte 主题背景色

问题描述

Rmarkdownwith tufte 主题中,当我使用 时ggplot2,我看到背景是白色的,并且不与 html 文件的背景颜色混合。见下文。使用透明的ggplot2没有帮助。

有没有办法在tufte 主题中使ggplot2背景与 html 文件的背景混合。Rmarkdow见下文

在此处输入图像描述

标签: rggplot2r-markdown

解决方案


我没有找到将 ggplot 的背景设置为 100% 透明或 NULL 的方法。但是您可以在 ggplot 对象中定义and plot.background,使其与 knit'ed html 的样式兼容。例如在这个 ggplot 中,导致这个渲染legend.backgroundtheme

```{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 代码


推荐阅读