質問

I have a xyplot and I want to draw grid lines on the 0 values.

How this can be done?

役に立ちましたか?

解決

If you're using package lattice (which is implied with xyplot), you can use panel.abline to draw lines over labeled ticks.

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(...)
        })

enter image description here

他のヒント

According to lattice changelog:

Changes in lattice 0.19
=======================

o Added new arguments 'grid' and 'abline' in panel.xyplot().

So you could do it in one line:

require(lattice)
X <- data.frame(xx=runif(20), yy=rnorm(20))

xyplot(yy~xx, X, abline=list(h=0))

Lattice graph with added line

If you want panel.grid like line style, then nice trick:

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

Lattice graph with added nice style line

There is a lattice llines function that replaces the function of lines() functionality in base. There is also a panel.lines function.

#---------- 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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top