Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 绘制多条路径时,ggmap运行缓慢_R_Ggplot2_Dataframe_Ggmap - Fatal编程技术网

R 绘制多条路径时,ggmap运行缓慢

R 绘制多条路径时,ggmap运行缓慢,r,ggplot2,dataframe,ggmap,R,Ggplot2,Dataframe,Ggmap,我获得了许多不同的GPS轨迹,并尝试使用ggmap将所有路径绘制到一张地图上。我试图制作一些类似于作者所做的工作,作者使用plotkml而不是我更喜欢的ggmap。不幸的是,在添加了几个路径之后,我的R脚本显然由于内存过多而停止运行。有人能帮忙吗 我在MacOS上使用的是R版本3.0.2 这是一个功能完整的脚本。我手动输入了三个示例数据帧,其中一个帧来自GPS坐标的一个跟踪文件。实际上,我有超过4000个这样的数据帧 library(ggmap) printf <- function(.

我获得了许多不同的GPS轨迹,并尝试使用ggmap将所有路径绘制到一张地图上。我试图制作一些类似于作者所做的工作,作者使用plotkml而不是我更喜欢的ggmap。不幸的是,在添加了几个路径之后,我的R脚本显然由于内存过多而停止运行。有人能帮忙吗

我在MacOS上使用的是R版本3.0.2

这是一个功能完整的脚本。我手动输入了三个示例数据帧,其中一个帧来自GPS坐标的一个跟踪文件。实际上,我有超过4000个这样的数据帧

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)
在运行脚本时,大约迭代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+

创建单个数据帧,就像在上面链接到的流动数据站点上所做的一样。该数据框应该有三列:索引、纬度和经度。lat和long是不言自明的。索引为每个路径采用唯一的值。同样,这已经在上面流动数据站点的代码中完成了。其余的:

#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

为什么所有的数据帧?尝试用分组变量组合成一个数据帧,并放弃循环。我认为问题不在于数据大小。我认为这是p+geom_path语句。我认为问题的根源是内存,如果整合数据帧有帮助,我不会感到惊讶。
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+
#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