如何在Quarto RevealJS演示文稿中移除ggplot2图表边框?
移除Quarto RevealJS演示文稿中ggplot2图表的黑色边框
问题描述
使用Quarto制作RevealJS演示文稿时,ggplot2生成的图表(第2页)周围出现黑色边框,需移除该边框。可复现代码如下:
Quarto演示文稿代码
--- title: "Test Presentation" format: revealjs: theme: [default, custom.scss] --- ## Some plot ```{r} library(ggplot2) ggplot(mtcars) + geom_boxplot(aes(as.factor(cyl), disp, fill = cyl)) + theme_minimal() + theme( legend.position = "none", panel.grid = element_blank(), axis.title = element_text(size = 20, color = "#fefefe"), axis.text = element_text(size = 15, color = "#fefefe"), plot.title = element_text(size = 20, color = "#fefefe"), plot.background = element_rect(fill = "#505050"), panel.background = element_rect(fill = "#505050"), axis.line.x.top = element_line(color = "#fefefe"), axis.line.x.bottom = element_line(color = "#fefefe"), axis.line.y.left = element_line(color = "#fefefe"), axis.line.y.right = element_line(color = "#fefefe") ) + guides(x.sec = "axis", y.sec = "axis")
### 自定义SCSS代码 ```scss /*-- scss:defaults --*/ // fonts $font-family-sans-serif: "Palatino Linotype", "Book Antiqua", Palatino, FreeSerif, serif !default; // colors $body-bg: #505050; $body-color: #ffffff; $link-color: #51483d !default; $selection-bg: #26351c !default; // headings $presentation-heading-font: "Palatino Linotype", "Book Antiqua", Palatino, FreeSerif, serif !default; $presentation-heading-color: #ffffff !default; /*-- scss:rules --*/ .reveal a { line-height: 1.3em; }
解决方案
黑色边框通常来自两个地方:ggplot图表自身的边框,或是RevealJS为图片容器添加的默认边框。以下两种方法可解决问题:
方法1:修改ggplot2主题设置
在theme()函数中添加plot.border = element_blank(),直接移除图表自身的边框:
ggplot(mtcars) + geom_boxplot(aes(as.factor(cyl), disp, fill = cyl)) + theme_minimal() + theme( legend.position = "none", panel.grid = element_blank(), axis.title = element_text(size = 20, color = "#fefefe"), axis.text = element_text(size = 15, color = "#fefefe"), plot.title = element_text(size = 20, color = "#fefefe"), plot.background = element_rect(fill = "#505050"), panel.background = element_rect(fill = "#505050"), axis.line.x.top = element_line(color = "#fefefe"), axis.line.x.bottom = element_line(color = "#fefefe"), axis.line.y.left = element_line(color = "#fefefe"), axis.line.y.right = element_line(color = "#fefefe"), plot.border = element_blank() # 新增该行移除图表边框 ) + guides(x.sec = "axis", y.sec = "axis")
方法2:修改自定义SCSS样式
如果边框来自RevealJS的图片容器,在custom.scss的scss:rules区块中添加样式,移除图片的边框和阴影:
/*-- scss:rules --*/ .reveal a { line-height: 1.3em; } // 移除所有演示文稿图片的边框与阴影 .reveal section img { border: none !important; box-shadow: none !important; }
若仅需针对ggplot生成的图表修改,可给代码块添加自定义类名后设置针对性样式,但全局移除的方式更简洁高效。
内容的提问来源于stack exchange,提问作者Shubham
相关产品推荐
相关产品推荐

