绘制地理编码:ggmap错误

时间:2014-03-17 14:45:45

标签: r maps ggmap

我想用R和ggmap包可视化数据框。

DF:

| lon       | lat       |
|-----------|-----------|
| 6.585863  | 51.09021  |
| 8.682.127 | 50.11092  |
| 7.460.367 | 5.152.755 |

我用

创建了一张地图
mapImageData <- get_googlemap(
+   "Germany", 
+   zoom=15
+ )

然后想添加地理编码:

ggmap(mapImageData) +
+     geom_point(aes(x=lon, y=lat), data=df, colour="red", size=5)

但我收到错误: 错误:geom_point需要以下缺失的美学:x,y

我做错了什么?

1 个答案:

答案 0 :(得分:6)

你有三个问题:

  1. 某些价值观中的不止一个关键点小数点可能不在正确的位置(另见我的评论)
  2. 地图以错误的位置为中心
  3. 缩放级别为高
  4. 让我们解决这个问题:

    # Get the right data
    ger <- read.table(text="lon lat
    6.585863 51.09021
    8.682127 50.11092
    7.460367 51.52755", header = TRUE, strip.white = TRUE)
    
    # Finding a good centerpoint
    mean(ger$lon) # outcome: 7.576119
    mean(ger$lat) # outcome: 50.90956
    
    # Get the map; you might have to try several zoomlevels te get the right one
    library(ggmap)
    mapImageData <- get_googlemap(center = c(lon = 7.576119, lat = 50.90956), zoom=8)
    
    # Plot the points on the map
    ggmap(mapImageData) +
      geom_point(data=ger, aes(x=lon, y=lat), colour="red", size=6, alpha=.6)
    

    生成的地图:

    enter image description here