rmarkdown ggplot tufte 主题背景色

问题描述 投票:0回答:2

在带有tufte主题的

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

有没有办法使

ggplot2
的背景与带有 tufte 主题的
Rmarkdow
中 html 文件的背景混合。见下文

r ggplot2 r-markdown
2个回答
0
投票

我没有找到将 ggplot 背景设置为 100% 透明度或 NULL 的方法。但是您可以在 ggplot 对象的

plot.background
中定义
legend.background
theme
,以便与 knit'ed html 的样式兼容。例如在此 ggplot 中,结果为 this render:

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

0
投票

我也遇到了完全相同的问题。这是一个两步解决方案:

  1. 您的
    ggplot()
    的背景需要是透明的。在
    plot.background = element_blank()
    函数内设置
    panel.background = element_blank()
    theme()
  2. 最重要的方面是代码块中的参数
    bg = transparent
{r dev.args = list(bg = 'transparent')}

data %>%
 ggplot() +
 geom_line() +
 theme(
  plot.background = element_blank(),
  panel.background = element_blank()
  )
© www.soinside.com 2019 - 2024. All rights reserved.