2014-02-20 37 views
0

我獲得了許多不同的GPS跟蹤,並試圖使用ggmap將所有路徑繪製到一個地圖上。我正在嘗試生成類似Flowing Data所做的工作,其中該作者使用了plotkml而不是ggmap(我更喜歡)。不幸的是,在添加了幾條路徑之後,顯然,由於內存過多,我的R腳本停止工作。有人可以幫忙嗎?ggmap在繪製多條路徑時緩慢運行

我在MacOS上使用R版本3.0.2。

這是一個功能齊全的腳本。我已經手動輸入三個示例數據幀,其中一幀來自GPS座標的一個跟蹤文件。實際上,我已經超過了這樣的數據幀。

library(ggmap) 

printf <- function(...) cat(sprintf(...)) 

# --------------------------------------------------------- 
# Create three example data frames 
df1 <- structure(list(latitude = c(37.789165, 37.827332, 37.851501), 
    longitude = c(-122.411667, -122.210167, -122.265831)), .Names = c("latitude", 
    "longitude"), class = "data.frame", row.names = c(4634L, 5415L, 
    30777L)) 

df2 <- structure(list(latitude = c(37.331951, 37.332291), longitude = c(-122.029579, 
-122.028625)), .Names = c("latitude", "longitude"), class = "data.frame", row.names = c(18781L, 
18787L)) 

df3 <- structure(list(latitude = c(37.789333, 37.780834, 37.622833, 
37.7775), longitude = c(-122.408669, -122.423668, -122.330666, 
-122.43383)), .Names = c("latitude", "longitude"), class = "data.frame", row.names = c(44107L, 
44182L, 44237L, 44277L)) 

allDataFrames <- list(df1, df2, df3) 
numDataFrames <- length(allDataFrames) 
# --------------------------------------------------------- 


# Get the background map 
map <-get_googlemap(center=c(lon=-122.237, lat=37.753),maptype="roadmap", color="bw", zoom=9) 
p <- ggmap(map) 

dfCounter <- 0 

# Loop over all the data frames. 
for (df in allDataFrames) 
{ 
    # Plot this data frame as a path. 
    p <- p + geom_path(aes(x=longitude, y=latitude), data=df, colour="#ff0000", size=0.5) 
    printf("%d/%d\n", dfCounter, numDataFrames) 
    dfCounter <- dfCounter+1 
} 

print(p) 

現在,循環完成第300次迭代的時間,劇本研與沒有進展陷入停頓。我懷疑這是行:

p <- p + geom_path(aes(x=longitude, y=latitude), data=df, colour="#ff0000", size=0.5) 

,但我不知道如何在改善。我需要達到4000個數據幀,並且它已經停止在300.

我的MacOS上的「top」輸出顯示RSIZE(物理內存)和VSIZE(虛擬內存)都沒有問題。問題是什麼?

行書之前:

PID COMMAND  %CPU TIME  #TH #WQ #POR #MREG RPRVT RSHRD RSIZE VPRVT VSIZE PGRP PPID STATE UID FAULTS 
42708 R   0.0 00:00.18 1  0 21 126 30M 244K 34M 47M 2440M 42708 448 sleeping 501 9464 

在運行腳本,圍繞迭代150:

PID COMMAND  %CPU TIME  #TH #WQ #POR #MREG RPRVT RSHRD RSIZE VPRVT VSIZE PGRP PPID STATE UID FAULTS 
42708 R   100.1 00:45.87 1/1 0 22 544+ 2002M+ 244K 2018M+ 2034M+ 4429M+ 42708 448 running 501 773737+ 
+0

爲什麼所有的數據幀?嘗試使用分組變量合併到一個數據框中並放棄循環。 – Gregor

+0

我不認爲問題是數據大小。我認爲這是p + geom_path()語句。 – stackoverflowuser2010

+0

我認爲問題的根源在於內存,如果整合數據框有幫助,我不會感到驚訝。 – Gregor

回答

0

創建一個單獨的數據幀,就像是在你鏈接到上面的數據流進行現場。該數據框應該有三列:索引,經度和緯度。經緯度不言自明。該索引爲每個路徑採用唯一值。再次,這已經在上面的Flowing Data站點的代碼中完成。其餘:

#experiment with zoom and other ggmap options as you see fit. Random lat/long chosen here 

baseMap <- get_googlemap(center=c(lon = -80, lat = 38), maptype="roadmap", color="bw", zoom=11) 

map <- ggmap(baseMap) + 
geom_path(aes(x=longitude, y=latitude, group=index), data=routes, alpha=0.2, color="red") 

map