Advertisement
Guest User

myggcorrplot.r

a guest
Sep 6th, 2017
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. myggcorrplot <- function (corr, method = c("square", "circle"), type = c("full",
  2. "lower", "upper"), ggtheme = ggplot2::theme_minimal, title = "",
  3. show.legend = TRUE, legend.title = "Corr", show.diag = FALSE,
  4. colors = c("blue", "white", "red"), outline.color = "gray",
  5. hc.order = FALSE, hc.method = "complete", lab = FALSE, lab_col = "black",
  6. lab_size = 4, p.mat = NULL, sig.level = 0.05, insig = c("pch",
  7. "blank"), pch = 4, pch.col = "black", pch.cex = 5, tl.cex = 12,
  8. tl.col = "black", tl.srt = 45, lab.notsig="")
  9. {
  10. type <- match.arg(type)
  11. method <- match.arg(method)
  12. insig <- match.arg(insig)
  13. if (!is.matrix(corr) & !is.data.frame(corr))
  14. stop("Need a matrix or data frame!")
  15. corr <- as.matrix(corr)
  16. if (hc.order) {
  17. ord <- ggcorrplot:::.hc_cormat_order(corr)
  18. corr <- corr[ord, ord]
  19. if (!is.null(p.mat))
  20. p.mat <- p.mat[ord, ord]
  21. }
  22. if (type == "lower") {
  23. corr <- ggcorrplot:::.get_lower_tri(corr, show.diag)
  24. p.mat <- ggcorrplot:::.get_lower_tri(p.mat, show.diag)
  25. }
  26. else if (type == "upper") {
  27. corr <- ggcorrplot:::.get_upper_tri(corr, show.diag)
  28. p.mat <- ggcorrplot:::.get_upper_tri(p.mat, show.diag)
  29. }
  30. corr <- reshape2::melt(corr, na.rm = TRUE)
  31. corr$pvalue <- rep(NA, nrow(corr))
  32. corr$signif <- rep(NA, nrow(corr))
  33. if (!is.null(p.mat)) {
  34. p.mat <- reshape2::melt(p.mat, na.rm = TRUE)
  35. corr$coef <- corr$value
  36. corr$pvalue <- p.mat$value
  37. corr$signif <- as.numeric(p.mat$value <= sig.level)
  38. p.mat <- subset(p.mat, p.mat$value > sig.level)
  39. if (insig == "blank")
  40. corr$value <- corr$value * corr$signif
  41. }
  42. corr$abs_corr <- abs(corr$value) * 10
  43. p <- ggplot2::ggplot(corr, ggplot2::aes_string("Var1", "Var2",
  44. fill = "value"))
  45. if (method == "square")
  46. p <- p + ggplot2::geom_tile(color = outline.color)
  47. else if (method == "circle") {
  48. p <- p + ggplot2::geom_point(color = outline.color, shape = 21,
  49. ggplot2::aes_string(size = "abs_corr")) + ggplot2::scale_size(range = c(4,
  50. 10)) + ggplot2::guides(size = FALSE)
  51. }
  52. p <- p + ggplot2::scale_fill_gradient2(low = colors[1], high = colors[3],
  53. mid = colors[2], midpoint = 0, limit = c(-1, 1), space = "Lab",
  54. name = legend.title) + ggtheme() + ggplot2::theme(axis.text.x = ggplot2::element_text(angle = tl.srt,
  55. vjust = 1, size = tl.cex, hjust = 1), axis.text.y = ggplot2::element_text(size = tl.cex)) +
  56. ggplot2::coord_fixed()
  57. label <- as.character(round(corr[, "value"], 2))
  58. label[label=="0"] <- lab.notsig
  59. if (lab)
  60. p <- p + ggplot2::geom_text(ggplot2::aes_string("Var1",
  61. "Var2", label = "label"), color = lab_col, size = lab_size)
  62. if (!is.null(p.mat) & insig == "pch") {
  63. p <- p + ggplot2::geom_point(data = p.mat, ggplot2::aes_string("Var1",
  64. "Var2"), shape = pch, size = pch.cex, color = pch.col)
  65. }
  66. if (title != "")
  67. p <- p + ggplot2::ggtitle(title)
  68. if (!show.legend)
  69. p <- p + ggplot2::theme(legend.position = "none")
  70. p <- p + ggcorrplot:::.no_panel()
  71. p
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement