2017-06-20 85 views
0

我創建了一個使用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錯誤在某種程度上,但找不出如何。任何幫助,將不勝感激。

回答

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