嘗試:
unzip("ZMB_adm.zip", exdir="ZBM_adm")
zmb <- readOGR(dsn = "C:/R_Progs/ZBM_adm", layer="ZMB_ADM0")
## OGR data source with driver: ESRI Shapefile
## Source: "C:/R_Progs/ZBM_adm", layer: "ZMB_ADM0"
## with 1 features and 2 fields
## Feature type: wkbPolygon with 2 dimensions
的extdir
參數將保持整潔shape文件在自己的目錄中。
的原因ZMB_ADM0
是,有多種形狀文件:
ZMB_ADM0.SHP
ZMB_ADM1.SHP
ZMB_ADM2.SHP
各
自己的層:
INFO: Open of `ZMB_ADM0.SHP'
using driver `ESRI Shapefile' successful.
1: ZMB_ADM0 (Polygon)
INFO: Open of `ZMB_ADM1.SHP'
using driver `ESRI Shapefile' successful.
1: ZMB_ADM1 (Polygon)
INFO: Open of `ZMB_ADM2.SHP'
using driver `ESRI Shapefile' successful.
1: ZMB_ADM2 (Polygon)
你使用哪一個取決於你需要什麼級別的行政區域界線:
library(ggplot2)
library(grid)
map0 <- readOGR(dsn = "C:/R_Progs/ZBM_adm", layer="ZMB_ADM0")
map1 <- readOGR(dsn = "C:/R_Progs/ZBM_adm", layer="ZMB_ADM1")
map2 <- readOGR(dsn = "C:/R_Progs/ZBM_adm", layer="ZMB_ADM2")
[email protected]$id <- rownames([email protected])
[email protected]$id <- rownames([email protected])
[email protected]$id <- rownames([email protected])
adm_labs0 <- data.frame(id=rownames([email protected]), [email protected]$ADM0)
adm_labs1 <- data.frame(id=rownames([email protected]), [email protected]$ADM1)
adm_labs2 <- data.frame(id=rownames([email protected]), [email protected]$ADM2)
map_0 <- fortify(map0)
map_1 <- fortify(map1)
map_2 <- fortify(map2)
map_0 <- merge(map_0, adm_labs0)
map_1 <- merge(map_1, adm_labs1)
map_2 <- merge(map_2, adm_labs2)
map0 <- readOGR(dsn = "C:/R_Progs/ZBM_adm", layer="ZMB_ADM0")
map1 <- readOGR(dsn = "C:/R_Progs/ZBM_adm", layer="ZMB_ADM1")
map2 <- readOGR(dsn = "C:/R_Progs/ZBM_adm", layer="ZMB_ADM2")
[email protected]$id <- rownames([email protected])
[email protected]$id <- rownames([email protected])
[email protected]$id <- rownames([email protected])
adm_labs0 <- data.frame(id=rownames([email protected]), [email protected]$ADM0)
adm_labs1 <- data.frame(id=rownames([email protected]), [email protected]$ADM1)
adm_labs2 <- data.frame(id=rownames([email protected]), [email protected]$ADM2)
map_0 <- fortify(map0)
map_1 <- fortify(map1)
map_2 <- fortify(map2)
map_0 <- merge(map_0, adm_labs0)
map_1 <- merge(map_1, adm_labs1)
map_2 <- merge(map_2, adm_labs2)
g0 <- ggplot(map_0, aes(x=long, y=lat, group=group)) +
geom_polygon(color = "grey50", aes(fill=adm0), size=0.125)+
labs(x="",y="")+ theme_bw() + labs(title="ADM0") +
coord_fixed() + theme(legend.position="none")
g1 <- ggplot(map_1, aes(x=long, y=lat, group=group)) +
geom_polygon(color = "grey50", aes(fill=adm1), size=0.125)+
labs(x="",y="")+ theme_bw() + labs(title="ADM1") +
coord_fixed() + theme(legend.position="none")
g2 <- ggplot(map_2, aes(x=long, y=lat, group=group)) +
geom_polygon(color = "grey50", aes(fill=adm2), size=0.125)+
labs(x="",y="")+ theme_bw() + labs(title="ADM2") +
coord_fixed() + theme(legend.position="none")
grid.arrange(g0, g1, g2, ncol=3)
你真的做得比我想的要多。非常感謝,雖然我有我自己的另一個問題,那是我有多個版本的R,所以我得到的錯誤:「錯誤:ggplot2不知道如何處理類函數的數據」 – 2014-09-19 06:47:18
只是一個請求@hrbrmstr,當我上面運行你的代碼時,我得到:「$ < - 。data.frame'('* tmp *',」id「,value = c(」0「,」1「 ,「2」,「3」,::替換有72行,數據有1個。我不知道你是否可以花你的寶貴的建議我在哪裏可能會出現問題,否則創建和填充文件夾os shapefile。 – 2014-09-19 11:06:15