circlize 绘图小问题解答
小问题
之前有小伙伴在绘制环形图时遇到一些小问题:
图形怎么简洁一点? 怎么去掉每个扇区的边框? 怎么根据不同的扇区给扇区里的图形不同的颜色? 怎么看 peak 在基因组上的密度分布?
然后大概总结一下:
我们先随机产生一些 bed
格式的数据:
# 加载R包
library(circlize)
# 设定种子序列
set.seed(123)
# 生成数据
bed = generateRandomBed(nr = 2000,nc = 5)
head(bed)
chr start end value1 value2 value3 value4 value5
1 chr1 643242 7057416 -0.87661868 -0.44797391 -0.07767267 -0.5848076 0.8275879
2 chr1 7634457 9175071 0.04966380 -1.03537554 0.28414431 -0.7210173 0.3378812
3 chr1 9204434 9853594 -0.28592503 0.07506007 0.50533898 0.5271611 -0.5371033
...
先画一圈染色体图:
# 默认染色体
circos.initializeWithIdeogram()
circos.initializeWithIdeogram()
函数有 plotType 参数来控制显示的内容,我们可以去掉刻度,看起来就会简洁一些了:
# 去掉刻度
circos.initializeWithIdeogram(plotType = c("ideogram", "labels"))
默认绘制一圈 散点图 ,注意默认加载的是 hg19 的基因组:
# 默认散点图
circos.initializeWithIdeogram(plotType = c("ideogram", "labels"))
circos.genomicTrack(bed, numeric.column = 4,
panel.fun = function(region, value, ...) {
circos.genomicPoints(region, value)
})
可以看到是空心的黑色圆,我们用 pch
、cex
和 col
参数调整:
# 调整点大小和形状、颜色
circos.genomicTrack(bed, numeric.column = 5,
panel.fun = function(region, value, ...) {
circos.genomicPoints(region, value,
# 形状
pch = 16,
# 点大小
cex = 0.3,
col = '#29BB89')
})
pch 是形状参数,每个数字代表的图形如下:
那么怎么把每个扇区的边框去掉呢?有个 bg.border
参数可以设置:
# 去掉扇区边框
circos.initializeWithIdeogram(plotType = c("ideogram", "labels"))
circos.genomicTrack(bed, numeric.column = 5,
panel.fun = function(region, value, ...) {
circos.genomicPoints(region, value,
# 形状
pch = 16,
# 点大小
cex = 0.3,
col = '#29BB89')
})
circos.genomicTrack(bed, numeric.column = 5,bg.border = NA,
panel.fun = function(region, value, ...) {
circos.genomicPoints(region, value,
# 形状
pch = 18,
# 点大小
cex = 0.5,
col = '#FF7A00')
})
相比于没有去掉边框的是不是看起来更加简洁一些了。此外还可以使用 bg.col 参数设置扇区背景颜色:
# 设置背景颜色
circos.genomicTrack(bed, numeric.column = 5,bg.border = NA,bg.col = 'grey90',
panel.fun = function(region, value, ...) {
circos.genomicPoints(region, value,
# 形状
pch = 18,
# 点大小
cex = 0.5,
col = '#B8B5FF')
})
那么怎么给每个扇区给不同的颜色呢?不知道小伙伴们有没有记得我们之前讲过的,panel.fun 函数就像是一个 循环函数
,根据扇区分类,来一个个加上图形的,所以只需要给 col 每次循环不一样的颜色即可,可以使用 随机生成颜色的函数 ,每次生成一个随机颜色:
看看用法:
Usage
rand_color(n, hue = NULL, luminosity = "random", transparency = 0)
Arguments
n
number of colors # 生成颜色的数量
hue # 颜色色系
the hue of the generated color. You can use following default color name: red, orange, yellow, green, blue, purple, pink and monochrome. If the value is a hexidecimal color string such as #00FFFF, the function will extract its hue value and use that to generate colors.
luminosity # 饱和度
controls the luminosity of the generated color. The value should be a string containing bright, light, dark and random.
transparency # 透明度
transparency, numeric value between 0 and 1.
我们用循环生成 10 个颜色,类似于放到 panel.fun 里面:
rand_color(10)
[1] "#9B6450FF" "#17EAB0FF" "#AB4ED1FF" "#C73AADFF" "#2C4F70FF" "#7D7F72FF"
[7] "#89893BFF" "#A8A2A2FF" "#374A4BFF" "#05700AFF"
# 循环生成
library(plyr)
ldply(1:10,rand_color,n = 1)
V1
1 #783E3DFF
2 #471917FF
3 #E6B9B7FF
4 #2B1F1FFF
5 #2B2120FF
6 #FCA89FFF
7 #B14C3EFF
8 #5B2B24FF
9 #959493FF
10 #F4917DFF
测试一下,弄个浅色和深色,透明度一半的:
# 给扇区分类颜色
circos.initializeWithIdeogram(plotType = c("ideogram", "labels"))
# 浅色
circos.genomicTrack(bed, numeric.column = 5,
panel.fun = function(region, value, ...) {
circos.genomicPoints(region, value,
# 形状
pch = 16,
# 点大小
cex = 0.5,
col = rand_color(1,luminosity = 'light',transparency = 0.5))
})
# 深色
circos.genomicTrack(bed, numeric.column = 5,
panel.fun = function(region, value, ...) {
circos.genomicPoints(region, value,
# 形状
pch = 16,
# 点大小
cex = 0.5,
col = rand_color(1,luminosity = 'dark',transparency = 0.5))
})
我们还可以把颜色名称输出来方便以后使用:
# 同时输出颜色名称
circos.genomicTrack(bed, numeric.column = 5,
panel.fun = function(region, value, ...) {
col = rand_color(1,luminosity = 'light',transparency = 0.5)
print(col)
circos.genomicPoints(region, value,
# 形状
pch = 16,
# 点大小
cex = 0.5,
col = col)
})
[1] "#F3B9FD80"
[1] "#F180A180"
[1] "#C4ABFA80"
[1] "#FEEAC380"
[1] "#6BCBEB80"
[1] "#F775CE80"
[1] "#8DFAAC80"
[1] "#F8B77C80"
[1] "#789CF880"
[1] "#716BE780"
[1] "#F883CD80"
[1] "#AAFDE780"
[1] "#BCF68180"
[1] "#FAE28680"
[1] "#DCC4FD80"
[1] "#F7848880"
[1] "#FBC3FC80"
[1] "#FCE09D80"
[1] "#FFE4C980"
[1] "#E8F17980"
[1] "#D5FEA680"
[1] "#F293E780"
[1] "#F9B9F080"
[1] "#E5C7FF80"
最后怎么可视化 peak 在基因组上的 密度分布 呢?peak 是一个个有染色体,起始位置和终止位置构造起来的 区域 ,我们用 矩形图
就可以:
# 密度分布
circos.initializeWithIdeogram(plotType = c("ideogram", "labels"))
# 红色
circos.genomicTrack(bed, numeric.column = 7,bg.border = NA,
panel.fun = function(region, value, ...) {
circos.genomicRect(region, value, col = '#CF0000', border = NA, ...)
})
# 蓝色
circos.genomicTrack(bed, numeric.column = 8,bg.border = NA,
panel.fun = function(region, value, ...) {
circos.genomicRect(region, value, col = '#0061A8', border = NA, ...)
})
# 黑色
circos.genomicTrack(bed, numeric.column = 6,bg.border = NA,
panel.fun = function(region, value, ...) {
circos.genomicRect(region, value, col = 'black', border = NA, ...)
})
代码 我上传到 QQ 群 老俊俊生信交流群
文件夹里。欢迎加入。加我微信我也拉你进 微信群聊 老俊俊生信交流群
哦。
群二维码:
老俊俊微信:
所以今天你学习了吗?
欢迎小伙伴留言评论!
今天的分享就到这里了,敬请期待下一篇!
最后欢迎大家分享转发,您的点赞是对我的鼓励和肯定!
如果觉得对您帮助很大,赏杯快乐水喝喝吧!
推 荐 阅 读