1

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

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 轴上的每个刻度添加网格线并删除所有垂直网格线。那可能吗?

谢谢

4

3 回答 3

3
?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
于 2013-08-12T20:35:16.263 回答
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()函数中定义来更改水平线的颜色。

于 2013-08-12T20:30:39.387 回答
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”,仍将绘制垂直网格线

于 2013-08-12T20:47:33.927 回答