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.

41 lines
1.4 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. mor<-mcor[order$y, order$x]
  7. mpval<-mpval[order$y, order$x]
  8. df<-mcor %>% as.data.frame()
  9. if (tri == "lower"){
  10. df[lower.tri(df, diag=T)]<-NA
  11. }
  12. df<-df %>% add_column(Var1=rownames(mcor),.before=1) %>%
  13. gather(Var2, Value, -Var1) %>% filter(!is.na(Value))
  14. df.pval<-mpval %>% as.data.frame()
  15. if (tri == "lower"){
  16. df.pval[lower.tri(df.pval, diag=T)]<-NA
  17. }
  18. df.pval<-df.pval %>% add_column(Var1=rownames(mpval),.before=1) %>%
  19. gather(Var2, Value, -Var1) %>% filter(!is.na(Value))
  20. df.pval$Value<-round(df.pval$Value, 3)
  21. if (stat=="signif"){
  22. df.pval$Value<-gtools::stars.pval(df.pval$Value)
  23. }
  24. df$Var1<-factor(df$Var1, levels=order$x)
  25. df$Var2<-factor(df$Var2, levels=order$y)
  26. ggplot(df, aes(Var1, Var2))+
  27. geom_tile(aes(fill=Value), color=color)+
  28. geom_text(data=df.pval, aes(label=Value), color="white")+
  29. scale_fill_gradientn(colors=col2(200))+
  30. theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust=0.5),
  31. panel.background = element_blank(),
  32. axis.ticks = element_blank())
  33. }