使用ggplot2在世界地图上标记点?

时间:2017-06-20 01:32:29

标签: r ggplot2

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

Map

我试图通过在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错误,但无法弄清楚如何。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分: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