简体   繁体   English

将标签添加到使用 ggplot() 和 geom_polygon() 制作的 map

[英]Adding labels to a map made with ggplot() and geom_polygon ()

I did a map with ggplot and geom_polygon, however I can not recognize the names of the states so I wanted to add a label to every state ( make it interactive) to have the name of the state when i hover over the state area.我用ggplot和geom_polygon做了一个map,但是我无法识别状态的名称所以我想在每个state中添加一个label(使其交互)当我884208506836141438782.7上的884208506836141483688时具有state的名称I tried to add geom_text and geom_label but I got this error and I don't understand why it gives this error:我尝试添加 geom_text 和 geom_label 但出现此错误,我不明白为什么会出现此错误:

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

Here is the variables of my data set这是我的数据集的变量

My code:我的代码:

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() 

Can Anyone help me please?有人可以帮我吗?

Update: OP request see comments:更新:OP请求见评论:

We could something like this:我们可以这样:

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()

在此处输入图像描述

First answer:第一个答案:

Follow the instructions provided by @Allan Cameron in the comments, then try this:按照@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() 

在此处输入图像描述

data:数据:

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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM