使用for循环生成多个ggmaps(google)

时间:2017-05-02 10:08:39

标签: r google-maps for-loop ggmap

所以我试图使用for循环输出许多谷歌地图。

奇怪的是,如果我键入(i = 1; i = 2)它可以正常工作,但是如果我使用for循环那就不行了!

## THIS WORKS:

rand.coord <- cbind(rnorm(10, 40, 40),lat=rnorm(10, 40, 40))

i = 1  

df <- data.frame(cbind(lon=rand.coord[i,1],lat=rand.coord[i,2] ))
map <- get_map(location = df, source = "google", zoom = 4, scale = 2, 
       maptype = "satellite")
ggmap(map) 


i = 2 
df <- data.frame(cbind(lon=rand.coord[i,1],lat=rand.coord[i,2] ))
map <- get_map(location = df, source = "google", zoom = 4, scale = 2, 
       maptype = "satellite")
ggmap(map) 

#THIS DOES NOT WORK:

for ( i in 1:10){
    df <- data.frame(cbind(lon=rand.coord[i,1],lat=rand.coord[i,2] ))
    map <- get_map(location = df, source = "google", zoom = 4, scale = 2, 
           maptype = "satellite")
    ggmap(map) 
   }

0 个答案:

没有答案