簡體   English   中英

根據到R中質心的距離設置點的大小

[英]Set size of points based on distance to centroid in R

我對 R 和空間分析比較陌生。 我想創建一個地圖,顯示與多邊形(縣)質心周圍 30 公里緩沖區相交的所有點(在這種情況下對應於監測站的坐標),並將每個點的大小設置為取決於點到質心的距離。

我能夠生成地圖(報告如下),顯示所有相同大小的點,顏色編碼取決於它們是否與緩沖區相交:

### Compute counties' centroids
counties_trans$centroid <- st_centroid(counties_trans$geometry)

#------------------------------------------------------------------------------#

### 30km centroids' buffers
counties_trans$buff <- st_buffer(counties_trans$centroid, 30000)

#------------------------------------------------------------------------------#

### Focus on specific county: Dresden: SN_L = 14, RS = 14612.

#Define bounding box of state
box = c(xmin = 703301, ymin = 5562792, xmax = 921778, ymax = 5733351)
saxony <- st_crop(counties_trans, box)

#Stations within 30km from Dresden's centroid
a <- st_intersection(saxony$buff[which(saxony$RS == "14612")], poll_st_trans$geometry)

#Stations outside 30km radius from Dresden's centroid
c <- st_difference(poll_st_trans$geometry, saxony$buff[which(saxony$RS == "14612")])

#Stations outside 30km radius from Dresden's centroid an in same State
d <- st_intersection(c, saxony$geometry[which(saxony$SN_L == "14")])

#------------------------------------------------------------------------------#

###Produce map

#Counties' borders
plot(saxony$geometry[which(saxony$SN_L == "14")], reset = FALSE)

#Dresden's centroid
plot(saxony$centroid[which(saxony$RS == "14612")], add = TRUE, pch = 20)

#30km radius
plot(saxony$buff[which(saxony$RS == "14612")], add = TRUE)

#Stations in the State outside 30km radius
plot(d, add = TRUE, pch = 24, col = 'grey')

#Stations within 30km radius
plot(a, add = TRUE, pch = 17, col = 'royalblue3', cex = dist)

#Other states
plot(saxony$geometry[which(saxony$SN_L != "14")], add = TRUE, col = 'grey')

我計算了車站(僅與 30 公里緩沖區相交的車站)與縣質心之間的距離:

dist <- st_distance(a, saxony$centroid[which(saxony$RS == "14612")])

我缺少的部分是如何根據距離設置與 30 公里半徑內的站點相對應的點的大小(好吧,實際上,由於靠近質心的站點應該更大,所以它是反距離,但這很容易計算)。 我是否嘗試

plot(a, add = TRUE, pch = 17, col = 'royalblue3', cex = dist)

或者

ggplot(data = saxony) +
     geom_sf() +
     geom_sf(data = a, colour = "royalblue3", size = dist)

我總是得到一個藍色矩形。

有人知道我該怎么做嗎? 提前致謝。

上面代碼生成的地圖

有幾個建議,st_distance 返回一個以空間參考為單位的值,因此請嘗試在任何繪圖之前將其轉換為數字。 走 ggplot 路線時,您需要將 size 變量放入 aes() 調用中,並指定 inherit.aes=FALSE 例如:

ggplot(saxony)+
geom_sf()+
geom_sf(data=a, aes(size=dist), colour="royalblue3", inherit.aes=FALSE)

如果您要堆疊多個調用填充或顏色的幾何圖形,您可能還需要使用 ggnewscale 包

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM