【问题标题】:Plot clprofiles function without hitting enter each time绘制 clprofiles 函数而无需每次都按回车键
【发布时间】:2020-09-25 18:02:06
【问题描述】:

我正在寻找一种方法来获取所有变量图,而无需每次都按 Enter。 如果你熟悉 Kprototype 的这个函数 clprofiles,你知道这条消息Hit <Return> to see next plot:,我想一次看到所有的变量图。 现在我尝试在指令clprofiles(kpres, df) 之后执行“for 循环”:

    clprofiles(kpres, df)
for (i in 1:length(t)) {
  print("
        ")

}

但是没用。 感谢您的帮助。

【问题讨论】:

    标签: r plot cluster-analysis


    【解决方案1】:

    我找到了另一种在外部窗口(全屏)中显示绘图的解决方案,而不是每次都按“输入”,您只需单击

    dev.new(width=5,height=4,noRStudioGD = TRUE)
    clprofiles(kpres,df)
    

    【讨论】:

      【解决方案2】:

      在这种情况下,您将不得不覆盖clprofiles 的默认行为。将这个新函数 my.clprofiles 添加到您的脚本中:

      my.clprofiles <- function(object, x, vars = NULL, col = NULL){
        library(RColorBrewer)
        if(length(object$cluster) != nrow(x)) stop("Size of x does not match cluster result!")
        if(is.null(vars)) vars <- 1:ncol(x)
        if(!is.numeric(vars)) vars <- sapply(vars, function(z) return(which(colnames(x)==z)))
        if(length(vars) < 1) stop("Specified variable names do not match x!")
        if(is.null(col)){
          k <- max(unique(object$cluster))
          if(k > 2)  col <- brewer.pal(k, "Set3")
          if(k == 2) col <- c("lightblue","orange")
          if(k == 1) col <- "lightblue"
        }
        clusids <- sort(unique(object$cluster)) 
        if(length(col) != max(clusids)) warning("Length of col should match number of clusters!")
      
        #REMOVE PROMPT
        #par(ask=TRUE)
      
        par(mfrow=c(2,2))
      
        for(i in vars){
          if(is.numeric(x[,i])){
            boxplot(x[,i]~object$cluster, col = col, main = colnames(x)[i])
            legend("topright", legend=clusids, fill = col)
          } 
          if(is.factor(x[,i])){
            tab <- table(x[,i], object$cluster)
            for(j in 1:length(object$size)) tab[,j] <- tab[,j]/object$size[j]
            barplot(t(tab), beside = TRUE, main = colnames(x)[i], col = col)
          } 
        }
        invisible()
      }
      

      然后你可以调用它一次而无需按 Enter:

      my.clprofiles(kpres,x)
      

      这会产生与第一个答案相同的情节。

      【讨论】:

        【解决方案3】:

        您可以覆盖四个提示中的三个(但不是第一个),因为绘图方法在 clprofiles 命令中。如果您的目标只是将所有图打印在一个图上,则可以这样做:

        library(clustMixType)
        
        # Example from documentation
        n   <- 100;    prb <- 0.9;    muk <- 1.5 
        clusid <- rep(1:4, each = n)
        x1 <- sample(c("A","B"), 2*n, replace = TRUE, prob = c(prb, 1-prb))
        x1 <- c(x1, sample(c("A","B"), 2*n, replace = TRUE, prob = c(1-prb, prb)))
        x1 <- as.factor(x1)
        x2 <- sample(c("A","B"), 2*n, replace = TRUE, prob = c(prb, 1-prb))
        x2 <- c(x2, sample(c("A","B"), 2*n, replace = TRUE, prob = c(1-prb, prb)))
        x2 <- as.factor(x2)
        x3 <- c(rnorm(n, mean = -muk), rnorm(n, mean = muk), rnorm(n, mean = -muk), rnorm(n, mean = muk))
        x4 <- c(rnorm(n, mean = -muk), rnorm(n, mean = muk), rnorm(n, mean = -muk), rnorm(n, mean = muk))
        x <- data.frame(x1,x2,x3,x4)
        kpres <- kproto(x, 4)
        

        然后你可以先用par做准备:

        > par(mfrow=c(2,2))
        > clprofiles(kpres, x)
        Hit <Return> to see next plot: 
        > 
        

        它会产生:

        【讨论】:

        • 这真是个好主意,谢谢。但是我有太多的变量和这么多的水平,所以将所有地块放在一个地块中的解决方案不适合我的情况。我想要单独绘制每个变量。感谢您的帮助!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-12
        • 1970-01-01
        • 2019-10-26
        • 1970-01-01
        • 2014-10-12
        • 2020-05-21
        • 1970-01-01
        相关资源
        最近更新 更多