与R中的轮廓和多边形相交

Jd *_*aba 5 intersection r contour clip ggplot2

我已经尝试了几天来创建轮廓,然后将shapefile和轮廓绘制在同一文件上。现在,我可以在同一图上创建轮廓和shapefile。我想用shapefile剪切轮廓,只显示shapefile。

数据temp.csv可以在此链接上找到https://www.dropbox.com/s/mg2bo4rcr6n3dks/temp.csv Shape文件可在以下位置找到:https://www.dropbox.com/sh/ztvmibsslr9ocmc / YOtiwB8p9p

脚本文件image.scale.R可以在以下位置找到:“ https://www.dropbox.com/s/2f5s7cc02fpozk7/image.scale.R

到目前为止,我使用的代码如下:

## Required packages 
library(maptools) 
library(rgdal) 
library(sp) 
library(maptools) 
library(sm) 
require(akima) 
require(spplot) 
library(raster) 
library(rgeos)

## Set Working Directory 
setwd("C:\\Users\\jdbaba\\Documents\\R working folder\\shape")

## Read Data from a file 
age2100 <- read.table("temp.csv",header=TRUE,sep=",")

x <- age2100$x 
y <- age2100$y   
z <- age2100$z

####################################
##Load the shape file 
##################################### 
shapefile <- readShapePoly("Export_Output_4.shp")

fld <- interp(x,y,z)

par(mar=c(5,5,1,1)) filled.contour(fld)

###Import the image.scale 
source source("image.scale.R")
Run Code Online (Sandbox Code Playgroud)

http://menugget.blogspot.de/2011/08/adding-scale-to-image-plot.html

x11(width=8, height=7) 
layout(matrix(c(1,2), nrow=1, ncol=2), widths=c(6,1), height=6, respect=TRUE) 
layout.show(2)

par(mar=c(4,4,1,2)) 
image(fld,axes=T) 
contour(fld, add=TRUE)
#points(age2100$x,age2100$y, pch=".", cex=2,legend=F) 
plot(shapefile,add=T,lwd=2) 
box()
par(mar=c(4,0,1,4)) 
image.scale(fld, xlab="Eastings", ylab="Northings", xaxt="n", yaxt="n", horiz=FALSE)

axis(4)
mtext("Salinity", side=4, line=2.5)
Run Code Online (Sandbox Code Playgroud)

上面代码的输出如下:

在此处输入图片说明

现在,我想摆脱多边形shapefile中的彩色渐变和轮廓,只保留相交部分。

非常感谢您的帮助。

研究:我在堆栈交换Gis上找到了此链接https://gis.stackexchange.com/questions/25112/clip-depth-contour-with-spatial-polygon,我尝试按照这种方法创建轮廓时总是出错。

我在https://stat.ethz.ch/pipermail/r-sig-geo/2009-May/005793.html上找到了另一个类似的线程。但是我无法使其在我的数据集上运行。

我想在包装盒中感谢Marc帮助我做到这一点。

谢谢。

Gee*_*cid 4

事实上,@baptiste 为您提供了解决方案的强烈提示,即Paul Murrell 最近发表的论文。保罗慷慨地向我们提供了他的整个纸质手稿的代码,您可以从他的个人网站获取该代码。在副标题上,保罗通过这篇论文展示了可重复研究的美丽例子。一般来说,我采取了以下方法:

  • 从 shapefile 中提取纬度和经度坐标(执行此操作的函数位于此处,作者:Paul Hiemstra),
  • 用你的代码绘制所有内容,
  • 并使用polypath提取的坐标作为基线来删除 shapefile 定义的边界之外的所有内容。

    #function to extract coordinates from shapefile (by Paul Hiemstra)
    allcoordinates_lapply = function(x) { 
        polys = x@polygons 
        return(do.call("rbind", lapply(polys, function(pp) { 
               do.call("rbind", lapply(pp@Polygons, coordinates)) 
               }))) 
    } 
    q = allcoordinates_lapply(shapefile)
    
    #extract subset of coordinates, otherwise strange line connections occur...
    lat = q[110:600,1]
    long = q[110:600,2]
    
    #define ranges for polypath
    xrange <- range(lat, na.rm=TRUE)
    yrange <- range(long, na.rm=TRUE)
    xbox <- xrange + c(-20000, 20000)
    ybox <- yrange + c(-20000, 20000)
    
    #plot your stuff
    plot(shapefile, lwd=2)
    image(fld, axes=F, add=T)
    contour(fld, add=T)
    #and here is the magic 
    polypath(c(lat, NA, c(xbox, rev(xbox))),
             c(long, NA, rep(ybox, each=2)),
             col="white", rule="evenodd")
    
    Run Code Online (Sandbox Code Playgroud)

在此输入图像描述