绘制 clprofiles 函数而无需每次都按回车键
Plot clprofiles function without hitting enter each time
我正在寻找一种无需每次都按回车键即可获取所有变量图的方法。
如果你熟悉 Kprototype 的这个函数 clprofiles,你就会知道这个消息 Hit <Return> to see next plot:
,我想一次看到所有的变量图。
现在我尝试在指令 clprofiles(kpres, df)
:
之后执行 'for loop'
clprofiles(kpres, df)
for (i in 1:length(t)) {
print("
")
}
但是没用。
感谢您的帮助。
您可以覆盖四个提示中的三个(但不是第一个),因为绘图方法在 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:
>
它产生:
在这种情况下,您将不得不覆盖 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)
产生与第一个答案相同的情节。
我找到了另一种解决方案,它在外部 window(全屏)中显示绘图,而不是每次都按 "enter",您只需单击
dev.new(width=5,height=4,noRStudioGD = TRUE)
clprofiles(kpres,df)
我正在寻找一种无需每次都按回车键即可获取所有变量图的方法。
如果你熟悉 Kprototype 的这个函数 clprofiles,你就会知道这个消息 Hit <Return> to see next plot:
,我想一次看到所有的变量图。
现在我尝试在指令 clprofiles(kpres, df)
:
clprofiles(kpres, df)
for (i in 1:length(t)) {
print("
")
}
但是没用。 感谢您的帮助。
您可以覆盖四个提示中的三个(但不是第一个),因为绘图方法在 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:
>
它产生:
在这种情况下,您将不得不覆盖 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)
产生与第一个答案相同的情节。
我找到了另一种解决方案,它在外部 window(全屏)中显示绘图,而不是每次都按 "enter",您只需单击
dev.new(width=5,height=4,noRStudioGD = TRUE)
clprofiles(kpres,df)