Reppo for internal functions.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.6 KiB

  1. ggcorrplot<-function(df, var, color="#FFFFFF00", stat="signif", tri="all"){
  2. m.df<-df %>% spread(Cyt, Value) %>% select(-pats)
  3. mcor<-cor(m.df, m.df, use="pairwise.complete.obs") # Por defecto usa el método de Pearson.
  4. mpval<-Hmisc::rcorr(as.matrix(m.df))$P
  5. order<- mcor %>% as.data.frame() %>% add_column(Var1=rownames(mcor),.before=1) %>% clustsort
  6. mcor<-mcor[order$y, order$x]
  7. mpval<-mpval[order$y, order$x]
  8. df<-mcor %>% as.data.frame()
  9. if (tri == "upper"){
  10. df[lower.tri(df, diag=F)]<-NA
  11. }
  12. if (tri == "lower"){
  13. df[upper.tri(df, diag=F)]<-NA
  14. }
  15. df<-df %>% add_column(Var1=rownames(mcor),.before=1) %>%
  16. gather(Var2, Value, -Var1) %>% filter(!is.na(Value))
  17. df.pval<-mpval %>% as.data.frame()
  18. if (tri == "upper"){
  19. df.pval[lower.tri(df.pval, diag=F)]<-NA
  20. }
  21. if (tri == "lower"){
  22. df.pval[upper.tri(df.pval, diag=F)]<-NA
  23. }
  24. df.pval<-df.pval %>% add_column(Var1=rownames(mpval),.before=1) %>%
  25. gather(Var2, Value, -Var1) %>% filter(!is.na(Value))
  26. df.pval$Value<-round(df.pval$Value, 3)
  27. if (!stat %in% c("signif","none","pval")){stat<-"signif"}
  28. if (stat=="signif"){
  29. df.pval$Value<-gtools::stars.pval(df.pval$Value)
  30. }
  31. if (stat=="none"){
  32. df.pval$Value<-""
  33. }
  34. df$Var1<-factor(df$Var1, levels=order$x)
  35. df$Var2<-factor(df$Var2, levels=order$y)
  36. ggplot(df, aes(Var1, Var2))+
  37. geom_tile(aes(fill=Value), color=color)+
  38. geom_text(data=df.pval, aes(label=Value), color="white")+
  39. scale_fill_gradientn(colors=col2(200))+
  40. theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust=0.5),
  41. panel.background = element_blank(),
  42. axis.ticks = element_blank())
  43. }