前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >R语言circlize包画一幅好看的弦图~完整示例数据和代码

R语言circlize包画一幅好看的弦图~完整示例数据和代码

作者头像
用户7010445
发布2021-07-12 15:47:12
发布2021-07-12 15:47:12
2.6K00
代码可运行
举报
运行总次数:0
代码可运行

前些天在网上看到的代码,他最终的出图如下

image.png

这份教程的链接地址是 https://www.royfrancis.com/beautiful-circos-plots-in-r/

但是他没有公布完整的数据,只是提到了数据的格式,我试着按照他提到的数据自己模拟了一份数据

首先是最外圈染色体的长度信息

因为只有三条染色体,数据不多,直接通过代码输入

代码语言:javascript
代码运行次数:0
复制
circos.initialize(factors=c("genome_1","genome_2","genome_3"),
                  xlim=matrix(c(0,0,0,100,120,110),ncol=2))
第二圈的覆盖度信息

image.png

第三圈的GC含量

image.png

第四圈的基因名称

image.png

第五圈共线性的片段

image.png

image.png

教程中提到了两套代码,一套是不带参数调整细节

完整代码如下

代码语言:javascript
代码运行次数:0
复制

'''
### 弦图
### 参考链接
### https://www.royfrancis.com/beautiful-circos-plots-in-r/
'''
library(readxl)
cov<-read_excel("beautiful_plot/circlize/example_df.xlsx",
                sheet = "Sheet1")
cov
gc<-read_excel("beautiful_plot/circlize/example_df.xlsx",
               sheet = "Sheet2")
gc

ann<-read_excel("beautiful_plot/circlize/example_df.xlsx",
                sheet = "Sheet3")
ann

nuc1<-read_excel("beautiful_plot/circlize/example_df.xlsx",
                 sheet = "Sheet4")
nuc1

nuc2<-read_excel("beautiful_plot/circlize/example_df.xlsx",
                 sheet = "Sheet5")
nuc2


library(circlize)


col_text <- "grey40"
#circos.par("track.height"=0.8,gap.degree=5,cell.padding=c(0,0,0,0))
circos.initialize(factors=c("genome_1","genome_2","genome_3"),
                  xlim=matrix(c(0,0,0,100,120,110),ncol=2))
circos.track(ylim=c(0,1),panel.fun=function(x,y) {
  chr=CELL_META$sector.index
  xlim=CELL_META$xlim
  ylim=CELL_META$ylim
  circos.text(mean(xlim),mean(ylim),chr)
})

circos.track(track.index = get.current.track.index(),
             panel.fun = function(x, y) {
               circos.axis(h="top")
               })

circos.genomicTrack(data=cov,
                    panel.fun=function(region,value,...) {
                      circos.genomicLines(region,value)
})
# coverage y axis
circos.yaxis()

circos.track(factors=gc$chr,
             x=gc$start,
             y=gc$value1,
             panel.fun=function(x,y) {
               circos.lines(x,y)
               })
# gc y axis
circos.yaxis()

circos.genomicLabels(ann,labels.column=5)
circos.genomicLink(nuc1,nuc2)
circos.clear()

出图

image.png

还有一套代码是带有参数对图进行美化的
代码语言:javascript
代码运行次数:0
复制

'''
### 弦图
### 参考链接
### https://www.royfrancis.com/beautiful-circos-plots-in-r/
'''

library(readxl)
cov<-read_excel("beautiful_plot/circlize/example_df.xlsx",
                sheet = "Sheet1")
cov
gc<-read_excel("beautiful_plot/circlize/example_df.xlsx",
               sheet = "Sheet2")
gc

ann<-read_excel("beautiful_plot/circlize/example_df.xlsx",
                sheet = "Sheet3")
ann

nuc1<-read_excel("beautiful_plot/circlize/example_df.xlsx",
                 sheet = "Sheet4")
nuc1

nuc2<-read_excel("beautiful_plot/circlize/example_df.xlsx",
                 sheet = "Sheet5")
nuc2


library(circlize)

