I made a partial correlation analysis, with the ggm package
list = list(mtcars, mtcars)
list = lapply(list, function(x) x %>%
mutate(gear = as.factor(gear)))
library(ggm)
lapply(list, function(x) {
sapply(split(x, x$gear), function(x) {
pcor(u = c('mpg', 'disp', 'hp', 'vs'), S = var(x))
})
})
and with the pcor package
pcorr1 = list %>%
map(function(x) split(x[c('mpg', 'disp', 'hp', 'vs')], x$gear))
coeff = c("pearson", "spearman")
res = lapply(1:2, function(x) lapply(seq(coeff), function(x) {
lapply(pcorr1[[x]], function(y) pcor(y, method = coeff[[x]]))}))
Can anyone recommend a way how to compute such correlation in a graph with ggplot2?
Thanks
UPFATE Just to make understand I am wondering wether it is possible to use the correlation coefficients as y and on x all level of grouping variable (it should be a sort of barplot)
I'm having trouble understanding your expected output, but perhaps you could use pairs plots to show the correlation between variables and label each plot with your
ggm::pcor()value? E.g.Created on 2023-09-05 with reprex v2.0.2
Or is this totally off the mark?
NB. it's better to avoid using the word
listas a name for your list, and also it looks like you have 'duplicate' plots in themap()output, but I think it's because you havelist(mtcars, mtcars)as your input.Edit 1
Based on your update, it sounds like you want a barplot of the pcor values? What do you want your barplot to look like? I.e. how would you change this:
Created on 2023-09-08 with reprex v2.0.2