Quarto Revealjs列布局下地图适配空间问题求助
解决Quarto Revealjs中代码与地图并排时地图过小的问题
核心问题说明
你使用output-location: column实现代码与地图并排时出现地图过小的情况,本质是par(mar)仅对R基础绘图系统生效,对ggplot无效;而固定数值的out-width/out-height会破坏地图原生宽高比。以下是可行的解决方法:
方案1:清除ggplot内外边距并精简主题
针对sf地图的ggplot绘图,通过theme()清除绘图区域边距,同时移除不必要的主题元素(如坐标轴、网格),让地图尽可能占满分配空间:
--- format: revealjs --- ## A title ```{r} #| output-location: column #| echo: true library(spData) library(sf) library(ggplot2) ggplot() + geom_sf(data = nz) + theme_minimal() + # 清除绘图区域边距 theme(plot.margin = margin(0, 0, 0, 0, unit = "cm"), # 移除无需显示的元素 axis.text = element_blank(), axis.title = element_blank(), panel.grid = element_blank())
### 方案2:手动分配列宽比例 通过Quarto的`column`参数自定义代码与地图的列宽占比,给地图分配更多空间,同时用`out-width: 100%`让地图填满所在列: ```r --- format: revealjs --- ## A title ```{r} #| output-location: column #| column: [35%, 65%] # 代码占35%宽度,地图占65% #| echo: true #| out-width: 100% library(spData) library(sf) library(ggplot2) ggplot() + geom_sf(data = nz) + theme_minimal() + theme(plot.margin = margin(0, 0, 0, 0, "cm"))
### 方案3:固定画布尺寸并自适应列宽 用`fig.width`和`fig.height`设置地图的原始宽高比,再配合`out-width: 100%`让地图自适应列宽,既保证比例又填满空间: ```r --- format: revealjs --- ## A title ```{r} #| output-location: column #| echo: true #| fig.width: 8 #| fig.height: 6 # 保持4:3的原生宽高比 #| out-width: 100% library(spData) library(sf) library(ggplot2) ggplot() + geom_sf(data = nz) + theme_minimal() + theme(plot.margin = margin(0, 0, 0, 0, "cm"))
--- 内容的提问来源于stack exchange,提问作者Maël
相关产品推荐
相关产品推荐