col_text <- "grey40"
circos.par("track.height"=0.8,gap.degree=5,cell.padding=c(0,0,0,0))
circos.initialize(factors=c("genome_1","genome_2","genome_3"),
                  xlim=matrix(c(0,0,0,100,120,110),ncol=2))
circos.track(ylim=c(0,1),panel.fun=function(x,y) {
  chr=CELL_META$sector.index
  xlim=CELL_META$xlim
  ylim=CELL_META$ylim
  circos.text(mean(xlim),mean(ylim),chr,cex=0.5,col=col_text,
              facing="bending.inside",niceFacing=TRUE)
},bg.col="grey90",bg.border=F,track.height=0.06)

brk <- c(0,20,40,60,80,100,120)
circos.track(track.index = get.current.track.index(), 
             panel.fun = function(x, y) {
               circos.axis(h="top",
                           major.at=brk,
                           labels=brk,
                           labels.cex=0.4,
                           col=col_text,
                           labels.col=col_text,
                           lwd=0.7,
                           labels.facing="clockwise")
               },
             bg.border=F)
circos.genomicTrack(data=cov,
                    panel.fun=function(region,value,...) {
                      circos.genomicLines(region,
                                          value,
                                          type="l",
                                          col="grey50",
                                          lwd=0.6)
                      circos.segments(x0=0,
                                      x1=120,
                                      y0=100,
                                      y1=100,
                                      lwd=0.6,
                                      lty="11",
                                      col="grey90")
                      circos.segments(x0=0,
                                      x1=120,
                                      y0=150,
                                      y1=150,
                                      lwd=0.6,
                                      lty="11",
                                      col="grey90")
  #circos.segments(x0=0,x1=max(ref$V2),y0=500,y1=500,lwd=0.6,lty="11",col="grey90")
                      },
  track.height=0.08,
  bg.border=F)
circos.yaxis(at=c(100,150),
             labels.cex=0.25,
             lwd=0,
             tick.length=0,
             labels.col=col_text,
             col="#FFFFFF")

circos.track(factors=gc$chr,
             x=gc$start,
             y=gc$value1,
             panel.fun=function(x,y) {
               circos.lines(x,y,col="grey50",lwd=0.6)
               circos.segments(x0=0,
                               x1=120,
                               y0=30,
                               y1=30,
                               lwd=0.6,
                               lty="11",
                               col="grey90")
               circos.segments(x0=0,
                               x1=120,
                               y0=50,
                               y1=50,
                               lwd=0.6,
                               lty="11",
                               col="grey90")
               circos.segments(x0=0,
                               x1=150,
                               y0=70,
                               y1=70,
                               lwd=0.6,
                               lty="11",
                               col="grey90")
               },
             ylim=c(30,70),
             track.height=0.08,
             bg.border=F)
# gc y axis
circos.yaxis(at=c(30,50,70),
             labels.cex=0.25,
             lwd=0,
             tick.length=0,
             labels.col=col_text,
             col="#FFFFFF")

circos.genomicLabels(ann,
                     labels.column=5,
                     cex=0.25,
                     col=col_text,
                     line_lwd=0.5,
                     line_col="grey80",
                     side="inside",
                     connection_height=0.05,
                     labels_height=0.04)
rcols <- scales::alpha(ifelse(sign(nuc1$start-nuc1$end)!=sign(nuc2$start-nuc2$end),"#f46d43","#66c2a5"),alpha=0.4)
rcols
circos.genomicLink(nuc1,nuc2,col=rcols,border=NA)

circos.clear()

image.png

这个表示覆盖度和gc含量的折线数据比较少,看起来可能不太美观,换成自己的数据多了以后就好看了

示例数据和代码可以直接在公众号后台留言 20210617获取

欢迎大家关注我的公众号

小明的数据分析笔记本

小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 首先是最外圈染色体的长度信息
  • 第二圈的覆盖度信息
  • 第三圈的GC含量
  • 第四圈的基因名称
  • 第五圈共线性的片段
  • 还有一套代码是带有参数对图进行美化的
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档