【问题标题】:Adding labels to a map made with ggplot() and geom_polygon ()向使用 ggplot() 和 geom_polygon () 制作的地图添加标签
【发布时间】:2022-12-12 13:39:46
【问题描述】:

我用 ggplot 和 geom_polygon 做了一张地图,但是我无法识别州的名称所以我想为每个州添加一个标签(使其交互)以便在我将鼠标悬停在州区域上时获得州的名称。我尝试添加 geom_text 和 geom_label 但出现此错误,我不明白为什么会出现此错误:

Error in geom_point(): ! mapping must be created by aes()

Here is the variables of my data set

我的代码:

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_point(pop_usa, aes(x=long, y=lat, group=group, size=values)) +
    geom_text(data = pop_usa, aes(x=long, y=lat, group=group, label=state), size = 3, hjust=0, vjust=-1) +
    coord_map() 

有人可以帮我吗?

【问题讨论】:

  • geom_point中第一个参数是mapping,但是你传了pop_usa,好像认为第一个参数是data。该层应从主 ggplot 调用继承数据,因此只需从 geom_point 调用中删除 pop_usa

标签: r ggplot2 label mapping aes


【解决方案1】:

更新:OP 请求见 cmets:

我们可以这样:

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_point(aes(x=long, y=lat)) +
  geom_text(label=unique(pop_usa$state), 
            x=pop_usa$long[10],
            y=pop_usa$lat[8]
            , size = 3, hjust=0, vjust=-1) +
  coord_map()

第一个答案:

按照 cmets 中@Allan Cameron 提供的说明进行操作,然后试试这个:

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_point(aes(x=long, y=lat)) +
  geom_text(aes(label = state), size = 3, hjust=0, vjust=-1) +
  coord_map() 

数据:

df <- structure(list(state = c("alabama", "alabama", "alabama", "alabama", 
"alabama", "alabama", "alabama", "alabama", "alabama", "alabama", 
"alabama", "alabama", "alabama", "alabama", "alabama", "alabama", 
"alabama"), estimated_pop_2020 = c(4.823357, 4.823357, 4.823357, 
4.823357, 4.823357, 4.823357, 4.823357, 4.823357, 4.823357, 4.823357, 
4.823357, 4.823357, 4.823357, 4.823357, 4.823357, 4.823357, 4.823357
), long = c(-87.46201, -87.48493, -87.52503, -87.53076, -87.57087, 
-87.58806, -87.59379, -87.59379, -87.674, -87.81152, -87.88026, 
-87.92037, -87.95475, -88.00632, -88.01778, -88.01205, -87.99486
), lat = c(30.38968, 30.37249, 30.37249, 30.33239, 30.32665, 
30.32665, 30.30947, 30.28655, 30.27509, 30.2579, 30.24644, 30.24644, 
30.24644, 30.24071, 30.25217, 30.26936, 30.27509), group = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), order = 1:17), class = "data.frame", row.names = c("1", "2", 
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", 
"15", "16", "17"))

【讨论】:

  • 我怎样才能把名字放在中间一次而不是在边界上多次?非常感谢
  • 请看我的更新!
猜你喜欢
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
  • 2012-09-19
  • 2015-03-28
  • 2020-12-18
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多