【问题标题】:Combine correlation plot with coefficient table (ggplot2 -> ggstatsplot)将相关图与系数表相结合(ggplot2 -> ggstatsplot)
【发布时间】:2021-08-24 19:54:44
【问题描述】:

在使用 R 将表格与图组合在一张图像中时,您首选的技术是什么?我记得几个月前使用过 tableGrob() 以及拼凑或cowplot,但不记得细节了。

本示例使用 ggstatsplot 包。我想将相关系数添加到相关图(相关图)中。

if (!('ggstatsplot' %in% installed_packages)) {
    devtools::install_github('https://github.com/IndrajeetPatil/ggstatsplot')
}
needed_pkgs <- setdiff(c('ggstatsplot', 'statsExpressions',
                       'dplyr', 'nnet', 'MASS'),
                       installed_packages)
if (length(needed_pkgs) > 0) {
    install.packages(needed_pkgs)
}

library(ggstatsplot)
library(statsExpressions)
library(dplyr)
library(nnet)
library(MASS)
utils::example(topic = birthwt, echo = FALSE)

# model
bwt.mu <-
    nnet::multinom(
        formula = low ~ .,
        data = bwt,
        trace = FALSE
    )

original_cols <- colnames(bwt)

bwt.mu_coefstats <- ggcoefstats(x = bwt.mu, output = "tidy") %>% 
    # skipping first row = intercept
    slice(2:n()) %>% 
    dplyr::filter(term %in% original_cols) %>% 
    arrange(desc(p.value)) %>% 
    dplyr::select(term, estimate, p.value)

# Correlogram
cor_plot_out <- 
    ggstatsplot::ggcorrmat(bwt %>% dplyr::select(low, lwt, age))

想合并

bwt.mu_coefstats

cor_plot_out

【问题讨论】:

    标签: r ggplot2 cowplot patchwork


    【解决方案1】:

    关键元素是来自gridExtra 包的tableGrob()

    我们可以使用grid.arrange()

    对于表格,使用tableGrob() 创建一个类似于数据框图的表格。然后你可以将它与grid.arrange() 函数一起使用。

    library(gridExtra)
    
    bwt.mu_coefstats <- tableGrob(
        bwt.mu_coefstats,
        theme = ttheme_default(
          base_size = 10,
          base_colour = "grey25",
          parse = T
        ),
        rows = NULL
      )
    
    grid.arrange(cor_plot_out, bwt.mu_coefstats,
                 heights = c(10, 4))
    

    或拼凑:

    library(patchwork)
    cor_plot_out + bwt.mu_coefstats
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 2012-06-14
      相关资源
      最近更新 更多