ggplot2中世界地图上的标签点[重复]

问题描述 投票:1回答:1

我使用ggplot2库创建了世界地图。

“地图”

我正在尝试通过使用ggplot2中的标签分别标记这两个城市(上海和圣保罗)。但是,当我尝试添加标签时,出现错误消息:

Warning: Ignoring unknown aesthetics: labels
Error: geom_text requires the following missing aesthetics: x, y, label

这里是完整的代码:

require(maps)
require(mapdata)
library(ggplot2)

countries = c("Sao Paulo","Shanghai")

global <- map_data("world")
ggplot() + geom_polygon(data = global, aes(x=long, y = lat, group = 
group)) + 
  coord_fixed(1.3)

ggplot() + 
  geom_polygon(data = global, aes(x=long, y = lat, group = group), 
fill = NA, color = "red") + 
  coord_fixed(1.3)

gg1 <- ggplot() + 
  geom_polygon(data = global, aes(x=long, y = lat, group = group), 
fill = "green", color = "blue") + 
  coord_fixed(1.3)
gg1

labs <- data.frame(
  long = c(-46.625290,121.4580600),
  lat = c(-23.533773,31.2222200),
  stringsAsFactors = FALSE
)  

gg1 + 
  geom_point(data = labs, aes(x = long, y = lat), color = "red", size 
= 5) + ggtitle("World Map") + geom_text(aes(labels=countries),vjust=0, 
colour="red")

很明显,我以某种方式使用ggplot出错,但不知道如何。

r ggplot2
1个回答
0
投票
labs$countries <- countries
gg1 + 
    geom_point(data=labs, aes(long, lat), colour="red", size=5) + 
    ggtitle("World Map") +
    geom_text(data=labs, aes(long, lat, label=countries))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.