Tania
Tania

Reputation: 285

adding and removing grid lines from a lattice histogram

I have the following code, which creates a histogram:

inst <- data.frame(c(10:27),c(76,97,128,135,137,141,110,94,67,44,39,26,19,12,7,5,2,1))
names(inst) <- c("instSize","noOfInst")
library(lattice)
trellis.par.set(theme = col.whitebg())

myplot <- xyplot(noOfInst ~ instSize, data = inst, 
             type = c("h", "g"), col = "blue",
             xlab = "Instance Size (Number of Aircraft)",
             ylab = "Number Of Instances",
             scales=list(
               y=list(
                 at=seq(0,150,10),
                 labels=seq(0,150,10) ),
               x=list(
                 at=seq(10,27,1),
                 labels=seq(10,27,1) )
             )
             )

I would like to add the grid lines for every tick in the y axis and remove all of the vertical grid lines. Is that possible?

Thanks

Upvotes: 1

Views: 2159

Answers (3)

dayne
dayne

Reputation: 7784

myplot <- xyplot(noOfInst ~ instSize, data = inst, 
                 type = "h", col = "blue",
                 xlab = "Instance Size (Number of Aircraft)",
                 ylab = "Number Of Instances",
                 scales=list(
                   y=list(
                     at=seq(0,150,10),
                     labels=seq(0,150,10) ),
                   x=list(
                     at=seq(10,27,1),
                     labels=seq(10,27,1) )
                 ),
                 panel=function(...){
                   panel.abline(h=seq(0,150,10))
                   panel.xyplot(...)
                 }
)

You can change the color of the horizontal lines by defining col within the panel.abline() function.

Upvotes: 2

Tania
Tania

Reputation: 285

thanks both. Here is what I did at the end, just in case someone else in the future has the same problem:

myplot <- xyplot(noOfInst ~ instSize, data = inst, 
             type = c("h"), col = "blue",
             xlab = "Instance Size (Number of Aircraft)",
             ylab = "Number Of Instances",
             panel=function(...){
               panel.abline(h=seq(0,150,10),col="grey")
               panel.xyplot(...)
             },
             scales=list(
               y=list(
                 at=seq(0,150,10),
                 labels=seq(0,150,10) ),
               x=list(
                 at=seq(10,27,1),
                 labels=seq(10,27,1) )
             )
             )

This is more or less dayne's answer, however there are two things that I would like to point out:

  • the order of the commands within the function matter

    panel.xyplot(...) panel.abline(h=seq(0,150,10)) will put the grid lines on top of the data.

  • if you don't remove the "g" from the type list the vertical grid lines will still be drawn

Upvotes: 0

IRTFM
IRTFM

Reputation: 263411

?panel.grid

myplot <- xyplot(noOfInst ~ instSize, data = inst
            , col = "blue",
             xlab = "Instance Size (Number of Aircraft)",
             ylab = "Number Of Instances",
          panel = function(x, y) {
           panel.grid(h = 16, v = 0)
           panel.xyplot(x, y, type = "h")
                                 },
             scales=list(
               y=list(
                 at=seq(0,150,10),
                 labels=seq(0,150,10) ),
               x=list(
                 at=seq(10,27,1),
                 labels=seq(10,27,1) )
             )
             )
myplot

Upvotes: 3

Related Questions