我有一个包含127个变量的数据集:
cols <- c("Important", paste("var", 1:126, sep = ""))我有一个cormat物体是这样制作的:
cormat <- round(cor(data), 3)我还有一个27英寸的1440显示器(如果这很重要的话)。
library(ggcorrplot)
ggcorrplot(cormat, lab = TRUE,
outline.col = "white", ggtheme = ggplot2::theme_gray)..。我看不懂,因为它的变量太多了。我试过不同的数字,超过30个变量难以辨认。所以我做了:
ggcorrplot(transac_shadow_cor[1:30, 1:30], lab = TRUE,
outline.col = "white", ggtheme = ggplot2::theme_gray)
ggcorrplot(transac_shadow_cor[31:60, 31:60], lab = TRUE,
outline.col = "white", ggtheme = ggplot2::theme_gray)以此类推,制作了5张清晰的热图。(1:30、31:60、61:90、91:120、121:127)
请求:,我想构建一个构建这些热图的for loop,但是我不知道如何对所有变量进行30的子集。如果在此基础上,我可以在每个热图上有第一个变量“重要”,因为..。这很重要,但如果我不这么做也没什么大不了的。
我不喜欢ggcorrplot,它只是我用的那个。
发布于 2019-09-17 04:18:49
这将是一个起点:
transac_shadow_cor
Size <- 30
Init <- 0
Iteration <- floor(dim(transac_shadow_cor)[1] / Size) #You have some remaining
variables
End <- Size
for (i in 1:Iteration){
ggcorrplot(transac_shadow_cor[c(Init+1):End, c(Init+1):End], lab = TRUE,
outline.col = "white", ggtheme = ggplot2::theme_gray)
Inti <- Init + Size
End <- End + Size
}发布于 2019-09-17 06:38:20
回答我自己的问题,酷。@Orlando Sabogal差点就有了。以下是我根据他的建议编写的代码:
Size <- 30
Init <- 0
Iteration <- floor(dim(transac_shadow_cor)[1] / Size) #You have some remaining
End <- Size
heatmaps <- c() # this strores heatmaps for latter printing
for (i in 1:Iteration) {
# Store each heatmap
heatmaps[[i]] <- ggcorrplot(transac_shadow_cor[c(Init+1):End, c(Init+1):End], lab = TRUE,
outline.col = "white", ggtheme = ggplot2::theme_gray)
# now print it
plot(heatmaps[[i]])
Init <- Init + Size # he had a typo here
End <- End + Size
}现在是棘手的部分。在每幅图上用“重要”变量打印它
Size <- 30
Init <- 0
Iteration <- floor(dim(cormat)[1] / Size) #You have some remaining
End <- Size
heatmaps <- c() # this strores heatmaps for latter printing
range <- c(1, seq(from = 2, to = End)) # the range of variables to appear on the heatmap
for (i in 1:Iteration) {
# Store each heatmap
heatmaps[[i]] <- ggcorrplot(cormat[range, range], lab = TRUE,
outline.col = "white", ggtheme = ggplot2::theme_gray)
# now print it
plot(heatmaps[[i]])
# move the chains of the loop
Init <- Init + Size # he had a typo here
End <- End + Size
range <- c(1, seq(from = Init+1, to = End)) # increase range
}而且起作用了!哈!它看起来像一件可怕的事情,但它毕竟没有那么糟糕。谢谢,奥兰多·萨博加尔,如果没有你的主意,那是不可能的
https://stackoverflow.com/questions/57966847
复制相似问题