[英]Select / subset spatial data in R
我正在處理一個包含空間數據(lat / long)的大型數據集。 我的數據集包含了我在分析中不需要的一些位置(它使得在ArcMap中處理的文件很重 - 許多Go數據)。
這就是我想為我的工作分配相關數據的原因。 以下是我的數據示例:
lat long
59.979687 29.706236
60.136177 28.148186
59.331383 22.376234
57.699154 11.667305
54.90566 17.768538
57.122417 8.59762
55.8108 10.820534
57.86072 22.616697
59.912144 30.24504
我的數據來自以下地圖中涵蓋的所有區域。 我想只在波羅的海上工作,所以(差不多)只有這條暗線左邊的點。
有人聽說過一個包或者有關於如何處理這個問題的一些想法嗎?
另一種解決方案可能是在波羅的海周圍繪制一個多邊形,並僅選擇該多邊形內的點。
我假設你的意思是“向右”,因為你說“另一個解決辦法可能是在波羅的海周圍繪制一個多邊形,只選擇這個多邊形內的點”
# your sample data
pts <- read.table(text="lat long
59.979687 29.706236
60.136177 28.148186
59.331383 22.376234
57.699154 11.667305
54.90566 17.768538
57.122417 8.59762
55.8108 10.820534
57.86072 22.616697
59.912144 30.24504", header=TRUE)
library(ggplot2)
library(sp)
library(rgdal)
# a rough baltic polygon that I deliberately made to exclude one point
baltic <- data.frame(lat=c(69.91, 57.98, 51.0, 60, 69.91),
long=c(23.07, 7.05, 12.0, 30, 23.07))
# only doing this to show the points, you can filter out in similar fashion
pts$is_in <- factor(point.in.polygon(pts$long, pts$lat, baltic$long, baltic$lat))
# I made an _extremely_ oversimplified map of europe for speedy plotting
europe <- readOGR("http://rud.is/dl/simple_europe.geojson", "OGRGeoJSON")
europe_map <- fortify(europe)
# plot to show it works
gg <- ggplot()
gg <- gg + geom_map(map=europe_map, data=europe_map,
aes(x=long, y=lat, map_id=id),
fill="white", color="black", size=0.25)
gg <- gg + geom_point(data=pts, aes(x=long, y=lat, color=is_in))
gg <- gg + geom_path(data=baltic, (aes(x=long, y=lat)), linetype=2)
# lambert isn't a bad projection for the region
gg <- gg + coord_map("lambert", lat0=50, lat1=70, xlim=c(5,31), ylim=c(50, 70))
gg
您可以使多邊形盡可能大,以獲得要包含的點。 記住,我把它做得足夠小,故意排除那個“紅色”點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.