首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将置信区间值添加到GGally:ggpairs图

将置信区间值添加到GGally:ggpairs图
EN

Stack Overflow用户
提问于 2021-08-19 14:44:46
回答 1查看 42关注 0票数 0

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

从http://handlesman.blogspot.com/2011/03/matrix-plot-with-confidence-intervals.html绘制

编辑:

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-08-19 15:30:28

解决了!

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

代码语言:javascript
复制
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))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68849987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档