Re: [R] Contour plot overlayed with line plot

From: Roger Bivand <Roger.Bivand_at_nhh.no>
Date: Thu 11 May 2006 - 17:36:51 EST


On Wed, 10 May 2006, Jonathan Greenberg wrote:

> Duncan (and others):
>
> I figured it out (thank you!), I ended up doing a
> contour(interp(x,y,z)), and I did figure out lines(), but I can't get the
> output of lines to "sync" with the proper coordinates of the contour()
> output (its overlaying the line over the contour plot, but not in the proper
> location). Any ideas?

To fake your input data from the built-in volcano data set:

set.seed(1)
NAs <- sample(1:length(volcano), 5000)
volcanoNAs <- volcano
is.na(volcanoNAs) <- NAs
crds <- 10*(which(!is.na(volcanoNAs), arr.ind=TRUE)) crdsz <- cbind(crds, c(volcanoNAs[!is.na(c(volcanoNAs))]))

giving a three-column matrix of two coordinates and elevation values.

library(akima)
new_volcano <- interp(x=crdsz[,1], y=crdsz[,2], z=crdsz[,3]) str(new_volcano)

to see what it looks like, by default a 40 by 40 grid, and

contour(new_volcano, asp=1)
points(x=crdsz[,1], y=crdsz[,2], pch=4, col="grey")

plots the points on the same coordinate system.

What are the specific command you are using, say in relation to this toy data set?

Bets are off if you use filled.contour(); that lets you insert extra graphics, but the have to be done in the function arguments, not afterwards.

>
> --j
>
> --
>
> Jonathan A. Greenberg, PhD
> NRC Research Associate
> NASA Ames Research Center
> MS 242-4
> Moffett Field, CA 94035-1000
> Phone: 415-794-5043
> AIM: jgrn3007
> MSN: jgrn3007@hotmail.com
>
> -----Original Message-----
> From: Duncan Murdoch [mailto:murdoch@stats.uwo.ca]
> Sent: Wednesday, May 10, 2006 8:41 PM
> To: Jonathan Greenberg
> Subject: Re: [R] Contour plot overlayed with line plot
>
> On 5/10/2006 11:24 PM, Jonathan Greenberg wrote:
> > Keeping with the "newbie" questions -- I'm having a hard time figuring out
> > how to order the data -- I have x,y,z triplets, but I'm getting this
> error:
> >
> > ...increasing 'x' and 'y' values expected
> >
> > I saw in the documentation they should be ordered, but a) how do I do
> this,
> > and b) do I need to sort by x first then y?
>
> I think you don't understand the requirements of contour(). It wants
> the z values to be in an M x N matrix, and x to be a vector of length N,
> y a vector of length M. (Or did I get those transposed? It's late...)
> The x values are the x coordinates of each column of z, the y values are
> the y coordinates of them.
>
> If you have triplets (x,y,z) of data that you want to plot as a surface,
> you need to convert them to the form above, e.g. using the interp()
> function from the akima package.
>
> If I completely misunderstood your question: you can use the sort()
> function to order x and y.
>
> Duncan Murdoch
> >
> > Thanks!
> >
> > --j
> >
> > --
> >
> > Jonathan A. Greenberg, PhD
> > NRC Research Associate
> > NASA Ames Research Center
> > MS 242-4
> > Moffett Field, CA 94035-1000
> > Phone: 415-794-5043
> > AIM: jgrn3007
> > MSN: jgrn3007@hotmail.com
> >
> > -----Original Message-----
> > From: Duncan Murdoch [mailto:murdoch@stats.uwo.ca]
> > Sent: Wednesday, May 10, 2006 7:43 PM
> > To: Jonathan Greenberg
> > Subject: Re: [R] Contour plot overlayed with line plot
> >
> > On 5/10/2006 7:46 PM, Jonathan Greenberg wrote:
> >> I apologize for what may be a newbie question: I have two sets of data,
> > one
> >> is X,Y,Z data that I'd like to make a contour plot of (Z defining the
> >> contours), and a second set of X,Y data (Y as a function of X) which I
> > would
> >> like made into a line plot OVERLAYED on the contour plot (X and Y from
> > both
> >> plots are in the same units and will have overlapping values). Any
> >> suggestions on how to do this in R? Thanks!\
> >
> > Use contour() first in the usual way, then use the lines() function to
> > add lines to it. (Or points(), etc.)
> >
> > Duncan Murdoch
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> >
https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand@nhh.no

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Thu May 11 17:40:46 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 12 May 2006 - 06:10:09 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.