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

介绍

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

加载R包

{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)

Importing data

{r, 复制代码
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)

组间检验

{r} 复制代码
stat.test <- plotdata %>%
  t_test(value ~ variable) %>%
  adjust_pvalue(method = "bonferroni") %>%
  add_significance("p.adj")

stat.test

基础图

绘制基础的直方图

{r, 复制代码
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"))

修图

  • 添加显著性结果以及合并图形
{r, 复制代码
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

systemic information

{r} 复制代码
devtools::session_info()
markdown 复制代码
─ 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

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Reference

相关推荐
岁月如歌,青春不败1 小时前
HMSC联合物种分布模型
开发语言·人工智能·python·深度学习·r语言
赵钰老师15 小时前
结构方程模型【SEM】:非线性、非正态、交互作用及分类变量分析
分类·数据挖掘·数据分析·r语言
Tiger Z21 小时前
R 语言科研绘图第 11 期 --- 柱状图-基础
开发语言·程序人生·r语言·贴图
岁月如歌,青春不败21 小时前
R语言森林生态系统结构、功能与稳定性分析与可视化
开发语言·人工智能·python·深度学习·r语言
pk_xz1234562 天前
R 和 Origin 完成细菌 OTU 表、土壤理化性质数据的微生物 Beta 多样性分析
算法·机器学习·r语言
乄北城以北乀2 天前
第1章 R语言中的并行处理入门
开发语言·分布式·r语言
生信圆桌3 天前
【生信圆桌x教程系列】如何安装 seurat V4版本R包
开发语言·r语言
赵钰老师3 天前
基于R语言APSIM模型应用及批量模拟(精细农业、水肥管理、气候变化、粮食安全、土壤碳周转、环境影响、农业可持续性、农业生态等)
开发语言·数据分析·r语言
赵钰老师4 天前
【R语言遥感技术】“R+遥感”的水环境综合评价方法
开发语言·数据分析·r语言
生信圆桌4 天前
【生信圆桌x教程系列】如何安装 seurat V5版本R包,最详细安装手册
开发语言·r语言