按组设置颜色段

Set color segments by group

我正在尝试制作一种 xyplot,其中一条线从底部到值。问题是不知道怎么调整线条的颜色

time <-  rnorm(50, 5, 2)
death.count <- rnorm(50, -0.25, 0.25)
Inoc.size <-rep(c("A", "B"), times=25)
    
data <- data.frame(time, death.count, Inoc.size)   
    
xyplot(death.count ~ time, data,
       groups=Inoc.size, ylim=c(0, -0.5),
       xlab=list("Time - h", cex=1.5),
       ylab=list(expression("Death cells - ln N"[i]), cex=1.5),
       par.settings=list(
           alpha=0.5,
           superpose.symbol=list(pch=c(15, 16, 17, 18), 
               col=c(myColours[3], myColours[6], myColours[4], myColours[7]))),
           auto.key=T,
           panel=panel.superpose,
           panel.groups=function(x, y, col, group.number, groups, ...) {
               xj <- jitter(as.numeric(x), factor=0.5)
               panel.xyplot(xj, y, ...)
               panel.segments(x0=xj, x1=xj, y0=0, y1=y, groups, lwd=2)
           }
)

我想你需要 col=c(myColours[3],myColours[6],myColours[4],myColours[7]) 到 panel.segments。

panel.segments(x0=xj,x1=xj, y0=0, y1=y,groups,lwd = 2, col=c(myColours[3],myColours[6],myColours[4],myColours[7]))

我不太明白你的情节与你的代码如何匹配。 但这可能对你有用。在 panel.groups 中定义 col 并在 panel.segments.

中引用由 group.number 索引的内容
library(lattice)
 myColours=1:7
 time<-  rnorm(50,5,2)
     death.count<- rnorm(50,-0.25,0.25)
     Inoc.size<-rep(c("A","B"),times=25)

     data<-data.frame( time,death.count, Inoc.size)  

 xyplot(death.count~time, data,
       groups=Inoc.size,#ylim=c(0,-0.5),
    xlab = list("Time - h", cex=1.5),
   ylab = list(expression("Death cells - ln N"[i]), cex=1.5),
    par.settings= list(
     alpha = 0.5,superpose.symbol=list(
     pch=c(15,16,17,18),
      col=c(myColours[3],myColours[6],myColours[4],myColours[7])),
         pch=c(15,16,17,18),
     col=c(myColours[3],myColours[6],myColours[4],myColours[7])),
       auto.key=T,
    panel = panel.superpose,
        panel.groups = function(x, y, col,group.number,groups, ...) {
           xj=jitter(as.numeric(x), factor=0.5)
 col=c(myColours[3],myColours[6],myColours[4],myColours[7])
           panel.xyplot(xj,y,...)
           panel.segments(x0=xj,x1=xj, y0=0, y1=y,col=col[group.number],groups,
                                        lwd = 2)
                              }
                     )