How to add lines to a levelplot made using lattice (abline somehow not working)?

夙愿已清 提交于 2019-12-12 07:23:52

问题


I want to draw horizontal and vertical lines on my level plot corresponding to x values from 74 to 76 and y values from 28 to 32. Below is my R code. But when I run the following,I get the levelplots but no lines. I also recieve no error from R. The default theme on my installation is something which maps the values to pink and cyan. I have also tried using the panel function but no luck with that as well.

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

回答1:


That's not how lattice graphics work. In fact, if you read ?levelplot you'll see that there is no argument to that function called abline, so I'm not sure where you got that syntax from.

You add things to lattice graphics by altering the panel function. There are many panel.* functions for doing various things, like plotting points, lines, scatterplot smoothers, etc. In this case there's a panel.abline that we'd like to use. So we define our own panel function.

This uses the very first example from ?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)

Our new panel function needs to first draw the levelplot, so we have it call panel.levelplot first. Then we want to add some lines, so we add panel.abline for that purpose.



来源:https://stackoverflow.com/questions/7622041/how-to-add-lines-to-a-levelplot-made-using-lattice-abline-somehow-not-working

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