R语言 使用ggplot向Map添加标注

tpgth1q7  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(136)

我用ggplot()做了一个Map,我想给Map添加标签,或者是一个交互式的东西,这样当我悬停在州区域上时,它会给我它的名字。我在Stackoverflow中寻找提示,但是它不起作用。
你有什么建议吗?
How my data looks like

scaling_map <-ggplot(pop_usa, aes(long,lat)) + 
geom_polygon(aes(group= group, fill = estimated_pop_2020) ,color="black") +
theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), axis.title.y=element_blank(), 
axis.text.y=element_blank(), axis.ticks.y=element_blank(),plot.title = element_text(face = "bold",hjust = 0.5)) +
  ggtitle("Estimated population by state") +
  scale_fill_gradient(name ="Estimated population (log10)" ,low = "#FFFFCC" , high = "#336600") +
  geom_sf_text(aes(label = state))
  coord_map()

下面是我运行时得到的消息:

Error in `geom_sf_text()`:
! Problem while computing stat.
ℹ Error occurred in the 2nd layer.
Caused by error in `compute_layer()`:
! `stat_sf_coordinates()` requires the following missing aesthetics: geometry
Run `rlang::last_error()` to see where the error occurred.
fjaof16o

fjaof16o1#

考虑到错误
第一个月
尝试,而不是geom_polygon

geom_sf(aes(group = group, fill = estimated_pop_2020, geometry = geometry) ,color="black")

并将几何图形提供给geom_sf_text,如下所示:

geom_sf_text(aes(label = state, geometry = geometry)) ## see note

注意:除非空间数据框pop_usa的几何列与默认的“几何”命名不同

相关问题