生信分析流程R语言做生信生物信息学

9 可视化数据探索 R作图ggplot

2019-05-02  本文已影响27人  陈宇乔
exprSet['GAPDH',]
exprSet['ACTB',]
boxplot(exprSet,las=2)

ggplot2 探索数据


if(T){
  gene_expression<- as.data.frame(exprSet['COL11A1',])
  gene_expression$group<- group_list
  exprSet_L<- melt(gene_expression)
  names(exprSet_L)[2]<- c('sample')
  p=ggplot(exprSet_L,aes(x=sample,y=value,fill=group))+geom_boxplot()
  print(p)}
logFC_cutoff <- with(DEG,mean(abs( logFC)) + 2*sd(abs( logFC)) )
DEG$change = as.factor(ifelse(DEG$P.Value < 0.05 & abs(DEG$logFC) > logFC_cutoff,
                              ifelse(DEG$logFC > logFC_cutoff ,'UP','DOWN'),'NOT')

做成合并的图

gene_name<- c('PLAU','SPP1','BGN','NDC80','BUB1B','KIF2C','AURKB','BUB1','CXCL1','CXCL10','CXCL8','MMP9','CDC6','MCM10','MCM2')
library(reshape2)

if(T){
  gene_expression<- as.data.frame(t(exprSet[gene_name,]))
  match(colnames(exprSet),phe$submitter_id.samples)
  gene_expression$group<- factor(phe$group_list,levels = c('tumor','normal'))
  # gene_expression$samlple<- rownames(gene_expression)
  exprSet_L<- melt(gene_expression,id.vars = c('group'))
  names(exprSet_L)[2]<- c('sample')
  p=ggplot(exprSet_L,aes(x=sample,y=value,fill=group))+geom_boxplot()+ stat_compare_means(method = "wilcox.test",label="p.signif")
  ggsave('./figure/multi_gene_ggplot.pdf', p)
  print(p)
}

exprSet_L 最终效果
# # Example 2
# #::::::::::::::::::::::::::::::::::::::::::
# ToothGrowth
# class(ToothGrowth)
# ggpaired(ToothGrowth, x = "supp", y = "len",
#          color = "supp", line.color = "gray", 
#          facet.by = "dose",
#          line.size = 0.4,
#          palette = "npg")
#######################################  单基因的表达
rm(list = ls())

load(file = './Rdata/step0.Rdata')
load(file = './Rdata/@step00_idtransed.Rdata')

exprSet[1:4,1:4]

########### 探索数据 配对数据
# gene_name<- c('PLAU','SPP1','BGN','NDC80')
# gene_name<- c('BUB1B','KIF2C','AURKB','BUB1','CXCL1','CXCL10','CXCL8','MMP9','CDC6','MCM10','MCM2')
gene_name<- c('PLAU','SPP1','BGN','NDC80','BUB1B','KIF2C','AURKB','BUB1','CXCL1','CXCL10','CXCL8','MMP9','CDC6','MCM10','MCM2')
library(ggpubr)
library(ggplot2)
library(reshape2)
if(T){
  gene_expression<- as.data.frame(t(exprSet[gene_name,]))
  match(colnames(exprSet),sample_id$V1)
  gene_expression$group<- factor(sample_id$V2)
  gene_expression$ID<- sample_id$V6
  # gene_expression$samlple<- rownames(gene_expression)
  exprSet_L<- melt(gene_expression,id.vars = c('group','ID'))
  names(exprSet_L)[3]<- c('gene')
  exprSet_L<- exprSet_L[order(exprSet_L$group),]
  # ID 在数据框中,才能保证正确排序
  # 排序这一步很重要
  p=ggpaired(exprSet_L, x="group", y="value", color = "group", 
             facet.by = "gene",
             line.color = "gray", 
             line.size = 0.4, palette = "jco")+ 
    stat_compare_means(paired = TRUE,method = "wilcox.test",label="p.signif")
  print(p)
}
ggsave('./figure/ggplot_boxplot_paired_test.pdf',p,width = 20, height = 60, units = "cm")

?ggplot
?ggsave



最终效果

image.png

添加注释

https://www.shixiangwang.top/post/ggpubr-add-pvalue-and-siglevels/
http://www.sthda.com/english/wiki/comparing-means-in-r
https://zhuanlan.zhihu.com/p/27491381

image.png
上一篇下一篇

猜你喜欢

热点阅读