• R可视化:分组频率分布直方图和密度图


    介绍

    • ggplot2绘制分组频率分布直方图和密度图

    加载R包

    knitr::opts_chunk$set(message = FALSE, warning = FALSE)
    library(tidyverse)
    library(patchwork)
    library(ggpubr)
    library(rstatix)
    
    # rm(list = ls())
    options(stringsAsFactors = F)
    options(future.globals.maxSize = 10000 * 1024^2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Importing data

    inputdata <- data.frame(
      GroupA = c(rnorm(80, 4, 2), rnorm(120, 10, 3)),
      GroupB = c(rnorm(130, 4, 2), rnorm(70, 10, 3)))
    
    plotdata <- inputdata %>%
      tidyr::pivot_longer(
        cols = GroupA:GroupB,
        names_to = "variable",
        values_to = "value")
    
    head(plotdata)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    组间检验

    stat.test <- plotdata %>%
      t_test(value ~ variable) %>%
      adjust_pvalue(method = "bonferroni") %>%
      add_significance("p.adj")
    
    stat.test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    基础图

    绘制基础的直方图

    p1 <- plotdata %>% 
      dplyr::filter(variable == "GroupA") %>%
      ggplot(aes(x = value)) +
      geom_histogram(binwidth = 1, color = "black", fill = "#03B0AB", cex = 1) +
      geom_density(aes(value, y = after_stat(count)), color = "black",
                   fill = "#03B0AB", alpha = 0.2, cex = 1) +
      scale_x_continuous(expand = c(0, 0)) +
      scale_y_continuous(expand = c(0, 0), limits = c(0, 33)) +
      labs(x = NULL, y = "Count") +
      theme_classic(base_size = 15) +
      theme(axis.text = element_text(color = "black"))
    
    p2 <- plotdata %>% 
      dplyr::filter(variable == "GroupB") %>%
      ggplot(aes(x = value)) +
      geom_histogram(binwidth = 1, color = "black", fill = "#FB632E", cex = 1) +
      geom_density(aes(value, y = after_stat(count)), color = "black",
                   fill = "#FB632E", alpha = 0.2, cex = 1) +
      scale_x_continuous(expand = c(0, 0)) +
      scale_y_continuous(expand = c(0, 0), limits = c(0, 33)) +
      labs(x = NULL, y = NULL) +
      theme_classic(base_size = 15) +
      theme(axis.line.y = element_blank(),
            axis.text.y = element_blank(),
            axis.ticks.y = element_blank(),
            axis.text = element_text(color = "black"))
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    在这里插入图片描述

    修图

    • 添加显著性结果以及合并图形
    pl <- p1 + 
      geom_rect(aes(xmin = 12, 
                    xmax = 16, 
                    ymin = mean(inputdata$GroupA) + 3 * sd(inputdata$GroupA), 
                    ymax = mean(inputdata$GroupA) + 3.5 * sd(inputdata$GroupA)),
                color = "#03B0AB", fill = "transparent", cex = 1) +
      annotate("text", x = 13.5 , y = mean(inputdata$GroupA) + 3.2 * sd(inputdata$GroupA), 
               label = "GroupA", size = 3.5) +
      annotate("segment", x = mean(inputdata$GroupA), xend = mean(inputdata$GroupA), 
               y = 0, yend = 31,
               linetype = 2, cex = 1) +
      coord_cartesian(clip = "off", xlim = c(ceiling(min(inputdata$GroupA)) - 1, 
                                             ceiling(max(inputdata$GroupA)) + 1)) +
      theme(axis.title = element_text(size = 11, face = "bold"),
            axis.text = element_text(color = "black", size = 9)) +
      
      p2 +
      geom_rect(aes(xmin = 12,
                    xmax = 16,
                    ymin = mean(inputdata$GroupB) + 3 * sd(inputdata$GroupB), 
                    ymax = mean(inputdata$GroupB) + 3.5 * sd(inputdata$GroupB)),
                color = "#FB632E", fill = "transparent", cex = 1) +
      annotate("text",x = 13.5, y = mean(inputdata$GroupB) + 3.2 * sd(inputdata$GroupB), 
               label = "GroupB", size = 3.5) +
      annotate("segment", x = mean(inputdata$GroupB), xend = mean(inputdata$GroupB), 
               y = 0, yend = 31, linetype = 2, cex = 1) +
      xlab("Value") +
      theme(axis.title.x = element_text(hjust = -0.2)) +
      coord_cartesian(clip = "off", xlim = c(ceiling(min(inputdata$GroupB)) - 1, 
                                             ceiling(max(inputdata$GroupB)) + 1)) +
      annotate("segment", x = -2*mean(inputdata$GroupA), xend = mean(inputdata$GroupB), 
               y = 31, yend = 31, cex = 1) +
      annotate("text", x = -mean(inputdata$GroupA)+2, y = 32, size = 5, 
               label = paste0("p=", stat.test$p.adj)) +
      theme(axis.title = element_text(size = 11, face = "bold"),
            axis.text = element_text(color = "black", size = 9))
    
    pl
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    在这里插入图片描述

    systemic information

    devtools::session_info()
    
    • 1
    ─ Session info ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
     setting  value
     version  R version 4.3.3 (2024-02-29)
     os       macOS Sonoma 14.2
     system   aarch64, darwin20
     ui       RStudio
     language (EN)
     collate  en_US.UTF-8
     ctype    en_US.UTF-8
     tz       Asia/Shanghai
     date     2024-04-24
     rstudio  2023.12.1+402 Ocean Storm (desktop)
     pandoc   3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
    
    ─ Packages ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
     package     * version date (UTC) lib source
     abind         1.4-5   2016-07-21 [1] CRAN (R 4.3.0)
     backports     1.4.1   2021-12-13 [1] CRAN (R 4.3.0)
     broom         1.0.5   2023-06-09 [1] CRAN (R 4.3.0)
     cachem        1.0.8   2023-05-01 [1] CRAN (R 4.3.0)
     car           3.1-2   2023-03-30 [1] CRAN (R 4.3.0)
     carData       3.0-5   2022-01-06 [1] CRAN (R 4.3.0)
     cli           3.6.2   2023-12-11 [1] CRAN (R 4.3.1)
     colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.3.0)
     crayon        1.5.2   2022-09-29 [1] CRAN (R 4.3.0)
     devtools      2.4.5   2022-10-11 [1] CRAN (R 4.3.0)
     digest        0.6.35  2024-03-11 [1] CRAN (R 4.3.1)
     dplyr       * 1.1.4   2023-11-17 [1] CRAN (R 4.3.1)
     ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.3.0)
     evaluate      0.23    2023-11-01 [1] CRAN (R 4.3.1)
     fansi         1.0.6   2023-12-08 [1] CRAN (R 4.3.1)
     farver        2.1.1   2022-07-06 [1] CRAN (R 4.3.0)
     fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.3.0)
     forcats     * 1.0.0   2023-01-29 [1] CRAN (R 4.3.0)
     fs            1.6.3   2023-07-20 [1] CRAN (R 4.3.0)
     generics      0.1.3   2022-07-05 [1] CRAN (R 4.3.0)
     ggplot2     * 3.5.0   2024-02-23 [1] CRAN (R 4.3.3)
     ggpubr      * 0.6.0   2023-02-10 [1] CRAN (R 4.3.0)
     ggsignif      0.6.4   2022-10-13 [1] CRAN (R 4.3.0)
     glue          1.7.0   2024-01-09 [1] CRAN (R 4.3.1)
     gtable        0.3.4   2023-08-21 [1] CRAN (R 4.3.0)
     hms           1.1.3   2023-03-21 [1] CRAN (R 4.3.0)
     htmltools     0.5.8   2024-03-25 [1] CRAN (R 4.3.1)
     htmlwidgets   1.6.4   2023-12-06 [1] CRAN (R 4.3.1)
     httpuv        1.6.15  2024-03-26 [1] CRAN (R 4.3.1)
     knitr         1.45    2023-10-30 [1] CRAN (R 4.3.1)
     labeling      0.4.3   2023-08-29 [1] CRAN (R 4.3.0)
     later         1.3.2   2023-12-06 [1] CRAN (R 4.3.1)
     lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.3.1)
     lubridate   * 1.9.3   2023-09-27 [1] CRAN (R 4.3.1)
     magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.3.0)
     memoise       2.0.1   2021-11-26 [1] CRAN (R 4.3.0)
     mime          0.12    2021-09-28 [1] CRAN (R 4.3.0)
     miniUI        0.1.1.1 2018-05-18 [1] CRAN (R 4.3.0)
     munsell       0.5.0   2018-06-12 [1] CRAN (R 4.3.0)
     patchwork   * 1.2.0   2024-01-08 [1] CRAN (R 4.3.1)
     pillar        1.9.0   2023-03-22 [1] CRAN (R 4.3.0)
     pkgbuild      1.4.4   2024-03-17 [1] CRAN (R 4.3.1)
     pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.3.0)
     pkgload       1.3.4   2024-01-16 [1] CRAN (R 4.3.1)
     plyr          1.8.9   2023-10-02 [1] CRAN (R 4.3.1)
     profvis       0.3.8   2023-05-02 [1] CRAN (R 4.3.0)
     promises      1.2.1   2023-08-10 [1] CRAN (R 4.3.0)
     purrr       * 1.0.2   2023-08-10 [1] CRAN (R 4.3.0)
     R6            2.5.1   2021-08-19 [1] CRAN (R 4.3.0)
     Rcpp          1.0.12  2024-01-09 [1] CRAN (R 4.3.1)
     readr       * 2.1.5   2024-01-10 [1] CRAN (R 4.3.1)
     remotes       2.5.0   2024-03-17 [1] CRAN (R 4.3.1)
     reshape       0.8.9   2022-04-12 [1] CRAN (R 4.3.0)
     rlang         1.1.3   2024-01-10 [1] CRAN (R 4.3.1)
     rmarkdown     2.26    2024-03-05 [1] CRAN (R 4.3.1)
     rstatix     * 0.7.2   2023-02-01 [1] CRAN (R 4.3.0)
     rstudioapi    0.16.0  2024-03-24 [1] CRAN (R 4.3.1)
     scales        1.3.0   2023-11-28 [1] CRAN (R 4.3.1)
     sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.3.0)
     shiny         1.8.1   2024-03-26 [1] CRAN (R 4.3.1)
     stringi       1.8.3   2023-12-11 [1] CRAN (R 4.3.1)
     stringr     * 1.5.1   2023-11-14 [1] CRAN (R 4.3.1)
     tibble      * 3.2.1   2023-03-20 [1] CRAN (R 4.3.0)
     tidyr       * 1.3.1   2024-01-24 [1] CRAN (R 4.3.1)
     tidyselect    1.2.1   2024-03-11 [1] CRAN (R 4.3.1)
     tidyverse   * 2.0.0   2023-02-22 [1] CRAN (R 4.3.0)
     timechange    0.3.0   2024-01-18 [1] CRAN (R 4.3.1)
     tzdb          0.4.0   2023-05-12 [1] CRAN (R 4.3.0)
     urlchecker    1.0.1   2021-11-30 [1] CRAN (R 4.3.0)
     usethis       2.2.3   2024-02-19 [1] CRAN (R 4.3.1)
     utf8          1.2.4   2023-10-22 [1] CRAN (R 4.3.1)
     vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.3.1)
     withr         3.0.0   2024-01-16 [1] CRAN (R 4.3.1)
     xfun          0.43    2024-03-25 [1] CRAN (R 4.3.1)
     xtable        1.8-4   2019-04-21 [1] CRAN (R 4.3.0)
     yaml          2.3.8   2023-12-11 [1] CRAN (R 4.3.1)
    
     [1] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library
    
    ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    Reference

  • 相关阅读:
    ActiveMQ-基础知识
    单细胞分析:marker鉴定(11)
    Android JobScheduler介绍
    代码随想录动态规划——编辑距离总结篇
    网络中常用的请求方法
    C++自学总结
    打字速度测试,生成您的打字速度证书?
    vue 孙子组件获取祖先组件数据
    直播课堂系统06-搭建项目前端环境
    UE5——动画混合
  • 原文地址:https://blog.csdn.net/H20230717/article/details/138153381