R按组整理相关图

R Tidy correlogram by group

我想使用一种整洁的方法按组生成相关图。 我对 iris 和库 dplyrcorrplot 的尝试:

library(corrplot)
library(dplyr)

par(mfrow=c(2,2))
iris %>%
  group_by(Species) %>%
  group_map(~ corrplot::corrplot(cor(.x,use = "complete.obs"),tl.cex=0.7,title =""))

它有效,但我想在每个地块上添加物种名称。 另外,非常欢迎任何其他整洁的方法/功能!

我们可以使用 cur_group()

library(dplyr)
library(corrplot)
out <- iris %>% 
      group_by(Species) %>%
       summarise(outr = list( corrplot::corrplot(cor(cur_data(),
            use = "complete.obs"),tl.cex=0.7,title = cur_group()[[1]])))


或者如果我们使用 group_map,则默认为 .keep = FALSE。指定为TRUE,提取组元素

iris %>%
  group_by(Species) %>%
  group_map(~ corrplot::corrplot(cor(select(.x, where(is.numeric)),
      use = "complete.obs"),tl.cex=0.7,title = first(.x$Species)), .keep = TRUE)

您可以使用 splitmap 方法与 imap -

library(dplyr)
library(purrr)

iris %>%
  split(.$Species) %>%
  imap(~corrplot::corrplot(cor(.x[-5],use ="complete.obs"),tl.cex=0.7,title =.y))