使用ggplot绘制非洲物种分布图时遇警告问题求助
问题解决:ggplot绘制非洲物种分布图的警告处理
警告原因
geom_map() 语法不需要指定 x 和 y 美学参数,你在 aes() 中传入这两个参数才触发了"Ignoring unknown aesthetics: x and y"警告。geom_map() 是通过 map_id 来匹配地图数据与你的物种分布数据的,和 geom_polygon 的用法有本质区别。
修正步骤与示例代码
1. 确保地图数据格式正确
如果用 rnaturalearth 获取非洲地图:
library(ggplot2) library(rnaturalearth) # 获取非洲国家地图数据(sf格式) africa <- ne_countries(continent = "africa", returnclass = "sf")
如果用 maps 包:
library(maps) # 获取长格式非洲地图数据 africa <- map_data("world", region = "Africa")
2. 正确调用geom_map绘制底图
假设你的物种分布数据为 bulbostylis_data,包含经纬度列(如 longitude/latitude)和匹配地图区域的列(如 country 或 region):
ggplot() + # 绘制非洲地图底图:用map_id关联地图数据的区域标识 geom_map( data = africa, map = africa, aes(map_id = name), # sf格式用name列,maps包生成的用region列 fill = "lightgray", color = "white" ) + # 自动适配地图范围 expand_limits(x = africa$long, y = africa$lat) + # 添加物种分布点(这里才需要x/y美学) geom_point( data = bulbostylis_data, aes(x = longitude, y = latitude), color = "darkred", size = 1.5 ) + # 启用地理坐标系 coord_map() + labs(x = "经度", y = "纬度", title = "非洲Bulbostylis属物种分布")
关键注意点
geom_map()仅需map_id来匹配区域,不要在它的aes里加x和y;- 物种分布点用
geom_point()绘制,这里才需要指定x(经度)和y(纬度); - 确保
map_id对应的列在地图数据和物种数据中是匹配的(比如都是国家名称)。
内容的提问来源于stack exchange,提问作者Siromenthe
相关产品推荐
相关产品推荐

