R:在 filled.contour 图上绘制散点图

R: Plotting a scatterplot over a filled.contour plot

我是 R 的新手,使用 Plotting contours on an irregular grid . Using some sample data from Plotting contours on an irregular grid 中的数据等插值数据制作了一个 filled.contour 图,我使用以下代码制作了一个 filled.contour 和简单的散点图

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

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

有没有办法让 plot(x,y) 和 filled.contour(fld) 在同一个 plot 上(叠加)?我试过点(x,y),但这与 x 和 y 轴不匹配。在 Matlab 中,我相信我会用 hold 来做到这一点,但我不确定如何在 R 上做到这一点。

谢谢!

您可以为此使用参数 plot.titleplot.axes

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

(via)

一种方法是阅读 filled.contour 的代码,然后做一个 像这样的小黑客:

做出你的身材:

filled.contour(fld)

通过从参数列表中复制这些常量来定义它们。

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

通过从函数体中复制代码来计算这些值

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

从函数体复制代码设置布局

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

请注意,该图实际上是在色标之后绘制的, 但我们不想颠倒布局的顺序,因为 layout 实际上将 'current' 区域设置为最后一个区域,因为 第一次调用 plot.new 将导致当前区域环绕 到第一个区域。因此,当您设置绘图 window 并通过以下方式绘制点时:

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

他们根据需要叠加图形。