2017-06-03 118 views
0

我尝试使用地图中的点显示地图中的国家/地区数据。这里的数据框:地图中的映射点

> dput(countries) 
structure(list(country = structure(c(5L, 6L, 3L, 4L, 10L, 8L, 
11L, 7L, 1L, 13L, 9L, 12L, 2L), .Label = c("Australia", "China", 
"France", "Georgia", "India", "Ireland", "Malaysia", "Poland", 
"Qatar", "Singapore", "South Africa", "Spain", "USA"), class = "factor"), 
    Latitude = c(20.593684, 53.142367, 46.227638, 32.165622, 
    1.352083, 51.919438, -30.559482, 4.210484, -25.274398, 37.09024, 
    25.354826, 40.463667, 35.86166), Longitude = c(78.96288, 
    -7.692054, 2.213749, -82.900075, 103.819836, 19.145136, 22.937506, 
    101.975766, 133.775136, -95.712891, 51.183884, -3.74922, 
    104.195397), Value = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 1L, 2L, 2L)), .Names = c("country", "Latitude", "Longitude", 
"Value"), class = "data.frame", row.names = c(NA, -13L)) 

从这里代码:

library(maps) 
library(ggplot2) 
base_world <- map_data("world") 
map_data_coloured <- 
    base_world + 
    geom_point(data=countries, 
       aes(x=Longitude, y=Latitude, colour=Value), size=5, alpha=I(0.7)) 

但我收到此错误:

Error in as.vector(x, mode) : 
    cannot coerce type 'environment' to vector of type 'any' 

回答

2

你需要通过geom_polygon参数映射您base_world对象

ggplot() + 
    geom_polygon(data=base_world, aes(x=long, y=lat, group=group)) + 
    geom_point(data=countries, aes(x=Longitude, y=Latitude, colour=Value), size=5, alpha=I(0.7)) 

enter image description here