ggplot2/ggpubr包:绘制配对箱线图、配对小提琴图和配对点图

数据和代码获取:请查看主页个人信息!!!

关键词“配对图”

大家好,今天我将介绍如何使用R语言ggplot2包和ggpubr包绘制分组/分面配对图的方法。

图片

配对图最大的优点在于可以展示相同样本处理前后对应关系:

图片

图片来源:Nat Commun:Clinical practices underlie COVID-19 patient respiratory microbiome composition and its interactions with the host

Step1:载入数据​​​​​​​

rm(list=ls())pacman::p_load(tidyverse,reshape2,ggpubr)# 实战data <- read.csv('data.csv')head(data)

图片

数据信息:ID是TCGA的barcode,Group是肿瘤与对照样本,paired是配对的barcode信息,Expression则是目标基因的表达量。

Step2:ggpubr包简单示例​​​​​​​

# ggpubr绘图示例ggpaired(ToothGrowth,          x = "supp",          y = "len",         color = "supp",          line.color = "gray",          line.size = 0.4,         palette = "jco") +  stat_compare_means(paired = TRUE)

图片

绘图实战:

Step3:ggpubr包绘制配对箱线图​​​​​​​

ggpaired(data, x = "Group", y = "Expression", color = "black", fill = "Group", line.size = 0.5) +  geom_line(aes(group = paired), color = "grey80") +  stat_compare_means(paired = TRUE, size = 2) +  facet_wrap(~ Type, scales = 'free_x', nrow = 2) +  ggsci::scale_fill_npg() +  ggsci::scale_color_npg() +  theme_classic2(base_size = 9) +  xlab('Expression')ggsave('pic1.png', width = 10, height = 4)

图片

Step4:ggplot2绘制配对箱线图​​​​​​​

