Skip to content

Beginner plot and map questions

3 messages · Web User, vincent@7d4.com, Roger Bivand

#
Hi,

I have not been able to find answers to these questions in the FAQs, 
manuals, or R-help archives.  If answers are available somewhere, please 
direct me to them.

1.) Is there a way to convert a table (e.g. represented as a data frame) 
to a function, specifying which columns are input and which column is 
output?  It would seem that this would be useful for plotting 
experimental results, since e.g. contour(x, y, f) requires f to be a 
function.

2.) If x is longitude and y is latitude, then I would think that 
contour(x, y, f) could be used to create a contour map with equally 
spaced latitude and longitude.  And I've seen that the "maps" and 
"mapproj" packages can be used to create maps in different projections.
a.) Once a base map is created and experimental data is projected to fit 
the base map, how is it added onto the map?
b.) Is there a way to combine the contouring and projection 
functionality to get contour maps in any of the supported projections?

3.) What algorithm is used by the contour() function?  Are there any 
parameters that can be changed, such as radius of interest for a data 
point to affect a grid point?

Thanks for any help or references.
#
Web User a ??crit :
I think on something rather ugly.
Hopefully somebody will propose a more elegant solution.

1/ store your data in a global array myexpresults[,]
2/ define f(x,y) as
f function(x,y) {return(myexpresults[x,y]);};

x,y are integers and f is discrete in the above case.
But if you want to allow x,y continuous,
you can also interpolate in f() with the grid results
the nearest from (x,y).

You can also do it without a global, by reading your table directly
in f(), but this will demand to reread the table each time f is called
which may be rather impracticable.
#
(OFF TOPIC) Although not mentioned in our exellent posting guide:

http://www.r-project.org/posting-guide.html

or Eric Raymond's excellent advice refered to in the posting guide:

http://www.catb.org/~esr/faqs/smart-questions.html

I personally often find it difficult to formulate a sensible reply to 
questions posted without a real user name and affiliation. I can 
appreciate that people posting to the list come from different generations 
and "cultures", and may not feel comfortable in saying who they are on the 
list.

I would, though, appeal to posters to give those who try to reply to 
questions at least a little help, by including an informative signature 
block. 

For want of a better name:

Dear Web User,
On Fri, 14 Oct 2005, Web User wrote:

            
You are not refering to the contour() method in the graphics package, 
which does not take an f= argument. Its third argument is z, which is a 
matrix of surface values on a grid. If your x and y vectors represent 
something other than required by the default contour() method, you must 
interpolate to such a grid first, for example using the interp() function 
in the akima package, or in some other way. As far as I am aware, R and 
contributed packages cannot compute contours directly from irregularly 
spaced (x,y,z) values. If your question relates to a contributed package, 
please say so.
It depends what kind of data, but note that the map() function in the maps 
package plots in coordinates transformed to the plotting device when 
projection is used. Without projection, map() plots and returns long/lat 
coordinates, but note that the y axis is stretched in relation to the x 
axis depending on the mean latitude of the map.
Using the contourLines() function, you can extract the lines in the 
coordinates given by the x and y grid sequences. You can also in principle 
project them too, and will be able to co-register them on the plot made by 
map(). 

If you are not bound to use the maps package for plotting and projection,
I would suggest that you look at the sp package on CRAN, and the spproj
and spmaps packages on http://r-spatial.sourceforge.net/R as a possible
alternatives. Then you can access coastline data from maps, convert
contourLines() output to a SpatialLinesDataFrame with the sp function
contourLines2SLDF(), and project both using the transform() methods in
spproj (after setting the correct projection strings for the spatial
objects).
Please see ?contour: Becker, R. A., Chambers, J. M. and Wilks, A. R. 
(1988) _The New S Language_. Wadsworth & Brooks/Cole; but see also:

http://finzi.psych.upenn.edu/R/Rhelp02a/archive/12366.html

in which Ross Ihaka writes:

"The handling of the "crossing case" in R is an implementation of that in
Cleveland's "Visualizing Data" (one of the "for-the-record" sections).
I don't think the algorithm permits crossings."

which seems authoritative, of course, the code is the complete 
documentation.