将Google地理编码输出转换为R中的地图

时间:2012-12-10 05:10:56

标签: r maps ggplot2 geocode

我担心做一些相对容易的事情会有些麻烦。我正在尝试使用Google地理编码API的输出有效地复制此问题World Map - Plotting Circles, size of circle relevant to No of中的ggplot地图。我传递了一堆有问题格式的自由文本位置数据(即它擅长的),并得到了下表。

> summary(userlocations)
       ID              lon                lat             lonmin        
 Min.   :   1.0   Min.   :-169.867   Min.   :-82.86   Min.   :-180.000  
 1st Qu.: 618.8   1st Qu.: -91.815   1st Qu.: 33.72   1st Qu.: -91.956  
 Median :1200.0   Median : -77.201   Median : 40.06   Median : -78.110  
 Mean   :1220.5   Mean   : -49.884   Mean   : 35.40   Mean   : -51.709  
 3rd Qu.:1804.2   3rd Qu.:  -2.248   3rd Qu.: 45.52   3rd Qu.:  -2.798  
 Max.   :2500.0   Max.   : 174.886   Max.   : 71.29   Max.   : 174.771  
                  NA's   :643        NA's   :643      NA's   :643       
     lonmax             latmin           latmax            location   
 Min.   :-169.774   Min.   :-90.00   Min.   :-61.00   London   :  65  
 1st Qu.: -89.013   1st Qu.: 33.07   1st Qu.: 33.95   Canada   :  31  
 Median : -75.891   Median : 39.74   Median : 40.59   Atlanta  :  25  
 Mean   : -47.521   Mean   : 34.01   Mean   : 36.86   England  :  22  
 3rd Qu.:  -1.521   3rd Qu.: 44.37   3rd Qu.: 47.08   New York :  22  
 Max.   : 180.000   Max.   : 71.23   Max.   : 85.41   Las Vegas:  21  
 NA's   :643        NA's   :643      NA's   :643      (Other)  :3286

虽然我知道可以做的事情如下:

map(database="worldHires")
points(userlocations$lon, userlocations$lat, pch=20)

产生一个相当小,丑陋的地图。为了重现ggplot示例,我需要(至少)获得重复lat / lon entires的计数器,所以我知道何时有例如在伦敦有65人,但似乎我不能只使用userlocations $ location,因为这个字段只包含我首先传递给Google的非规范化自由文本;只有lon和lat似乎有有用的数据。

我为这个问题的简单性道歉,但如果有人能提供帮助,我真的很感激。

1 个答案:

答案 0 :(得分:0)

如果你可以将代码整齐地格式化为两个向量x(经度)和y(latitute),那么你可以使用:

require(ggplot2)
ggplot(data.frame(x = x, y = y, circle_size = '\\add a vector'), aes(x, y, size = circle_size) + geom_point()

我没有测试上面的函数,但它应该为你提供的例子产生一个非常相似的图。