首页 > 解决方案 > 使用 lattice 包中的 qqmath 更改点的颜色

问题描述

我在 Lattice 包中使用 qqmath 制作了一个图(我将其子集为仅 3 点,以便更容易举例)。

table <- data.table(Col1=c(12,3,4), Col2 = c(54,4,6), Col3 = c("Pink", "Pink", "Red"))

PrbGrd <- qnorm(c(0.00001,0.0001,0.001,0.01, 0.05, 0.10,0.20,0.30,0.40, 
0.50, 0.60, 0.70,0.80,0.90,0.95,0.99,0.999,0.9999,0.99999))

PrbGrdL<-c("0.001","0.01","0.1","1","5","10","20","30","40","50","60","70","80","90","95","99","99.9","99.99","99.999")
PrbGrdL2<- c("99.999","99.99","99.9","99","95","90","80","70","60","50","40","30","20","10","5","1","0.1","0.01","0.001")


ValGrd<- c(seq(0.001,0.01,0.001),seq(0.01,0.1,0.01),seq(0.1,1,0.1),seq(1,10,1),seq(10,100,10),seq(100,1000,100),seq(1000,10000,1000))
ValGrd<- log10(ValGrd)
ValGrd2 <- c(-2:20)


ProbPlot <- qqmath(~ Col1, 
                data= table,
                distribution = function(p) qnorm(p),
                main = "Normal probability plot",
                pch=20,
                cex=0.5,
                xlab="Probability of Lower",
                ylab = "Pb",
                #xlim = c(max(PrbGrd),min(PrbGrd)),
                xlim = c(min(PrbGrd),max(PrbGrd)),
                scales=list(y=list(alternating=1),x = list(at = PrbGrd, labels = PrbGrdL, cex = 0.8)),
                #yscale.components=yscale.components.log10ticks,
                panel=function(x,...){
                  panel.abline(v=PrbGrd ,col="grey",lty=3)
                  panel.abline(h=ValGrd2,col="grey",lty=3)
                  panel.qqmath(x,distribution=qnorm)
                }
)

我想使用表格第三列(Col3)中的颜色来更改绘图上各个点的颜色。我不知道如何在 qqmath 中做到这一点,使用常规绘图功能会很简单......但使用 qqmath 似乎并不那么简单。

谢谢!

标签: rcolorspackagelattice

解决方案


更新:基于 OP 的评论。

您可以添加col参数,例如:

ProbPlot <- qqmath(~ Col1, 
                data = table,
                distribution = function(p) qnorm(p),
                main = "Normal probability plot",
                pch = 20,
                cex = 0.5,
                xlab = "Probability of Lower",
                ylab = "Pb",
                #xlim = c(max(PrbGrd),min(PrbGrd)),
                xlim = c(min(PrbGrd),max(PrbGrd)),
                scales = list(y = list(alternating = 1),
                              x = list(at = PrbGrd,
                                       labels = PrbGrdL,
                                       cex = 0.8)),
                #yscale.components=yscale.components.log10ticks,
                panel = function(x, ...){
                  panel.abline(v = PrbGrd, 
                               col = "grey", 
                               lty = 3)
                  panel.abline(h = ValGrd2, 
                               col = "grey", 
                               lty = 3)
                  panel.qqmath(x,
                               distribution=qnorm,
                               col = table$Col3) # add colors for each point
                }
)

推荐阅读