Hi there!!! I want help regarding the correlation heatmap generation showing the significance level within the heatmap cells. I want to show the P-value <0.05 as "" and P-value <0.001 as "*". For that, I am following the script from here. You can find the script under the header "Stars instead of p values:" (also, I have added the codes at the end of this query). When I run with my data ("my_data") I am facing an error message
"Error in FUN(X[[i]], ...) : object 'Method' not found".
Can you please tell me how should I run the code and where am I doing a mistake? Please bear with me if it is a silly question as I am not an expert with R.
Thanks, dpc
CODE:
library(Hmisc)
library(car)
mydf=df
set.seed(12345)
mydf[,2:5] = sapply(mydf[,2:5],jitter,amount=15)
mydf=mydf[c(1:10,20:29,39:48,58:67),]
# calculate r
c = rcorr(as.matrix(mydf[sapply(mydf,is.numeric)]))$r
# calculate p values
p = rcorr(as.matrix(mydf[sapply(mydf,is.numeric)]))$P
plots <- dlply(mydf, .(Method), function (x1) {
ggplot(data.frame(subset(melt(rcorr(as.matrix(x1[sapply(x1,is.numeric)]))$r)[lower.tri(c),],Var1 != Var2),
pvalue=Recode(subset(melt(rcorr(as.matrix(x1[sapply(x1,is.numeric)]))$P)[lower.tri(p),],Var1 != Var2)$value , "lo:0.01 = '***'; 0.01:0.05 = '*'; else = ' ';")),
aes(x=Var1,y=Var2,fill=value)) +
geom_tile(aes(fill = value),colour = "white") +
geom_text(aes(label = sprintf("%1.2f",value)), vjust = 0) +
geom_text(aes(label = pvalue), vjust = 1) +
theme_bw() +
scale_fill_gradient2(name="R^2",midpoint=0.25,low = "blue", high = "red") +
xlab(NULL) +
ylab(NULL) +
theme(axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
panel.border=element_blank()) +
ggtitle(x1$Method) + theme(plot.title = element_text(lineheight=1,face="bold")) +
geom_text(data = subset(melt(rcorr(as.matrix(x1[sapply(x1,is.numeric)]))$r),Var1==Var2),
aes(label=Var1),vjust=1 )
})
grid.arrange(plots$Single_ROI + theme(legend.position='none'),
plots$Simple_2_ROI + theme(legend.position='none'),
plots$WIG_Method + theme(legend.position='none'),
plots$WIG_drawn_bg + theme(legend.position='none'),
ncol=2,
nrow=2)
Hi,
The error is because you do not have the
Method
object. Try to use the Rcorrplot
package: https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.htmlI think it does what you want and it is fairly well documented.
António