r lattice - wrong position of horizontal abline after bwplot

醉酒当歌 提交于 2019-12-11 02:33:32

问题


I can do this without problems:

boxplot(coef ~ habitat, data = res)
abline(h = 0, col = "green")

But when I use lattice, the horizontal line is misplaced:

bwplot(coef ~ habitat, data = res)
abline(h = 0, col = "green")

I tried to use panel.abline instead but that places the green line on top of the picture.


回答1:


Use a panel function; order the contained functions in the order you'd like parts added; make use of ... to avoid having to know / manage all parameters to the panel function

bwplot(coef ~ habitat, data = res, panel=function(...) {
    panel.abline(h=0, col="green")
    panel.bwplot(...)
})

The notion of a panel function makes more sense when there are several panels, e.g.,

res = data.frame(coef=rnorm(99) + (-1):1, habitat=sample(letters[1:3], 99, TRUE),
                 grp=c("W", "X", "Y"))
bwplot(coef ~ habitat | grp, data = res, panel=function(x, y, ...) {
    ## green line at panel-median y value
    panel.abline(h=median(y), col="green")
    panel.bwplot(x, y, ...)
})


来源:https://stackoverflow.com/questions/18274873/r-lattice-wrong-position-of-horizontal-abline-after-bwplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!