临床上经常会碰到要观察给予一种干预(如药物、手术等)前后变化的情况,在这种情形下绘制前后受试者相关指标的变化就显得很有必要了。
- 通过ggpaired函数实现(来自ggpaired函数自带的示例)
Example 1
library(ggpubr)
before <-c(200.1, 190.9, 192.7, 213, 241.4, 196.9, 172.2, 185.5, 205.2, 193.7)
after <-c(392.9, 393.2, 345.1, 393, 434, 427.9, 422, 383.9, 392.3, 352.2)
d <- data.frame(before = before, after = after)
ggpaired(d, cond1 = "before", cond2 = "after",
fill = "condition", palette = "jco")
ggpaired_1.png
Example 2
ggpaired(ToothGrowth, x = "supp", y = "len",
color = "supp", line.color = "gray", line.size = 0.4,
palette = "npg")
ggpaired_2.png
- 单纯使用ggplot2实现(示例数据来源于简书作者@纪伟讲测序)
library(ggplot2)
dat <- read.delim('MAP2.txt')
dat$MAP2 <- log(dat$MAP2, 2)
head(dat) # Group1相当于标签,用于分组;Group2是配对的样本,用于连线
image.png
ggplot(dat, aes(x = group1, y = MAP2)) +
geom_boxplot(aes(fill = group1), show.legend = FALSE, width = 0.6) +
geom_point() +
geom_line(aes(group = group2), color = 'gray', lwd = 0.5) +
scale_fill_manual(values = c('#FE7280', '#AC88FF')) +
theme(panel.grid = element_blank(),
axis.line = element_line(colour = 'black', size = 1),
panel.background = element_blank(),
plot.title = element_text(size = 20, hjust = 0.5),
plot.subtitle = element_text(size = 15, hjust = 0.5)) +
theme(axis.text = element_text(size = 20, color = 'black'),
axis.title = element_text(size = 20, color = 'black')) +
labs(x = '', y = 'expression of MAP2', title = 'MAP2',
subtitle = 'Tumor vs Normal p = 1.33E-06 (DESeq2)')
image.png
附:ggplot中直线的类型:
image.png
参考资料:
@一个人旅行- boxplot图添加连线(R实现)
简书作者@纪伟讲测序:R语言绘制配对样品箱线图
网友评论