如何在ggmap绘制的核爆炸地图上添加炸弹名称标签?
解决ggmap添加核爆炸点名称标签的问题
你的代码里有两个关键问题导致无法显示炸弹名称标签:
geom_text没有关联数据集df_new_complete,它不知道要从哪里读取名称和坐标信息aes(label="Name")里的引号会让R把"Name"当成固定字符串,而不是调用数据框里的Name列
修正后的代码如下:
worldmap <- get_stamenmap( bbox = c(left = -137.01, bottom = 11.06, right = -32.77, top = 56.64), maptype = "terrain", zoom = 4, messaging = FALSE, urlonly = FALSE, force = FALSE, https = FALSE, ) ggmap(worldmap) + geom_point(data = df_new_complete, aes(x = Longitude, y = Latitude), size = 1.5) + # 关联数据集,用Name列作为标签,同时调整位置避免遮挡点 geom_text(data = df_new_complete, aes(x = Longitude, y = Latitude, label = Name), nudge_x = 0.5, # 横向偏移标签,可根据需求调整 size = 3) + theme_map()
如果标签出现重叠,可以尝试:
- 调整
nudge_x/nudge_y的数值,让标签和点错开 - 使用
geom_text_repel()(需要先安装ggrepel包),它会自动避免标签重叠:
# 先安装包 install.packages("ggrepel") library(ggrepel) ggmap(worldmap) + geom_point(data = df_new_complete, aes(x = Longitude, y = Latitude), size = 1.5) + geom_text_repel(data = df_new_complete, aes(x = Longitude, y = Latitude, label = Name), size = 3) + theme_map()
内容的提问来源于stack exchange,提问作者Jordan Omar Roche
相关产品推荐
相关产品推荐

