Search code examples
rlattice

adding abline to the xyplot (Semi-log)


I am trying to add the abline on xyplot with y-axis on logscale. I can add the line using abline when y is not on log scale but it doesn't work when y is on log scale.

Any suggestions please!

Thank you!

library(lattice)
y <- c(2,10,25,70)
x <- c(0.2,0.3,0.5,1)

xyplot(y~x,
  type='b',
  scales=list(y=list(log=TRUE)),
  panel = function(x, y) {
    panel.xyplot(x, y)
    panel.abline(h=8.8)
  }
)

Solution

  • Oddly lattice does not handle the log scale correctly for abline. But it seems to work if you do the calculation for lattice:

    library(lattice)
    y <- c(2,10,25,70)
    x <- c(0.2,0.3,0.5,1)
    
    xyplot(y~x,
           type='b',
           scales=list(y=list(log=T)),
           panel = function(x, y) {
             panel.xyplot(x, y)
             panel.abline(h=log(8.8,10))
           }
    )
    

    enter image description here