Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9190507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:28:41+00:00 2026-06-17T20:28:41+00:00

I have been trying for a few days to create the contour and then

  • 0

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.

The data temp.csv can be found on this link https://www.dropbox.com/s/mg2bo4rcr6n3dks/temp.csv
Shapefile can be found on the following location: https://www.dropbox.com/sh/ztvmibsslr9ocmc/YOtiwB8p9p

The script file image.scale.R can be found on the following location “https://www.dropbox.com/s/2f5s7cc02fpozk7/image.scale.R “

The code that I have used so far is as follows:

## 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")

# 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)

The output of the above code is as follows:

enter image description here

Now, I want to get rid of the colored gradients and the contours from the polygon shapefile and only leave the intersection part.

Any help is highly appreciated.

Research: I found this link https://gis.stackexchange.com/questions/25112/clip-depth-contour-with-spatial-polygon on Stack exchange Gis and I tried to follow this method I always get error while creating the contour.

I found another similar thread on https://stat.ethz.ch/pipermail/r-sig-geo/2009-May/005793.html . But I couldn’t make it work on my dataset.

I would like to acknowledge Marc in the box for helping me in getting to this point.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:28:42+00:00Added an answer on June 17, 2026 at 8:28 pm

    Indeed, @baptiste gave you a strong hint for the solution, the recent paper by Paul Murrell. Paul was generous to provide us with the code for his entire paper manuscript, which you can get from his personal website. On the side topic, Paul shows beautiful example of reproducible research with this paper. Generally, I took the following approach:

    • extract latitude and longitude coordinates from the shapefile (a function to do this is here, by Paul Hiemstra),
    • plot everything with your code,
    • and use polypath to remove everything outside the boundaries defined by shapefile, using extracted coordinates as a baseline.

      #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")
      

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to do this for a few days now using AVFoundation
I have been working with Mahout in the past few days trying to create
Over the past few days I have been trying to create/run a project in
I have been trying to figure out for a few days now why my
I have been searching through here and google for a few days now, trying
For the past few days I have been trying to play any sound over
I have been struggling for a few days trying to find out how come
So I have been trying and battling with this for a few hours now.
Have been trying to encrypt an xml file to a string so that I
I have been trying for almost a week now to create an SQLite database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.