ggplot 绘制小提琴图+箱线图+统计检验

文章参考:


实战

1.加载数据:

options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor")
options("repos"=c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
rm(list = ls())  ## 魔幻操作,一键清空~
options(stringsAsFactors = F)
load('GSE9257_lasso.Rdata')
###提取IL13RA2, CDH3 and COMP
load("myoverlap.Rdata")
rt=as.data.frame(t(exprSet[myoverlap,]))
rt$group=ifelse(meta_GSE9257_lasso$event==1,"IPF","Control")
x=colnames(rt)[4]

2.数据准备:

###加载需要的包
library(reshape2)
library(tidyr)
library(ggplot2)
library(ggpubr)
###数据转换:
#把数据转换成gglpot2输入文件
data=melt(rt,id.vars=c("group")) ##宽数据变长数据
colnames(data)=c("group","Gene","Expression")
head(data)

3.做图

###绘制图形
#绘制boxplot
p=ggboxplot(data, x="Gene", y="Expression", color = "group", 
            ylab="Gene expression",
            xlab="",
            legend.title=x,
            palette = c("blue","red"),
            width=0.6, add = "none")
p=p+rotate_x_text(60)
p1=p+stat_compare_means(aes(group=group),
                        method="t.test",
                        symnum.args=list(cutpoints = c(0, 0.001, 0.01, 0.05, 1), 
                                         symbols = c("***", "**", "*", " ")),
                        label = "p.signif")
#输出图片
#pdf(file=outFile, width=6, height=5)
print(p1)

小提琴图:

# plot
ggplot(data,aes(x = Gene, y = Expression,fill = group)) +
  # 小提琴图层
  geom_violin(position = position_dodge(0.9),alpha = 0.5,
              width = 1.2,trim = T,
              color = NA) +
  # 箱线图图层
  geom_boxplot(width = .2,show.legend = F,
               position = position_dodge(0.9),
               color = 'grey20',alpha = 0.5,
               outlier.color = 'grey50') +
  # 主题调整
  theme_bw(base_size = 14) +
  theme(axis.text.x = element_text(angle = 0,color = 'black'),#,hjust = 1
        legend.position = 'top',
        aspect.ratio = 0.8) + ##图形长宽比例
  # 颜色设置
  scale_fill_manual(values = c('Control'='#398AB9','IPF'='red'),
                    name = '') +
  # 添加显著性标记
  stat_compare_means(aes(group=group),
                     method="t.test",
                     symnum.args=list(cutpoints = c(0, 0.001, 0.01, 0.05, 1),
                                      symbols = c("***", "**", "*", "NS")),label = "p.signif",
                     label.y = 8,size = 5) +
  #ylim(0.5,3.5)
  ###添加标题
  labs(x="GSE9257")+
  theme(axis.title.x = element_text(color="black", size=12, face="bold")
  )

###添加标题:
p +labs(title="Plot of length \n by dose",
 x ="Dose (mg)", y = "Teeth length")+
 theme(
 plot.title = element_text(color="red", size=12, face="bold.italic"),
 axis.title.x = element_text(color="blue", size=12, face="bold"),
 axis.title.y = element_text(color="#993333", size=12, face="bold")
 )
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。