【问题标题】:R: Plotting a scatterplot over a filled.contour plotR:在填充的轮廓图上绘制散点图
【发布时间】:2015-01-31 23:02:52
【问题描述】:

我对 R 非常陌生,并且使用插值数据(如 Plotting contours on an irregular grid 中的数据)制作了填充轮廓图。使用来自Plotting contours on an irregular grid 的一些样本数据,我使用以下代码制作了一个填充的轮廓和简单的散点图

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) 在同一个图上(重叠)?我已经尝试过点(x,y),但这与 x 和 y 轴不匹配。在 Matlab 中,我相信我会使用 hold 来执行此操作,但我不确定如何在 R 上执行此操作。

谢谢!

【问题讨论】:

    标签: r plot


    【解决方案1】:

    您可以为此使用参数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)

    【讨论】:

    • 嗯,有趣的是,我得到了想要的结果,但现在我缺少我的情节标题或情节轴(取决于我替换的参数)。关于如何拥有这一切的任何建议?
    • 只需分别在points()axis(1); axis(2)之前/之后添加title("my title")
    • 不客气。如果这个答案解决了你的问题,你可以“接受”它并关闭话题。
    【解决方案2】:

    一种方法是阅读 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 实际上将“当前”区域设置为最后一个区域,因为 第一次调用plot.new 将导致当前区域环绕 到第一个区域。因此,当您设置绘图窗口并通过以下方式绘制点时:

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

    他们根据需要覆盖图形。

    【讨论】:

      猜你喜欢
      • 2021-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 2015-04-23
      相关资源
      最近更新 更多