2

I am very new to R and have made a filled.contour plot using interpolated data like the data found in Plotting contours on an irregular grid . Using some sample data from Plotting contours on an irregular grid , I made a filled.contour and simple scatterplot using the following codes

x <- datr$Lat
y <- datr$Lon
z <- datr$Rain

require(akima)
fld <- interp(x,y,z)
filled.contour(fld)
plot(x,y)

Is there a way to make the plot(x,y) and filled.contour(fld) on the same plot (overlaying)? I have tried the points(x,y), but this doesn't match the x and y axes. In Matlab, I believe I would do this with hold, but I am unsure how to do it on R.

Thanks!

Community
  • 1
  • 1
KHeal
  • 23
  • 4

2 Answers2

1

You could use the arguments plot.title or plot.axes for that:

x <- 10*1:nrow(volcano)
y <- 10*1:ncol(volcano)
filled.contour(x, y, volcano, plot.title = { 
  points(x = 200, y = 200)
})

(via)

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Huh, interesting, I got the desired result, but now I'm missing either my plot title or plot axes (depending on what argument I substituted). Any suggestions on how to have it all? – KHeal Jan 31 '15 at 23:41
  • Just add `title("my title")` before/after `points()`, or `axis(1); axis(2)` respectively. – lukeA Jan 31 '15 at 23:49
  • You're welcome. If this answer solves your problem, you can "accept" it and close the topic. – lukeA Feb 02 '15 at 18:48
0

One way is to read the code for filled.contour, and do a little hacking like so:

Make your figure:

filled.contour(fld)

Define these constants by copying them from the arguments list.

nlevels = 20
zlim = range(z, finite = TRUE)
las = 1 
levels = pretty(zlim, nlevels)
xlim = range(x, finite = TRUE)
ylim = range(y, finite = TRUE)
xaxs = "i"
yaxs = "i"
asp = NA

Calculate these values by copying code from the function body

mar.orig <- (par.orig <- par(c("mar", "las", "mfrow")))$mar
w <- (3 + mar.orig[2L]) * par("csi") * 2.54

Set the layout by copying code from the function body

layout(matrix(c(2, 1), ncol = 2L), widths = c(1, lcm(w)))

Noteice that the figure is actually plotted after the color scale, but we don't wnat to reverse the order of the layout because layout actually sets the 'current' region as the last region because the first call to plot.new will cause the current region to wrap around to the first region. Hence, when you set the plot window and plot the points via:

plot.window(ylim=ylim,xlim=xlim)
points(x,y)
title(main='title',
      sub='Sub-Title',
      xlab='This is the x axis',
      ylab='This is the y axis')

They overlay figure as desired.

Jthorpe
  • 9,756
  • 2
  • 49
  • 64