我正在使用光栅包中的getData函数来检索阿根廷地图。我希望使用ggplot2绘制结果地图,因此我使用broom包中的tidy函数将其转换为数据帧。这很好用,但是我不知道如何保存联邦地区的名称,以便在地图上使用它们。
以下是我的原始代码,它没有保留地区名称:
# Original code: ##################################
# get the map data from GADM.org and then simplify it
arg_map_1 <- raster::getData(country = "ARG", level = 1, path = "./data/") %>%
# simplify
rmapshaper::ms_simplify(keep = 0.01) %>%
# tidy to a dataframe
broom::tidy()
# plot the map
library(ggplot2)
ggplot(data=arg_map_1) +
geom_map(map=arg_map_1, aes(x=long, y=lat, map_id=id, fill=id),
color="#000000", size=0.25)下面是从SPDF中提取地区名称并将其用作地图ID的代码:
# Code with a hack to keep the district names: ################################
# get the map data from GADM.org and then simplify it
arg_map_1 <- raster::getData(country = "ARG", level = 1, path = "./data/") %>%
# simplify
rmapshaper::ms_simplify(keep = 0.01)
for(region_looper in seq_along(arg_map_1@data$NAME_1)){
arg_map_1@polygons[[region_looper]]@ID <-
as.character(arg_map_1@data$NAME_1[region_looper])
}
# tidy to a dataframe
arg_map_1 <- arg_map_1 %>%
broom::tidy()
library(ggplot2)
ggplot(data=arg_map_1) +
geom_map(map=arg_map_1, aes(x=long, y=lat, map_id=id, fill=id),
color="#000000", size=0.25)我一直在想,一定有某种方法可以使用保存名称的整洁函数,但我终究还是弄不明白。
发布于 2017-05-15 20:25:55
您可以使用plyr包中的join函数。下面是一个通用的解决方案(它看起来很长,但实际上非常简单):
shapefile:假设您的工作目录中有一个shapefile my_shapefile.shp。让我们加载它:
shape <- readOGR(dsn = "/my_working_directory",layer = "my_shapefile")
请注意,在这个shapefile中有一个数据帧,可以用shape@data访问它。例如,此数据框可能如下所示:
head(shape@data)代码区域标签0 E12000006东英格兰E12000006 1 E12000007伦敦E12000007 2 E12000002西北E12000002 3 E12000001东北E12000001 4 E12000004东米德兰兹E12000004 5 E12000003约克郡和亨伯郡
来自shapefile 的
broom包来整理shapefile数据帧:整齐<- new_df (Shape)
结果如下所示:
> head(new_df)
long lat order hole piece group id
1 547491.0 193549.0 1 FALSE 1 0.1 0
2 547472.1 193465.5 2 FALSE 1 0.1 0
3 547458.6 193458.2 3 FALSE 1 0.1 0
4 547455.6 193456.7 4 FALSE 1 0.1 0
5 547451.2 193454.3 5 FALSE 1 0.1 0
6 547447.5 193451.4 6 FALSE 1 0.1 0不幸的是,tidy()丢失了变量名(在本例中为“region”)。相反,我们得到了一个新的变量"id",从0开始。幸运的是,"id“的顺序与存储在shape@data$region中的顺序相同。让我们使用它来恢复名称。
使用行名的
tidy()创建的变量相同:Recover row name temp_df <- data.frame(temp_df@data$region) names(temp_df) <- c("region") # Create and append "id”temp_df$id <- seq(0,使用“id”使用新的数据帧恢复行名:最后,让我们将名称放回新的数据帧中:join <- new_df (new_df,temp_df,by="id")
就这样!您甚至可以使用join命令和"id“索引将更多变量添加到新的数据帧中。最终结果将类似于:
> head(new_df)
long lat order hole piece group id name var1 var2
1 547491.0 193549.0 1 FALSE 1 0.1 0 East of England 0.525 0.333
2 547472.1 193465.5 2 FALSE 1 0.1 0 East of England 0.525 0.333
3 547458.6 193458.2 3 FALSE 1 0.1 0 East of England 0.525 0.333
4 547455.6 193456.7 4 FALSE 1 0.1 0 East of England 0.525 0.333
5 547451.2 193454.3 5 FALSE 1 0.1 0 East of England 0.525 0.333
6 547447.5 193451.4 6 FALSE 1 0.1 0 East of England 0.525 0.333 发布于 2016-11-14 03:07:26
阿利斯泰尔的评论促使我继续推动region=参数。我尝试了许多次迭代,在这个线程https://github.com/tidyverse/ggplot2/issues/1447中找到了一些想法。
下面是抓取地区名称的代码:
# load the magrittr library to get the pipe
library(magrittr)
# load the maptools library to get the rgeos object
library(maptools)
arg_map_1 <- raster::getData(country = "ARG", level = 1, path = "./data/") %>%
# simplify
rmapshaper::ms_simplify(keep = 0.01) %>%
# tidy to a dataframe
broom::tidy(region="NAME_1")
# plot the map
library(ggplot2)
ggplot(data=arg_map_1) +
geom_map(map=arg_map_1, aes(x=long, y=lat, map_id=id, fill=id),
color="#000000", size=0.25)首先,请注意,必须加载maptools库才能正确执行整洁操作。另外,我想强调的是,从中提取区域信息的变量必须用引号括起来。我一直错误地假设,broom可以识别变量名,就像其他tidyverse包可以识别未加引号或用反引号括起来的列名一样。
https://stackoverflow.com/questions/40576457
复制相似问题