qqboxplot--实现Q-Q plot和箱型图的整合!

2021
07/27

+
分享
评论
作图丫
A-
A+

qqboxplot作为ggplot的扩展,可以实现q-q箱线图的绘制。

 
背景介绍




箱形图(Box-plot)又称为盒式图或箱线图,是一种用作显示一组  数据分散情况资料的统计图。它主要用于反映原始数据分布的特征,还可以进行多组数据分布特征的比较。QQplot也就是Quantile-Quantile Plots。是通过比较两个概率分布的分位数对这两个概率分布进行比较的概率图方法。

 今天小编给大家介绍的qqboxplot,正是整合了这两类图形,将Q-Q plot的尾部信息合并到传统箱线图中,并显示尾部的置信区间,qqboxplot对于大型数据集具有更高的可靠性。

 
R包安装






BiocManager::install("qqboxplot")library(qqboxplot)
 
可视化介绍




01  
比较箱线图、q-q图和 q-q箱线图  

使用来自一名自闭症患者和一名对照患者的随机基因样本,对表达counts值取对数进行统计。
library(ggplot2)library(dplyr)#设置统一文本大小eltext <- 12#q-q boxplotqqbox <- expression_data %>%  ggplot(aes(specimen, log_count)) + geom_qqboxplot(varwidth = TRUE, notch = TRUE) + ylab('logged normalized expression') + ggtitle("c) q-q boxplot") + theme(plot.title=element_text(size=eltext, face="plain", hjust=0.5), axis.title.x = element_text(size=eltext), axis.title.y = element_text(size=eltext), panel.border = element_blank(), panel.background = element_rect(fill="white"), panel.grid.major = element_line(colour = "grey70"), panel.grid.minor = element_line(colour="grey80"))
#常规箱型图box <- expression_data %>% ggplot(aes(specimen, log_count)) + geom_boxplot(varwidth = TRUE, notch = TRUE) + ylab('logged normalized expression') + ggtitle('a) boxplot') + theme(plot.title=element_text(size=eltext, face="plain", hjust=0.5), axis.title.x = element_text(size=eltext), axis.title.y = element_text(size=eltext), panel.border = element_blank(), panel.background = element_rect(fill="white"), panel.grid.major = element_line(colour = "grey70"), panel.grid.minor = element_line(colour="grey80"))
override.shape <- c(16, 17)#q-q plotqq <- expression_data %>% ggplot(aes(sample=log_count)) + geom_qq(aes(color=specimen, shape=specimen)) + xlab('theoretical normal distribution') + ylab('logged normalized expression') + ggtitle('b) q-q plot') + labs(color="specimen") + guides(color = guide_legend(override.aes = list(shape=override.shape)), shape=FALSE) + theme(plot.title=element_text(size=eltext, face="plain", hjust=0.5), axis.title.x = element_text(size=eltext), axis.title.y = element_text(size=eltext), panel.border = element_blank(), panel.background = element_rect(fill="white"), panel.grid.major = element_line(colour = "grey70"), panel.grid.minor = element_line(colour="grey80"), legend.position = c(0.8, 0.2))
library(gridExtra)#合并图片gridExtra::grid.arrange(box, qq, qqbox, ncol=3)


02  
示例  

模拟数据展示



tibble(y=c(rnorm(1000, mean=2), rt(1000, 16), rt(500, 4),  rt(1000, 8), rt(1000, 32)), group=c(rep("normal, mean=2", 1000),  rep("t distribution, df=16", 1000),  rep("t distribution, df=4", 500),  rep("t distribution, df=8", 1000),  rep("t distribution, df=32", 1000)))

使用模拟数据绘制箱型图:



simulated_data %>% ggplot(aes(factor(group, levels=c("normal, mean=2", "t distribution, df=32", "t distribution, df=16", "t distribution, df=8", "t distribution, df=4")), y=y)) + geom_boxplot(notch=TRUE, varwidth = TRUE) + xlab(NULL) + ylab(NULL) + theme(axis.text.x = element_text(angle = 23, size = 15), axis.title.y = element_text(size=15), panel.border = element_blank(), panel.background = element_rect(fill="white"),        panel.grid = element_line(colour = "grey70"))


使用同一组数据绘制QQ-plot


override.shape <- c(16, 17, 15, 3, 7)
simulated_data %>% ggplot(aes(sample=y, color=factor(group, levels=c("normal, mean=2", "t distribution, df=32", "t distribution, df=16", "t distribution, df=8", "t distribution, df=4")), shape=factor(group, levels=c("normal, mean=2", "t distribution, df=32", "t distribution, df=16", "t distribution, df=8", "t distribution, df=4")))) + geom_qq() + geom_qq_line() + labs(color="distribution") + xlab("Normal Distribution") + ylab("Simulated Datasets") + guides(color = guide_legend(override.aes = list(shape=override.shape)), shape=FALSE) + theme(axis.title.x = element_text(size=15), axis.title.y = element_text(size=15), panel.border = element_blank(), panel.background = element_rect(fill="white"),        panel.grid = element_line(colour = "grey70"))


