美文网首页
R语言之Circlize绘制累计图

R语言之Circlize绘制累计图

作者: Oodelay | 来源:发表于2019-04-07 23:44 被阅读0次

数据准备

library('statnet')
library('circlize')

df<-read.table("taxa_L2.txt", header=T,row.names = 1)  #核磁数据,行为有机碳成分,列为样品
df= t(df)
map = read.table('map.txt', row.names = 1, header = T) #样点分组信息,行为样品,列为分组属性
df= df[rownames(map),] #数据匹配,同时对样品进行排序

library('dplyr')

metadata = cbind(df,map[,2:3]) %>%  #将两文件合并
  aggregate(.~ Week*Site,data = ., mean) #分组求均值

1.1 选取一组样点进行分析

CS = subset(metadata,Site == 'CS')
rownames(CS) = CS$Week
CS = CS[,-(1:2)]

mat1<-as.matrix(CS) # 矩阵化

# 手动设置行列名(可选)
# rownames(my.data) <-c("CCK", "CNPK", "GCCK", "GCNPK")
# colnames(my.data) <-c("Alphaproteobacteria","Betaproteobacteria","Gammaproteobacteria",
#                       "Deltaproteobacteria","Acidobacteria","Actinobacteria",
#                       "Bacteroidetes","Chloroflexi","Firmicutes",  
#                       "Gemmatimonadetes","Planctomycetes","Thaumarchaeota" ,
#                       "Verrucomicrobia","Ascomycota",  "Basidiomycota", 
#                       "Zygomycota") 

1.2 颜色设定

grid.col = NULL

# 定义处理的颜色,这里随便选取了4个颜色,大家可以根据自己的喜好制定好看的配色
grid.col[c("W1", "W2", "W4", "W8","W16")] = c("blue", "black", "orange", "chocolate","pink") 

# 定义微生物各个门的颜色,
grid.col[colnames(mat1)] = c("lavender", "khaki","mistyrose", 
                                "sienna1", "skyblue", "brown1","maroon")

1.3 初步成图

# jpeg(file="circlize.jpg", width=4000, height=3000, pointsize=8,units = 'px',res = 600) 
#jpeg()与dev.off()合用,可将图直接导出而不在绘图区展示

# 参数设置
circos.par(gap.degree = c(rep(2, nrow(mat1)-1), 10, rep(2, ncol(mat1)-1), 10), #设置各个扇叶的间距
           start.degree = -95) #设置起始位置,0处于时钟3点处

# 出图,本人这里只用了少部分参数,所有参数见此包的help文档,或者看下文
chordDiagram(mat1,
             order = c('W1','W2','W4','W8','W16',
                       'Alkyl_C','O-CH3/NCH','O-alkyl_C','O-C-O_anomeric_C',
                       'Aromatic_C','Aromatic_C-O','Carbonyl'),
             annotationTrack = c('grid','name','axis'), #依次为外环的阴影,名称和外围坐标轴,可选择显示或者不显示
             directional = TRUE,
             diffHeight = 0,
             grid.col = grid.col, 
             transparency = 0.5) 

# 图例制作
legend("topright",pch=20,legend=colnames(mat1),
       col=grid.col[colnames(mat1)],bty="n",
       cex=1,pt.cex=3,border="black",title = 'NMR',title.col = 'red') # 设定核磁成分的图例

legend("topleft",pch=20,legend= c('1','2','4','8','16'),
       col=grid.col[c('W1','W2','W4','W8','W16')],bty="n",
       cex=1,pt.cex=3,border="black",title = 'Week',title.col = 'blue') # 设定样品分组图例

abline(v = 0, lty = 2, lwd = 2, col = "#00000080") #添加横线,将样品和核磁成分区分开

circos.clear() #清空设置
# 绘图结束后写入文件
# dev.off()

我们目前预览的图看,图例和图区严重重叠。是因为绘图区空间的限制。我们只需在图片输出时调置图片宽大于高即可解决。


2.1 针对所有样点

rownames(metadata) = paste0(rep(c('CQ','CS','YT'),each = 5),c(1,16,2,4,8))
mat = as.matrix(metadata[,-(1:2)])

