我用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.
1条答案
按热度按时间fjaof16o1#
考虑到错误
第一个月
尝试,而不是
geom_polygon
:并将几何图形提供给geom_sf_text,如下所示:
注意:除非空间数据框
pop_usa
的几何列与默认的“几何”命名不同