shell散点图_两种方法批量绘制散点图

这篇博客介绍了如何利用R语言在Shell环境中批量绘制基因表达矩阵与数学变量(math)的散点图,通过两种方法:直接绘制和使用facet分面。博客中提供了完整的代码示例,包括数据模拟、数据处理和图形生成,帮助读者理解如何分析基因表达数据的相关性。
摘要由CSDN通过智能技术生成

小技巧分享。

模拟数据

就模拟一个基因表达矩阵吧,列是基因,有10个基因,然后行是样本,这10个基因在100个样本的表达量。

expr=rnorm(1000)

dim(expr)=c(100,10)

colnames(expr)=LETTERS[1:10]

ITH_genes=LETTERS[1:10]

math=rbinom(10)

有一个变量是math值,也是100个样本的一个指标,然后就可以看10个基因分别和这个math的散点图,看相关性。

使用

这个时候表达矩阵是宽的

box

dat=data.frame(gene=as.numeric(expr[,i]),

math=math)

head(dat)

## 画boxplot

library(ggpubr)

ggscatter(dat, x = "gene", y = "math",

color = 'black', shape = 21, size = 0.5, # Points color, shape and size

add = "reg.line", # Add regressin line

add.params = list(color = "blue", fill = "lightgray"), # Customize reg. line

conf.int = TRUE, # Add confidence interval

cor.coef = TRUE, # Add correlation coefficient. see ?stat_cor

xlab = i,

cor.coeff.args = list(method = "pearson", label.sep = "\n")

)

})

library(cowplot)

plot_grid(plotlist=box, ncol=5 )

使用facet分面

这个时候宽表达矩阵,需要折叠起来,变成长的数据形式才能绘图。

library(reshape2)

dat=melt(expr)

dat[,1]=rep(math,100)

colnames(dat)=c('math','gene','value')

p=ggscatter(dat, y= "math", x = "value", shape = 21, size = 0.5,

add = "reg.line", # Add regressin line

add.params = list(color = "blue", fill = "lightgray"), # Customize reg. line

conf.int = TRUE, # Add confidence interval

cor.coef = TRUE, # Add correlation coefficient. see ?stat_cor

cor.coeff.args = list(method = "pearson" , label.sep = "\n")

)

facet(p, facet.by = "gene" )

全部代码

expr=rnorm(1000)

dim(expr)=c(100,10)

colnames(expr)=LETTERS[1:10]

ITH_genes=LETTERS[1:10]

math=rbinom(10)

box

dat=data.frame(gene=as.numeric(expr[,i]),

math=math)

head(dat)

## 画boxplot

library(ggpubr)

ggscatter(dat, x = "gene", y = "math",

color = 'black', shape = 21, size = 0.5, # Points color, shape and size

add = "reg.line", # Add regressin line

add.params = list(color = "blue", fill = "lightgray"), # Customize reg. line

conf.int = TRUE, # Add confidence interval

cor.coef = TRUE, # Add correlation coefficient. see ?stat_cor

xlab = i,

cor.coeff.args = list(method = "pearson", label.sep = "\n")

)

})

library(cowplot)

plot_grid(plotlist=box, ncol=5 )

library(reshape2)

dat=melt(expr)

dat[,1]=rep(math,100)

colnames(dat)=c('math','gene','value')

p=ggscatter(dat, y= "math", x = "value", shape = 21, size = 0.5,

add = "reg.line", # Add regressin line

add.params = list(color = "blue", fill = "lightgray"), # Customize reg. line

conf.int = TRUE, # Add confidence interval

cor.coef = TRUE, # Add correlation coefficient. see ?stat_cor

cor.coeff.args = list(method = "pearson" , label.sep = "\n")

)

facet(p, facet.by = "gene" )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值