使用同一组数据绘制Q-Q boxplot



simulated_data %>% ggplot(aes(factor(group, levels=c("normal, mean=2", "t distribution, df=32", "t distribution, df=16", "t distribution, df=8", "t distribution, df=4")), y=y)) + geom_qqboxplot(notch=TRUE, varwidth = TRUE, reference_dist="norm") + xlab("reference: normal distribution") + ylab(NULL) + guides(color=FALSE) + theme(axis.text.x = element_text(angle = 23, size = 15), axis.title.y = element_text(size=15), axis.title.x = element_text(size=15), panel.border = element_blank(), panel.background = element_rect(fill="white"),        panel.grid = element_line(colour = "grey70"))

使用qqboxplot中自带的一些示例数据绘图


comparison_data <- indicators %>% filter(year==2008 & `Series Code`=="SL.TLF.ACTI.1524.MA.NE.ZS")
indicators %>%  #将series名称中的标签更改为较短的标题  mutate(`Series Name`= ifelse( `Series Name`=="Labor force participation rate for ages 15-24, male (%) (national estimate)", "Male ages 15-24", "Female ages 15-24")) %>% ggplot(aes(as.factor(year), y=indicator))+ geom_qqboxplot(notch=TRUE, varwidth = TRUE, compdata=comparison_data$indicator) + xlab("Year") + ylab("Country level labor force\nparticipation rate (%)") + facet_wrap(~factor(`Series Name`, levels = c("Male ages 15-24", "Female ages 15-24"))) + theme(strip.text = element_text(size=12), axis.text.x = element_text(size = 15), axis.title.x = element_text(size=15), axis.title.y = element_text(size=12), panel.border = element_blank(), panel.background = element_rect(fill="white"),        panel.grid = element_line(colour = "grey70"))


spike_data %>% filter(region=="V1") %>% ggplot(aes(factor(orientation), nspikes)) + geom_qqboxplot(notch=TRUE, varwidth = TRUE, reference_dist="norm") + xlab("orientation") + ylab("spike count") + theme(axis.text.x = element_text(size = 15), axis.text.y = element_text(size=14), axis.title.x = element_text(size=15), axis.title.y = element_text(size=15), panel.border = element_blank(), panel.background = element_rect(fill="white"),        panel.grid = element_line(colour = "grey70"))


同样的数据用bean plot展示


spike_data %>% filter(region=="V1") %>% ggplot()+ geom_violin(aes(x=factor(orientation),y=nspikes),fill='grey',trim=F, draw_quantiles = c(.25, .5, .75))+ geom_segment(aes( x=match(factor(orientation),levels(factor(orientation)))-0.1, xend=match(factor(orientation),levels(factor(orientation)))+0.1, y=nspikes,yend=nspikes), col='black' ) + xlab("orientation") + ylab("spike count") + theme(axis.text.x = element_text(size = 15), axis.text.y = element_text(size=14), axis.title.x = element_text(size=15), axis.title.y = element_text(size=15), panel.border = element_blank(), panel.background = element_rect(fill="white"), panel.grid = element_line(colour = "grey70"))


 
小编总结




qqboxplot在箱型图和Q-Q图的结合上做了非常好的尝试,作为ggplot的扩展包,内部的函数也是大家比较熟悉的一些,上手还是非常快的!

 

不感兴趣

看过了

取消

本文由“健康号”用户上传、授权发布,以上内容(含文字、图片、视频)不代表健康界立场。“健康号”系信息发布平台,仅提供信息存储服务,如有转载、侵权等任何问题,请联系健康界(jkh@hmkx.cn)处理。
关键词:
整合,实现,箱型

人点赞

收藏

人收藏

打赏

打赏

不感兴趣

看过了

取消

我有话说

0条评论

0/500

评论字数超出限制

表情
评论

为你推荐

推荐课程


社群

  • 医生交流群 加入
  • 医院运营群 加入
  • 医技交流群 加入
  • 护士交流群 加入
  • 大健康行业交流群 加入

精彩视频

您的申请提交成功

确定 取消
剩余5
×

打赏金额

认可我就打赏我~

1元 5元 10元 20元 50元 其它

打赏

打赏作者

认可我就打赏我~

×

扫描二维码

立即打赏给Ta吧!

温馨提示:仅支持微信支付!