从网格直方图中添加和删除网格线

时间:2013-08-12 20:18:28

标签: r lattice

我有以下代码,它创建了一个直方图:

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

我想为y轴上的每个刻度添加网格线,并删除所有垂直网格线。这可能吗?

由于

3 个答案:

答案 0 :(得分:2)

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

您可以通过在col函数中定义panel.abline()来更改水平线的颜色。

答案 1 :(得分:2)

?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

答案 2 :(得分:0)

谢谢两位。这是我最后所做的,以防万一其他人遇到同样的问题:

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

这或多或少是dayne的答案,但我想指出两件事:

  • 函数内命令的顺序

    panel.xyplot(...) panel.abline(H = SEQ(0,150,10)) 将网格线放在数据的顶部。

  • 如果您不从类型列表中删除“g”,则仍将绘制垂直网格线