绘制地理编码:ggmap错误

4
我想用R和ggmap包可视化一个数据框。
数据框为:
| 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 我做错了什么?

不知道这是否与您的问题有关,但您需要检查您的经度和纬度(其中三个中有两个“。”)。 - Henrik
@Henrik @user2874571 可能在数据转换过程中出现了问题。应该只有一个小数点。此外,最后一个 lat 的小数点可能需要向右移动一位。 - Jaap
1个回答

7

你有三个问题:

  1. 一些值中存在多个小数点,且小数点可能位置不正确(请参见我的评论)
  2. 地图中心位置不正确
  3. 缩放级别过高

让我们来解决这些问题:

# 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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接