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
相关产品推荐
相关产品推荐

