2016-11-29 67 views
0

之前已经提出过类似的问题,但用于将一个数据帧的不同列中的数据添加到绘图。 Link to the questionggplot映射:“for”循环仅添加最终映射图层

在我的情况,我想补充新的图层,如果他们在全球环境中存在,我最终的地图有“for”循环。

有了像现在,只有最后一层,点,绘制地图,而不是线层的代码:

rm(list = ls()) 
library(OpenStreetMap) 
library(ggplot2) 
library(sp) 

### The base layer map 
map <- OpenStreetMap::openmap(c(55.8759, -4.2946), c(55.8638, -4.2776), type="osm") 
map <- openproj(map) 
myMap <- ggplot2::autoplot(map) 

### Create some custom map files 
# Some spatial point 
new_shapefile <- data.frame(long = -4.290, lat = c(55.868, 55.872)) 
sp::coordinates(new_shapefile) <- c("long", "lat") 
sp::proj4string(new_shapefile) <- sp::CRS("+init=epsg:4326") 

# Some spatial line 
x <- c(-4.290,-4.285) 
y <- c(55.868,55.868) 
new_line <- SpatialLines(list(Lines(Line(cbind(x,y)), ID="a"))) 
new_line = SpatialLinesDataFrame(new_line, data.frame(Z = c("Road"), row.names = c("a"))) 


addLayer <- function(){ 
    glob <- globalenv() 
    customFiles <- data.frame(ls(pattern = "^(?i)new", envir = glob)) 
    colnames(customFiles) <- "X" 
    customFiles$X <- as.character(customFiles$X) 
    for(i in 1:length(customFiles$X)){ 
    plot <- myMap 
    gg.data <- get(paste(customFiles$X[i])) 
    if(grepl("^SpatialPolygons", class(get(paste(customFiles$X[i])))) == TRUE){ 
     plot <- plot + geom_polygon(data = gg.data, aes(long, lat, group= group)) 
    } 
    if(grepl("^SpatialLines", class(get(paste(customFiles$X[i])))) == TRUE){ 
     plot <- plot + geom_path(data = gg.data, aes(long, lat, group= group)) 
    } 
    if(grepl("^SpatialPoints", class(get(paste(customFiles$X[i])))) == TRUE){ 
     plot <- plot + geom_point(data = data.frame(coordinates(gg.data)), aes(long, lat)) 
    } 
    } 
    print(plot) 
} 


### Run the function 
addLayer() 

enter image description here

既然不能用melt来解决这个问题,正如前面的问题所建议的那样:是否有其他方法来绘制“for”循环中的所有图层?

+0

在循环的每次迭代中,您都以“新鲜”图表开始。您可能想在启动循环之前启动剧情“plot < - myMap”。它看起来像每个if语句中的“gg.data”也需要工作。在points图层中,将它变成一个data.frame,使图层可以正常工作,但是直接在其他图层中使用“gg.data”,并且它看起来不是data.frame。 – aosmith

+0

非常感谢你,在“for”循环之前移动'plot < - myMap'实际上解决了它!我只将点图层转换为data.frame,因为它会给我一个错误:'ggplot2不知道如何处理类SpatialPoints的数据' – Joris

回答

1

您目前正在循环的每次迭代中开始启动您的基本映射,它会替代您在上一次迭代中创建的plot。在开始循环之前启动底图,将所有图层添加到相同的地图。

addLayer <- function(){ 
    glob <- globalenv() 
    customFiles <- data.frame(ls(pattern = "^(?i)new", envir = glob)) 
    colnames(customFiles) <- "X" 
    customFiles$X <- as.character(customFiles$X) 
    plot <- myMap 
    for(i in 1:length(customFiles$X)){ 
    gg.data <- get(paste(customFiles$X[i])) 
    if(grepl("^SpatialPolygons", class(get(paste(customFiles$X[i])))) == TRUE){ 
     plot <- plot + geom_polygon(data = gg.data, aes(long, lat, group= group)) 
    } 
    if(grepl("^SpatialLines", class(get(paste(customFiles$X[i])))) == TRUE){ 
     plot <- plot + geom_path(data = gg.data, aes(long, lat, group= group)) 
    } 
    if(grepl("^SpatialPoints", class(get(paste(customFiles$X[i])))) == TRUE){ 
     plot <- plot + geom_point(data = data.frame(coordinates(gg.data)), aes(long, lat)) 
    } 
    } 
    print(plot) 
}