You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac OSX下用rayshader绘制geom_sf时转换geom到grob报错

解决rayshader调用plot_gg处理sf对象时的fontsize错误

问题原因

这是开发版ggplot2(v3.4.0.9000)与rayshader(v0.34.6)的兼容性问题:新版本ggplot2调整了geom_sf元素的gpar参数处理逻辑,对fontsize的默认值设置方式发生变化,而rayshader尚未适配该改动,导致转换grob时触发长度为0的fontsize错误。

可行解决方案

1. 手动指定所有文本元素的fontsize

在ggplot绘图时,明确设置所有涉及文本的主题元素fontsize,避免出现空值:

library(ggplot2)
library(sf)
library(rayshader)

# 替换为你的sf数据
nc <- st_read(system.file("shape/nc.shp", package="sf"))

# 绘图时强制设置fontsize
pp <- ggplot(nc) +
  geom_sf(aes(fill = AREA)) +
  labs(title = "NC Counties") +
  theme(
    plot.title = element_text(fontsize = 12),
    axis.text = element_text(fontsize = 10),
    legend.text = element_text(fontsize = 10),
    legend.title = element_text(fontsize = 10)
  )

# 调用plot_gg
plot_gg(pp, width = 5, height = 5, scale = 250)

2. 降级ggplot2到稳定版

若不需要开发版的新功能,降级至最新稳定版(如v3.4.3)可直接解决兼容性问题:

remove.packages("ggplot2")
install.packages("ggplot2")

3. 处理sf数据的缺失值

提示的“5行缺失值被移除”与fontsize错误无关,可提前清理数据:

# 移除sf对象中的缺失值行
nc_clean <- st_drop_na(nc)

内容的提问来源于stack exchange,提问作者ekstroem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 15:05:16