【问题标题】:Draw grid lines on specific values in xyplot在 xyplot 中的特定值上绘制网格线
【发布时间】:2011-05-15 14:04:30
【问题描述】:

我有一个xyplot,我想在 0 值上绘制网格线。

如何做到这一点?

【问题讨论】:

    标签: r plot lattice


    【解决方案1】:

    根据lattice changelog

    格子 0.19 的变化
    ========================

    o 在panel.xyplot() 中添加了新参数'grid''abline'

    所以你可以在一行中完成:

    require(lattice)
    X <- data.frame(xx=runif(20), yy=rnorm(20))
    
    xyplot(yy~xx, X, abline=list(h=0))
    

    如果你想要panel.grid 喜欢的线条样式,那么不错的技巧:

    xyplot(yy~xx, X, abline=c(list(h=0),trellis.par.get("reference.line")))
    

    【讨论】:

      【解决方案2】:

      如果您使用包latticexyplot 隐含),您可以使用panel.abline 在标记的刻度上绘制线条。

      my.df <- data.frame(a = runif(10, min = -1, max = 1), b = runif(10, min = -1, max = 1))
      my.plot <- xyplot(b ~ a, data = my.df)
      update(my.plot, panel = function(...) {
                  panel.abline(h = 0, v = 0, lty = "dotted", col = "light grey")
                  panel.xyplot(...)
              })
      

      【讨论】:

        【解决方案3】:

        有一个 lattice llines 函数替换了 base.line() 函数中的函数。还有一个 panel.lines 函数。

        #---------- method --------------
         xyplot(-1:1 ~ -1:1, type="l")
        trellis.focus("panel", 1, 1)
        do.call("panel.abline", list(h=0,v=0, lty=3) )
        trellis.unfocus()
        # --- that method has the advantage of also demonstrating 
        #        how to modify an existing plot
        
        #---------- method 2--------------
        
         xp <-xyplot(-2:1 ~ -2:1, type="l", panel=function(...){
         panel.xyplot(...)
         panel.abline(h=0,v=0, lty=3)} )
        xp
        

        【讨论】:

        • 你能举个例子吗
        猜你喜欢
        • 2012-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-20
        • 2021-10-09
        • 1970-01-01
        相关资源
        最近更新 更多