在使用 corrr 绘图之前指定相关强度

Specifying Correlation strength prior to plotting using corrr

我有一个相关性数据框,我试图只播种高于 10% 的相关性 然后我想使用 corrr

绘制它

我获取我的数据集的相关性,然后过滤到绝对值 >.1 的地方,但它在网络图段上失败了

Error in UseMethod("network_plot") : no applicable method for 'network_plot' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"

library(tidyverse)
library(corrr)

# Create the Dataframe
mydf <- data.frame(a=sample(rnorm(n = 100,sd = 15),replace=TRUE),
                   b=sample(rnorm(n = 100,sd = 15),replace=TRUE),
                   c=sample(rnorm(n = 100,sd = 15),replace=TRUE),
                   d=sample(rnorm(n = 100,sd = 15),replace=TRUE),
                   e=sample(rnorm(n = 100,sd = 15),replace=TRUE),
                   f=sample(rnorm(n = 100,sd = 15),replace=TRUE))


test <- mydf %>% 
  correlate(method = "spearman") %>% 
  gather("n", "corr", 2:7, na.rm = TRUE) %>% 
  filter(abs(corr) > 0.1) %>% 
  spread(rowname,corr) %>%
  network_plot(legend = TRUE)

我在包的 documentation 中看到您可以指定您希望可视化的相关性,但是即使我要求不包括它们,它似乎仍会打印标签,这就是为什么我走 gather/spread 路线

  network_plot(min_cor = .2, legend = TRUE)

感谢您的宝贵时间

感谢您通过电子邮件提醒我这个问题(当我有更多时间时,我会单独回复)。我现在就记下一个快速解决方案。

这是一个有趣的问题。据我所知,您想完全排除与任何其他变量的相关性不超过绝对量级的任何变量。

如您所述,指定 min_cor 将停止绘制路径,但不会绘制变量本身。

我可能会在 GitHub 页面中添加一个问题,并尝试在将来解决它。现在,这是一个在 purrr 包的帮助下的变通方法。

library(corrr)
library(purrr)

mydf <- data.frame(a=sample(rnorm(n = 100,sd = 15),replace=TRUE),
                   b=sample(rnorm(n = 100,sd = 15),replace=TRUE),
                   c=sample(rnorm(n = 100,sd = 15),replace=TRUE),
                   d=sample(rnorm(n = 100,sd = 15),replace=TRUE),
                   e=sample(rnorm(n = 100,sd = 15),replace=TRUE),
                   f=sample(rnorm(n = 100,sd = 15),replace=TRUE))

# Create the correlation data frame
rdf <- mydf %>% correlate(method = "spearman")

# Identify which variables to keep
to_keep <- map_lgl(rdf, ~ !is.numeric(.) || any(abs(.) > .1))
to_keep <- names(to_keep)[!is.na(to_keep)]

# Create the network plot
rdf %>%
  focus_(.dots = to_keep, mirror = TRUE) %>% 
  network_plot(legend = TRUE, min_cor = .1)

如果这不起作用,您可能需要通过 devtools::install_github("drsimonj/corrr")

安装最新的 corrr 开发版本