title: Intersect the contour and polygon in R
body: <p>I have been trying for a few days to create the contour and then plot the shapefile and contour on the same file. Now, that I am able to create the contour and shapefile on the same plot. I want to clip the contour with the shapefile an only show the shapefile. </p>
  
  <p>The data temp.csv can be found on this link <a href="https://www.dropbox.com/s/mg2bo4rcr6n3dks/temp.csv" rel="nofollow noreferrer">https://www.dropbox.com/s/mg2bo4rcr6n3dks/temp.csv</a>
  Shapefile can be found on the following location: <a href="https://www.dropbox.com/sh/ztvmibsslr9ocmc/YOtiwB8p9p" rel="nofollow noreferrer">https://www.dropbox.com/sh/ztvmibsslr9ocmc/YOtiwB8p9p</a></p>
  
  <p>The script file image.scale.R can be found on the following location "<a href="https://www.dropbox.com/s/2f5s7cc02fpozk7/image.scale.R" rel="nofollow noreferrer">https://www.dropbox.com/s/2f5s7cc02fpozk7/image.scale.R</a> "</p>
  
  <p>The code that I have used so far is as follows:</p>
  
  <pre><code>## 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 &lt;- read.table("temp.csv",header=TRUE,sep=",")
  
  x &lt;- age2100$x 
  y &lt;- age2100$y   
  z &lt;- age2100$z
  
  ####################################
  ##Load the shape file 
  ##################################### 
  shapefile &lt;- readShapePoly("Export_Output_4.shp")
  
  fld &lt;- interp(x,y,z)
  
  par(mar=c(5,5,1,1)) filled.contour(fld)
  
  ###Import the image.scale 
  source source("image.scale.R")
  </code></pre>
  
  <p># <a href="http://menugget.blogspot.de/2011/08/adding-scale-to-image-plot.html" rel="nofollow noreferrer">http://menugget.blogspot.de/2011/08/adding-scale-to-image-plot.html</a></p>
  
  <pre><code>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)
  </code></pre>
  
  <p>The output of the above code is as follows:</p>
  
  <p><img src="https://i.stack.imgur.com/mCuuh.png" alt="enter image description here"></p>
  
  <p>Now, I want to get rid of the colored gradients and the contours from the polygon shapefile and only leave the intersection part. </p>
  
  <p>Any help is highly appreciated. </p>
  
  <p>Research: I found this link <a href="https://gis.stackexchange.com/questions/25112/clip-depth-contour-with-spatial-polygon">https://gis.stackexchange.com/questions/25112/clip-depth-contour-with-spatial-polygon</a> on Stack exchange Gis and I tried to follow this method I always get error while creating the contour. </p>
  
  <p>I found another similar thread on <a href="https://stat.ethz.ch/pipermail/r-sig-geo/2009-May/005793.html" rel="nofollow noreferrer">https://stat.ethz.ch/pipermail/r-sig-geo/2009-May/005793.html</a> . But I couldn't make it work on my dataset. </p>
  
  <p>I would like to acknowledge Marc in the box for helping me in getting to this point.</p>
  
  <p>Thanks.</p>