Skip to content

Instantly share code, notes, and snippets.

@Deleetdk
Created December 12, 2022 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deleetdk/1c65a68567be7c8c2b59a7ba63c7d43a to your computer and use it in GitHub Desktop.
Save Deleetdk/1c65a68567be7c8c2b59a7ba63c7d43a to your computer and use it in GitHub Desktop.
pairwise correlation with crosses
library(kirkegaard)
#get cors with p values
cors = iris[-5] %>%
wtd.cor()
cors = mpg[map_lgl(mpg, is.numeric)] %>%
wtd.cor()
#long format
cors_to_long = function(x, remove_lower = T, remove_diag = T) {
#remove as needed
if (remove_lower) x[lower.tri(x)] = NA
if (remove_diag) diag(x) = NA
#add var names
bind_cols(
value = as.numeric(x),
expand_grid(
Var2 = rownames(x),
Var1 = colnames(x)
)
) %>% filter(!is.na(value))
}
cor_mat_long = cors_to_long(cors$correlation)
cor_mat_p_long = cors_to_long(cors$p.value)
cor_mat %>%
GG_heatmap(reorder_vars = F) +
geom_text(data = cor_mat_p_long %>% filter(value > .05), aes(label = "X"), alpha = .2, size = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment