产生多重相关性”;“热图”;对于许多变量,使用for循环
我有一个包含127个变量的数据集:产生多重相关性”;“热图”;对于许多变量,使用for循环,r,ggplot2,data-visualization,R,Ggplot2,Data Visualization,我有一个包含127个变量的数据集: cols <- c("Important", paste("var", 1:126, sep = "")) 。。。我看不懂,因为变量太多了。我试过不同的数字,有30多个变量难以辨认。所以我做了: ggcorrplot(transac_shadow_cor[1:30, 1:30], lab = TRUE, outline.col = "white", ggtheme = ggplot2::theme_gray) ggcorrplot
cols <- c("Important", paste("var", 1:126, sep = ""))
。。。我看不懂,因为变量太多了。我试过不同的数字,有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
来构建这些热图,但我不知道如何将所有变量的子集设置为30。如果再加上它,我可以在每个热图上都有第一个变量“重要”,那将是多么神奇,因为。。这很重要,但如果我不这么做也没什么大不了的
我没有连接到
ggcorrplot
,它只是我正在使用的一个。这将是一个起点:
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
}
transac\u shadow\u cor
尺寸回答我自己的问题,酷。
@奥兰多·萨博加尔差点就成功了。以下是我根据他的建议编写的代码:
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
}
大小非常接近!,但有了你的回答,我自己设法找到了解决办法。谢谢
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
}
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
}