简体   繁体   中英

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.I tried to add geom_text and geom_label but I got this error and I don't understand why it gives this error:

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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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