【问题标题】:How to add lines to a levelplot made using lattice (abline somehow not working)?如何在使用 lattice 制作的 levelplot 中添加线条(abline 不知何故不起作用)?
【发布时间】:2011-11-29 03:20:17
【问题描述】:

我想在我的水平图上绘制水平线和垂直线,对应于从 74 到 76 的 x 值和从 28 到 32 的 y 值。下面是我的 R 代码。但是当我运行以下命令时,我得到了水平图但没有线条。我也没有收到来自 R 的错误。我安装的默认主题是将值映射到粉红色和青色的东西。我也尝试过使用面板功能,但也没有运气。

levelplot(d_fire_count_nom ~ longitude + latitude | factor(day)+factor(year), 
          data = asia,
          subset = (month == 10),  aspect="iso", contour = FALSE, layout=c(1,1), 
          main="If a fire occured in a region (low confidence) in October during 2001-2008", 
          scales=list(x=list(at=seq(from=60,to=98, by=1)), 
                      y=list(at=seq(from=5,to=38,by=1)),cex=.7, alternating=3), 
          xlim=c(60, 98), ylim=c(5, 38),
          abline=list(h=74:76, v=28:32, col="grey"))

【问题讨论】:

    标签: r lattice


    【解决方案1】:

    lattice 图形不是这样工作的。事实上,如果您阅读 ?levelplot,您会发现名为 abline 的函数没有参数,所以我不确定您从哪里得到该语法。

    您可以通过更改 panel 函数向 lattice 图形添加内容。有许多panel.* 函数可以做各种事情,比如绘制点、线、散点图平滑器等。在这种情况下,我们想使用panel.abline。所以我们定义了自己的panel函数。

    这使用了?levelplot的第一个例子:

    x <- seq(pi/4, 5 * pi, length.out = 100)
    y <- seq(pi/4, 5 * pi, length.out = 100)
    r <- as.vector(sqrt(outer(x^2, y^2, "+")))
    grid <- expand.grid(x=x, y=y)
    grid$z <- cos(r^2) * exp(-r/(pi^3))
    levelplot(z~x*y, grid,
            panel = function(...){
                panel.levelplot(...)
                panel.abline(h = 2.5)
                panel.abline(v = 2.5)
            }, 
            cuts = 50, scales=list(log="e"), xlab="",
            ylab="", main="Weird Function", sub="with log scales",
            colorkey = FALSE, region = TRUE)
    

    我们的新面板函数需要先绘制水平图,所以我们先调用panel.levelplot。然后我们想添加一些行,所以我们为此添加了panel.abline

    【讨论】:

    • @Ridhima 代码对我来说运行良好。我认为你复制+粘贴的方式出了点问题,引入了不寻常的字符。
    • @Joran 我被卡住了:( levelplot(d_fire_count_high ~ longitude + latitude | factor(day)+factor(year), data = asia, panel = function(...){ panel.levelplot( ...) panel.abline(h = 74) panel.abline(v = 28) },subset = (month == 10),aspect="iso",contour = FALSE,layout=c(1,1), main="如果2001-2008年10月某地区发生火灾(高信度)", scales=list(x=list(at=seq(from=60,to=98, by=1)), y= list(at=seq(from=5,to=38, by=1)),cex=.7, altering=3))
    • 当我运行上面的代码时,我没有收到错误,但也没有行。您的代码确实运行得很好。如果有更好的方式向您展示我的代码,请告诉我。
    • @Ridhima 您指定您的 x 比例从 60-98 和您的 y 比例从 5-38,但您想要 28 处的垂直线和 74 处的水平线?尝试绘制实际您想要的绘图区域内的线。
    • 非常感谢。我什至不知道对我犯的愚蠢错误说什么。以后我会更加小心。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 2014-11-29
    • 2020-05-31
    • 1970-01-01
    • 2013-04-22
    • 2014-02-09
    相关资源
    最近更新 更多