R geom_point和ggmap,位置

时间:2014-11-30 03:14:15

标签: r ggmap

我的目标是制作地图并在地图上放置位置。它与R geom_point and ggmap类似,但我可以获得地图,但是很难获得积分。

这是我正在使用的代码。

require(ggmap)
library(ggmap)
pp<- data.frame(lat=c(48.8535, 48.85418333, 48.86253333, 48.86373333, 48.86698333, 48.87033333,                 48.87281667, 48.86993333, 48.87718333, 48.86251667, 
48.85313333, 48.89153333, 48.88231667, 48.84368333, 48.84221667, 48.84473333, 48.8335,   48.86961667, >48.84681667), lon=c(-122.5735333, -122.54085, -122.5142667,
-122.4969667, -122.4857333, -122.4646, -122.44245, -122.4372167, -122.4128167, -122.4298,     -122.48205, -122.40875, -122.4423833, -122.55515, -122.5196667, -122.52105
, -122.5086333, -122.4067667, -122.4358833))
tenmile <- get_map(location = c(lon = -122.486328, lat = 48.862813),
    color = "color",
    source = "google",
    maptype = "roadmap",
    zoom = 12)
ggmap(tenmile,
extent = "device",
ylab = "Latitude",
xlab = "Longitude")
p<- ggmap(tenmile) + geom_point(data=pp, aes(x=lon, y=lat), color="red", size=30, alpha=0.5)  

1 个答案:

答案 0 :(得分:3)

我认为您唯一的问题是您没有将初始地图分配给对象。试试这个:

# Assign to variable
tenmile.map <- ggmap(tenmile,
      extent = "device",
      ylab = "Latitude",
      xlab = "Longitude")
# Then add the points to the base map you created.
p <- tenmile.map + geom_point(data=pp, aes(x=lon, y=lat),
                              color="red", size=30, alpha=0.5)  
p

如果您希望可以将这些组合起来,但仍需要执行所有地图规范:

all.in.one <- ggmap(tenmile,
      extent = "device",
      ylab = "Latitude",
      xlab = "Longitude") + 
  geom_point(data=pp, aes(x=lon, y=lat),
                          color="red", size=30, alpha=0.5)  
all.in.one

最后一条评论:library()require()之间存在非常小的差异,但它们都加载了包,您只需要使用其中一个。如果您有兴趣,可以参考很多细节in this question