【问题标题】:Dot Plot include vertical line and dots of different colors点图包括垂直线和不同颜色的点
【发布时间】:2020-03-23 21:05:26
【问题描述】:

我需要在下面的代码中包含一条垂直线, 例如,在位置 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)



【问题讨论】:

  • 您为此使用 dotplot 有什么原因吗?生成情节的方式使您无法意识到您想要什么。您想要一种取决于 x 轴的颜色吗?此代码生成的 3 个面板中的 x 轴并不相同。
  • 您好,可以用其他功能。例如,如果 x 的值

标签: r lattice


【解决方案1】:

我觉得奇怪的是,当值也用于其他绘图的 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)

输出以下图:

您可以为绘图函数(例如pch)直接指定额外的参数到pairs_cutoff。 另外,如果你想要完整的对称网格,请设置only.lower = F

【讨论】:

  • 您好,感谢您的关注。但是dotchart,作为变量XX1,变量XX1的值可以从x轴读取,y轴显示变量中观察的顺序(从下到上)。作为远端的孤立点,在绘图的任一侧,表明潜在的异常值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 2018-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
相关资源
最近更新 更多