facet_theme <- theme(strip.background = element_blank(),                     panel.spacing.x  = unit(0, "cm"), #x轴分面距离                     panel.spacing.y  = unit(0, "cm"),#y轴分面距离                     strip.text.x = element_text(size=7),                     strip.text.y = element_text(size=7, face="bold"),                     axis.text.x=element_text(angle=45,vjust=1, hjust=1),                     legend.position = 'none')
ggplot(data, aes(x = Group, y = Expression, color = Group)) +  geom_boxplot(outlier.size = 0.5) +  geom_line(aes(group = paired), color = "grey80", size = 0.5) +  geom_point(size = 0.5) +  stat_compare_means(comparisons = list(c("tumor", "normal")),                      paired = T, size = 2,label = "p.format",tip.length = 0) +  facet_wrap(~ Type, scales = 'free_y', nrow = 2) +  ggsci::scale_color_npg() +  theme_classic2(base_size = 9) +  facet_theme +  xlab('Expression')ggsave('pic2.png', width = 10, height = 4)

图片

这里优化了分面绘图主题,此外需要注意添加图层的顺序:

geom_boxplot → geom_line → geom_point;

geom_boxplot(outlier.size = 0.5) 和 geom_point(size = 0.5)

这两个函数中散点大小需要保持一致。

Step5:配对小提琴图​​​​​​​

ggplot(data, aes(x = Group, y = Expression, color = Group)) +  geom_violin(size = 1) +  geom_line(aes(group = paired), color = "grey80", size = 0.5) +  geom_point(size = 0.5, color = 'black') +  stat_compare_means(comparisons = list(c("tumor", "normal")),                      paired = T, size = 2,label = "p.format",tip.length = 0) +  facet_wrap(~ Type, scales = 'free_y', nrow = 2) +  ggsci::scale_color_npg() +  theme_classic2(base_size = 9) +  facet_theme +  xlab('Expression')ggsave('pic3.png', width = 10, height = 4)

这里的分组差异展示方式为p值:

图片

Step6:配对点图

ggplot(data, aes(x = Group, y = Expression, fill = Group, color = Group)) +  geom_line(aes(group = paired), color = "grey80", size = 0.5) +  geom_point(size = 1) +  stat_compare_means(comparisons = list(c("tumor", "normal")),                      paired = T, size = 2,label = "p.signif",tip.length = 0) +  facet_wrap(~ Type, scales = 'free_y', nrow = 2) +  scale_color_manual(values = c('red', 'black')) +  theme_classic2(base_size = 12) +  facet_themeggsave('pic4.png', width = 10, height = 4)

这里的分组差异展示方式为*号

  • ns: p > 0.05  *: p <= 0.05  **: p <= 0.01  ***: p <= 0.001  ****: p <= 0.0001

图片

数据和代码获取:请查看主页个人信息!!!

关键词“配对图”

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 绘制分组箱线图ggplot2 中常用的数据可视化方法之一。以下是用 R 语言和 ggplot2 绘制分组箱线图的步骤: 1. 导入需要绘制箱线图的数据集。例如,假设我们有一个名为 `data` 的数据集,其中含了两个因子变量 `group` 和 `category`,以及一个连续变量 `value`。 2. 使用 `ggplot()` 函数创建一个绘对象,并使用 `geom_boxplot()` 函数添加箱线图层。例如,代码如下: ``` library(ggplot2) ggplot(data, aes(x = group, y = value, fill = category)) + geom_boxplot() ``` 其中,`x` 参数指定箱线图中的因子变量,`y` 参数指定箱线图中的连续变量,`fill` 参数指定用于分组的因子变量。 3. 可以使用 `scale_fill_*()` 函数来更改填充颜色,其中 `*` 代表你想要的颜色变量,如 `scale_fill_brewer()`。例如: ``` ggplot(data, aes(x = group, y = value, fill = category)) + geom_boxplot() + scale_fill_brewer(palette = "Set1") ``` 其中 `palette` 参数指定所使用的调色板。 4. 可以添加其他元素来美化绘,如标题、坐标轴标签等等。例如: ``` ggplot(data, aes(x = group, y = value, fill = category)) + geom_boxplot() + scale_fill_brewer(palette = "Set1") + labs(title = "分组箱线图", x = "组别", y = "数值") ``` 其中,`labs()` 函数用于添加标签和标题。 以上是用 R 语言和 ggplot2 绘制分组箱线图的基本步骤。根据需要,你还可以进一步调整形的各个方面来满足你的需求。 ### 回答2: 要使用R语言ggplot2绘制分组箱线图,可以按照以下步骤进行操作: 步骤1:先安装和加载ggplot2。可以使用以下代码安装并加载ggplot2: install.packages("ggplot2") # 安装ggplot2 library(ggplot2) # 加载ggplot2 步骤2:准备数据。要绘制分组箱线图,需要准备含分组变量和数值变量的数据集。可以使用data.frame()函数创建数据框。例如: data <- data.frame(group = rep(c("A", "B", "C"), each = 100), value = rnorm(300, 0, 1)) 步骤3:使用ggplot()函数创建绘对象,并使用aes()函数设置x轴和y轴变量。例如: plot <- ggplot(data, aes(x = group, y = value)) 步骤4:使用geom_boxplot()函数添加箱线图层。例如: plot + geom_boxplot() 步骤5:可选的步骤,可以使用theme()函数和其他函数设置横轴标签、纵轴标签、标题等。例如: plot + geom_boxplot() + labs(x = "Group", y = "Value", title = "Grouped Boxplot") + theme(axis.text.x = element_text(angle = 45, hjust = 1)) 步骤6:运行代码并查看绘制的分组箱线图。 ### 回答3: 要使用R语言ggplot2绘制分组箱线图,首先需要安装并加载ggplot2。可以使用以下代码安装和加载ggplot2: install.packages("ggplot2") library(ggplot2) 接下来,需要准备数据集。数据集应含分组变量和数值变量。假设我们有一个含三个组别(A、B、C)和一个数值变量(score)的数据集df。数据集可以通过以下代码创建: df <- data.frame(group = rep(c("A", "B", "C"), each = 100), score = rnorm(300, mean = 50, sd = 10)) 接下来,使用ggplot函数创建一个基础层,并使用geom_boxplot函数添加箱线图层。使用aes函数将组别变量映射到x轴,将数值变量映射到y轴。设置fill参数来区分不同组别的箱线图。 ggplot(df, aes(x = group, y = score)) + geom_boxplot(fill = "lightblue") 然后,可以通过添加其他美化选项来自定义形。例如,可以使用theme函数更改表的主题,可以使用labs函数添加标题和轴标签。 ggplot(df, aes(x = group, y = score)) + geom_boxplot(fill = "lightblue") + theme_minimal() + labs(title = "分组箱线图", x = "组别", y = "分数") 最后,使用print函数打印形。 print(ggplot(df, aes(x = group, y = score)) + geom_boxplot(fill = "lightblue") + theme_minimal() + labs(title = "分组箱线图", x = "组别", y = "分数"))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值