2017-08-14 53 views
1

的多邊形,我試圖重新顯示有多少市政債券是你遠離克拉科夫地圖: How many municipals away are you from KrakowR:如何計數兩者之間

和城市克拉科夫改變弗羅茨瓦夫。該地圖在GIMP完成。

我得到了一個shapefile文件(可在這裏找到:http://www.gis-support.pl/downloads/powiaty.zip)。我讀了shape文件文檔包像maptoolsrgdalsf,但我無法找到一個自動功能來算,因爲我不喜歡做手工。

有沒有這樣的功能?

業績:地圖由休伯特Szotek做了https://www.facebook.com/groups/mapawka/permalink/1850973851886654/

回答

4

我不認爲在經歷了網絡分析,所以我必須承認不明白的每一行代碼如下所示。但它的作品!很多材料的改編從這裏:https://cran.r-project.org/web/packages/spdep/vignettes/nb_igraph.html

這是最後的結果:

enter image description here

代碼

# Load packages 
library(raster) # loads shapefile 
library(igraph) # build network 
library(spdep) # builds network 
library(RColorBrewer) # for plot colour palette 
library(ggplot2) # plots results 

# Load Data 
powiaty <- shapefile("powiaty/powiaty") 

首先在poly2nb函數用於計算鄰近地區:

# Find neighbouring areas 
nb_q <- poly2nb(powiaty) 

這就造成我們的空間網格,我們可以在這裏看到:

# Plot original results 
coords <- coordinates(powiaty) 
plot(powiaty) 
plot(nb_q, coords, col="grey", add = TRUE) 

enter image description here

這是我不是100%肯定發生了什麼位。基本上,它是工作了所有的shape文件之間的最短距離在網絡中,並返回這些對的矩陣。

# Sparse matrix 
nb_B <- nb2listw(nb_q, style="B", zero.policy=TRUE) 
B <- as(nb_B, "symmetricMatrix") 

# Calculate shortest distance 
g1 <- graph.adjacency(B, mode="undirected") 
dg1 <- diameter(g1) 
sp_mat <- shortest.paths(g1) 

已經進行的計算,現在可以將數據格式化進入繪圖格式,所以最短路徑矩陣合併與空間數據幀。

我不知道什麼是最好的ID使用,用於參照的數據集,所以我選擇了jpt_kod_je變量。

# Name used to identify data 
referenceCol <- powiaty$jpt_kod_je 

# Rename spatial matrix 
sp_mat2 <- as.data.frame(sp_mat) 
sp_mat2$id <- rownames([email protected]) 
names(sp_mat2) <- paste0("Ref", referenceCol) 

# Add distance to shapefile data 
[email protected] <- cbind([email protected], sp_mat2) 
[email protected]$id <- rownames([email protected]) 

數據現在以適當的格式顯示。使用基本功能spplot我們可以得到一個圖形相當迅速:

displaylayer <- "Ref1261" # id for Krakow 

# Plot the results as a basic spplot 
spplot(powiaty, displaylayer) 

我喜歡ggplot密謀更復雜的圖表,你可以控制的造型更容易。然而,它是一個比較挑剔的數據如何給進,所以我們需要重新爲它的數據,我們建立圖前:

# Or if you want to do it in ggplot 

filtered <- data.frame(id = sp_mat2[,ncol(sp_mat2)], dist = sp_mat2[[displaylayer]]) 
ggplot_powiaty$dist == 0 

ggplot_powiaty <- powiaty %>% fortify() 
ggplot_powiaty <- merge(x = ggplot_powiaty, y = filtered, by = "id") 
names(ggplot_powiaty) 

和劇情。我已經通過刪除不需要的元素並添加了背景來定製了它。此外,爲了使搜索區域位於中心黑色,我使用ggplot_powiaty[ggplot_powiaty$dist == 0, ]對數據進行子集分類,然後將其作爲另一個多邊形進行繪製。

ggplot(ggplot_powiaty, aes(x = long, y = lat, group = group, fill = dist)) + 
    geom_polygon(colour = "black") + 
    geom_polygon(data =ggplot_powiaty[ggplot_powiaty$dist == 0, ], 
       fill = "grey60") + 
    labs(title = "Distance of Counties from Krakow", caption = "Mikey Harper") + 
    scale_fill_gradient2(low = "#d73027", mid = "#fee08b", high = "#1a9850", midpoint = 10) + 
    theme(
    axis.line = element_blank(), 
    axis.text.x = element_blank(), 
    axis.text.y = element_blank(), 
    axis.ticks = element_blank(), 
    axis.title.x = element_blank(), 
    axis.title.y = element_blank(), 
    panel.grid.minor = element_blank(), 
    panel.grid.major = element_blank(), 
    plot.background = element_rect(fill = "#f5f5f2", color = NA), 
    panel.background = element_rect(fill = "#f5f5f2", color = NA), 
    legend.background = element_rect(fill = "#f5f5f2", color = NA), 
    panel.border = element_blank()) 

enter image description here

要繪製的弗羅茨瓦夫如在文章頂部顯示,只要改變displaylayer <- "Ref0264"和更新的稱號。

+0

哇!那是超級工作!我會分析它,並希望瞭解! –

相關問題