####颜色设定
grid.col = NULL

# 定义处理的颜色,这里随便选取了5个颜色,大家可以根据自己的喜好制定好看的配色
grid.col[rownames(metadata)] = rep(c("blue", "black", "orange", "chocolate","pink"),3) 

# 定义微生物各个门的颜色,
grid.col[colnames(mat)] = c("lavender", "khaki","mistyrose", 
                                "sienna1", "skyblue", "brown1","maroon")

# 定义各组件的间隔,同样点的week间距离较小,不同样点距离较大
circos.par(gap.after = c(rep(c(rep(1, nrow(mat)/3-1), 8), 3),rep(1,ncol(mat)-1),8),
           start.degree = 0) #定义起始点的位置,90为时钟12点的位置,逆时针排序

2.2 成图

# 若需输出图片,则取消jpeg() 和 dev.off()的注释符
#jpeg(file="circlize-2.jpg", width=4000, height=3000, pointsize=8,units = 'px',res = 600)

chordDiagram(mat, 
             annotationTrack = c('grid','axis'),
             directional = TRUE,
             diffHeight = 0,
             grid.col = grid.col, 
             transparency = 0.5,
             # 定义出图顺序,从时钟的3点出开始
             order = c(paste0(rep(c('CQ','CS','YT'),each = 5),c(1,2,4,8,16)),
                       'Alkyl_C','O-CH3/NCH','O-alkyl_C','O-C-O_anomeric_C',
                       'Aromatic_C','Aromatic_C-O','Carbonyl'),
            
             preAllocateTracks = list(
               track.height = uh(4, "mm"),
               track.margin = c(uh(4, "mm"), 0))
             )

# 图例制作
legend(x = 1, y = 1,pch=20,legend=colnames(mat), 
       col=grid.col[colnames(mat)],bty="n",text.font = 2,
       cex=1,pt.cex=3,title = 'NMR',title.col = 'red',title.adj = 0) # 设定图例

# 注意这里的颜色顺序和grid.col[rownames(mat)]中相对应
legend(x = 1,y =  -0.3,pch=15,
       legend= c('1','2','4','8','16'),
       col=c("blue", "orange", "chocolate","pink","black"),
       bty="n",text.font = 2,cex=1,
       pt.cex=3,pt.bg = 'white',title = 'Week',title.col = 'blue',title.adj = 0) # 设定图例

abline(h = 0.05, lty = 2, lwd = 2, col = "#00000080")

2.3 在圆环外围添加扇形进行分组

#定义后续图层叠加在圆环的第二层进行
circos.track(track.index = 2, panel.fun = function(x, y) {
  sector.index = get.cell.meta.data("sector.index")
  xlim = get.cell.meta.data("xlim")
  ylim = get.cell.meta.data("ylim")
  # circos.text(mean(xlim), mean(ylim), sector.index, cex = 0.6, niceFacing = TRUE)
}, bg.border = NA)

# 在外围添加扇形进行分组
highlight.sector(paste0('CS',c(1,2,4,8,16)), track.index = 1, col = "red", 
                 text = "CS", cex = 0.8, text.col = "black",font= 2,niceFacing = TRUE)
highlight.sector(paste0('CQ',c(1,2,4,8,16)), track.index = 1, col = "green", 
                 text = "CQ", cex = 0.8, text.col = "black",font= 2,niceFacing = TRUE)
highlight.sector(paste0('YT',c(1,2,4,8,16)), track.index = 1, col = "blue", 
                 text = "YT", cex = 0.8, text.col = "black", font= 2,niceFacing = TRUE)
highlight.sector(colnames(mat), track.index = 1, col = "purple", 
                 text = "Carbon Composition", cex = 1, font= 2,text.col = "black",niceFacing = TRUE)

circos.clear() #回复出厂设置

#dev.off()

除了颜值高点,看着都费劲啊,可读性不如堆积图
这不是科学应有之道,不可盲目追求花里胡哨的莫须有,而应求真求实。

相关文章

网友评论

      本文标题:R语言之Circlize绘制累计图

      本文链接:https://www.haomeiwen.com/subject/ethyiqtx.html