点图包括垂直线和不同颜色的点

问题描述 投票:0回答:1

我需要在下面的代码中添加一条竖线,例如,在位置x = 5并且所有小于5的点都有另一种颜色,例如蓝色。

变量的值可以从x轴读取,而y轴则显示变量中观察的顺序(从下到上)。远端的孤立点,在图中的任一侧,都表明潜在异常值

谢谢

library(dplyr)
library(lattice)
n = 1000
df <- tibble(
  xx1 = runif(n, min = 3, max = 10),
  xx2 = runif(n, min = 3, max = 10),
  xx3 = runif(n, min = 3, max = 10)
  )

MyVar <- c("xx1","xx2","xx3")

MydotplotBR <- function(DataSelected){

  P <- dotplot(as.matrix(as.matrix(DataSelected)),
               groups=FALSE,
               strip = strip.custom(bg = 'white',
                                    par.strip.text = list(cex = 1.2)),
               scales = list(x = list(relation = "same",tck = 1,
                                      draw = TRUE, at=seq(0,10,1)),x=list(at=seq),
                             y = list(relation = "free", draw = FALSE),
                             auto.key = list(x =1)), 
               col=10, 
               axes = FALSE,
               cex  = 0.4, pch = 5,   
               xlim=c(0,10),  
               xlab = list(label = "Variable Value", cex = 1.5),
               ylab = list(label = "Order of data in the file", cex = 1.5))

  print(P)

}
(tempoi <- Sys.time())
Vertemp <- MydotplotBR(df[,MyVar])
(tempof <- Sys.time()-tempoi)



r lattice
1个回答
0
投票

我觉得很奇怪,当在其他图的y轴上也使用值时,您只希望颜色取决于x轴。不过,这是一个自制的pairs_cutoff()函数,可以执行您想要的操作。

pairs_cutoff <- function(data, cutoff, cols = c("red", "blue"),
                         only.lower = F, ...){
  data <- as.data.frame(data)
  cns <- colnames(data)
  nc <- ncol(data)

  layout(matrix(seq_len(nc^2), ncol = nc))

  invisible(
  sapply(seq_len(nc), function(i){
    sapply(seq_len(nc), function(j){
      if(i == j){
        plot.new()
        legend("center", bty = "n", title = cns[i], cex = 1.5, text.font = 2, legend = "")
      } else {
        if(j < i & only.lower)
          plot.new()
        else{
          if(is.null(cutoff))
            cols <- cols[1]
          plot(data[,i], data[,j], col = cols[(data[,i] < cutoff) + 1], 
               xlab = cns[i], ylab = cns[j], ...)
        }
      }


    })
  })
  )
}

使用您的建议数据:

n = 1000
dat <- tibble(
  xx1 = runif(n, min = 3, max = 10),
  xx2 = runif(n, min = 3, max = 10),
  xx3 = runif(n, min = 3, max = 10)
)

pairs_cutoff(dat, cutoff = 5, only.lower = T)

输出以下图:enter image description here

您可以直接在pch中为绘图功能(例如pairs_cutoff)指定其他参数。另外,如果您需要图形的完全对称网格,请设置only.lower = F

© www.soinside.com 2019 - 2024. All rights reserved.