【问题标题】:add confidence interval values to GGally:ggpairs plot将置信区间值添加到 GGally:ggpairs 图
【发布时间】:2021-10-21 06:55:42
【问题描述】:

是否可以将置信区间值添加到GGally:ggpairs 图中?认为可以通过GGally::ggpairs(data, upper = list(continuous = ggcorr)) 调整版本的ggcorr(),但我一直无法理解内部代码。理想情况下,[this][1] 情节的GGally::ggpairs 版本将是理想的。

[图来自 http://handlesman.blogspot.com/2011/03/matrix-plot-with-confidence-intervals.html][2]

编辑:

这是我最接近的尝试,但无法调整矩阵中每个单元格的注释位置。

my_high <- function(data, mapping, ...){
  
  p <- ggally_cor(data, mapping = mapping)+annotate("text",x=0,y=0,label=paste0("[",round(cor.test(data[,1],data[,2],method = "pearson",conf.int=0.95)$conf.int[1],3),",",round(cor.test(data[,1],data[,2],method = "pearson",conf.int=0.95)$conf.int[2],3),"]"))
  return(p)
}

GGally::ggpairs(data, upper = list(continuous = my_high))```


  [1]: https://i.stack.imgur.com/XWNyb.png
  [2]: https://i.stack.imgur.com/7j2Qb.png

【问题讨论】:

    标签: r ggplot2 correlation confidence-interval ggally


    【解决方案1】:

    解决了!

    您需要手动调整GGally::ggally_cor()函数。

    ggally_cor_New<-
    function (data, mapping, ..., stars = TRUE, method = "pearson", 
              use = "complete.obs", display_grid = FALSE, digits = 3, title_args = list(...), 
              group_args = list(...), justify_labels = "right", align_percent = 0.5, 
              title = "Corr", alignPercent = warning("deprecated. Use `align_percent`"), 
              displayGrid = warning("deprecated. Use `display_grid`")) 
    {
      if (!missing(alignPercent)) {
        warning("`alignPercent` is deprecated. Please use `align_percent` if alignment still needs to be adjusted")
        align_percent <- alignPercent
      }
      if (!missing(displayGrid)) {
        warning("`displayGrid` is deprecated. Please use `display_grid`")
        display_grid <- displayGrid
      }
      na.rm <- if (missing(use)) {
        NA
      }
      else {
        (use %in% c("complete.obs", "pairwise.complete.obs", 
                    "na.or.complete"))
      }
      ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, 
                       align_percent = align_percent, display_grid = display_grid, 
                       title_args = title_args, group_args = group_args, justify_labels = justify_labels, 
                       justify_text = "left", sep = if ("colour" %in% names(mapping)) 
                         ": "
                       else ":\n", title = title, text_fn = function(x, y) {
                         if (GGally:::is_date(x)) {
                           x <- as.numeric(x)
                         }
                         if (GGally:::is_date(y)) {
                           y <- as.numeric(y)
                         }
                         corObj <- stats::cor.test(x, y, method = method, 
                                                   use = use)
                         cor_est <- as.numeric(corObj$estimate)
                         cor_txt <- formatC(cor_est, digits = digits, format = "f")
                         if (isTRUE(stars)) {
                           cor_txt <- str_c(cor_txt, signif_stars(corObj$p.value))
                           cor_CI <- as.numeric(corObj$conf.int)
                           cor_txt2 <- formatC(cor_CI, digits = digits, format = "f")
                           cor_txt <- str_c(cor_txt, paste0("[",cor_txt2[1],',',cor_txt2[2],"]"),sep="\n")
                         }
                         cor_txt
                       })
    }
    
    
    GGally::ggpairs(data, upper = list(continuous = ggally_cor_New))
    

    【讨论】:

      猜你喜欢
      • 2013-10-19
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-24
      • 1970-01-01
      • 2019-05-07
      相关资源
      最近更新 更多