1. 安装circlize包:
install.packages('circlize')
2. 数据准备与读取:
-
数据准备后存于excel表格:
Samples percent
Sample1 12
Sample2 21
Sample3 13
Sample4 14
Sample5 76
Sample6 12
Sample7 86
Sample8 28
Sample9 36
-
读取数据:
library(readxl)
df1<-read_xls(file.choose())
df1
3. 绘制普通的bar图:
library(ggplot2)
ggplot(df1,aes(x=Samples,y=percent))+
geom_col(aes(fill=Samples))+
theme_bw()+coord_flip()+labs(x="Sample Names",y="Percentage")+
theme(legend.position = "none")
绘制环形柱状图视觉效果会更加! 所以如下将实现对应的环形柱状图!
4. 制作环形柱状图:
color = rev(rainbow(length(df1$percent))) # color of the bar
library(circlize)
circos.par("start.degree" = 90, cell.padding = c(0, 0, 0, 0))
circos.initialize("a", xlim = c(0, 100)) # 'a` just means there is one sector
circos.track(ylim = c(0.5, length(df1$percent)+0.5), track.height = 0.8,
bg.border = NA, panel.fun = function(x, y) {
xlim = CELL_META$xlim
circos.segments(rep(xlim[1],9), 1:9,# white line
rep(xlim[2], 9), 1:9,
col = "grey")
circos.rect(rep(0, 9), 1:9 - 0.2, df1$percent, 1:9 + 0.2,#bar between distance
col = color, border = "red")#bar border color
circos.text(rep(xlim[1], 9), 1:9,
paste(df1$Samples, " - ", df1$percent, "%"), # pasted data&name
facing = "downward", adj = c(1.05, 0.0), cex = 0.8) #adj text position
breaks = seq(0, 100, by = 10)# percentage breaks
circos.axis(h = "top", major.at = breaks, labels = paste0(breaks, "%"), labels.cex = 0.6)
})
解释:
- circos.initialize("a", xlim = c(0, 100)) # 'a` just means there is one sector
- circos.track(ylim = c(0.5, length(percent)+0.5), track.height = 0.8, bg.border = NA, panel.fun = function(x, y) { xlim = CELL_META$xlim
- 内侧细线条的颜色: circos.segments(rep(xlim[1],2), 1:2,# brown line circle
rep(xlim[2], 2), 1:2,
col = "brown") - 柱子之间的距离和边框的颜色: circos.rect(rep(0, 9), 1:9 - 0.2, percent, 1:9 + 0.2,#bar between distance
col = color, border = "red")#bar border color - 标签文字的调整: circos.text(rep(xlim[1], 5), 1:5, paste(category, " - ", percent, "%"), # pasted data&name
facing = "downward", adj = c(1.05, 0.0), cex = 0.8) #adj text position - 外圈的百分数显示与间隔: breaks = seq(0, 100, by = 10)# percentage breaks
- circos.axis(h = "top", major.at = breaks, labels = paste0(breaks, "%"), labels.cex = 0.6)
5. 调整的图形
-
图形1:
library(circlize)
circos.par("start.degree" = 90, cell.padding = c(0, 0, 0, 0))
circos.initialize("a", xlim = c(0, 100)) # 'a` just means there is one sector
circos.track(ylim = c(0.5, length(df1$percent)+0.5), track.height = 0.8,
bg.border = NA, panel.fun = function(x, y) {
xlim = CELL_META$xlim
circos.segments(rep(xlim[1],9), 1:9,# white line
rep(xlim[2], 9), 1:9,
col = "grey")
circos.rect(rep(0, 9), 1:9 - 0.45, df1$percent, 1:9 + 0.45,#bar distance
col = color, border = "blue")#bar color
circos.text(rep(xlim[1], 9), 1:9,
paste(df1$Samples, " - ", df1$percent, "%"),
facing = "downward", adj = c(1.05, 0.5), cex = 0.8)
breaks = seq(0, 85, by = 5)
circos.axis(h = "top", major.at = breaks, labels = paste0(breaks, "%"),
labels.cex = 0.6)
})
-
图形2:
library(circlize)
circos.par("start.degree" = 90, cell.padding = c(0, 0, 0, 0))
circos.initialize("a", xlim = c(0, 100)) # 'a` just means there is one sector
circos.track(ylim = c(0.5, length(df1$percent)+0.5), track.height = 0.8,
bg.border = NA, panel.fun = function(x, y) {
xlim = CELL_META$xlim
circos.segments(rep(xlim[1],9), 1:9,# white line
rep(xlim[2], 9), 1:9,
col = "#CCCCCC")
circos.rect(rep(0, 9), 1:9 - 0.45, df1$percent, 1:9 + 0.45,#bar distance
col = color, border = "blue")#bar color
circos.text(rep(xlim[1], 9), 1:9,
paste(df1$Samples, " - ", df1$percent, "%"),
facing = "downward", adj = c(1.05, 0.5), cex = 0.8)
breaks = seq(0, 85, by = 5)
circos.axis(h = "top", major.at = breaks, labels = paste0(breaks, "%"),
labels.cex = 0.6)
})
6. 生成参数再绘制:
-
生成对应的标签参数:
DT = paste0(df1$Samples, "_", 1:9)
percent = df1$percent
color = rev(rainbow(length(df1$percent)))
-
作图:
library(circlize)
circos.par("start.degree" = 90, cell.padding = c(0, 0, 0, 0))
circos.initialize("a", xlim = c(0, 100)) # 'a` just means there is one sector
circos.track(ylim = c(0.5, length(percent)+0.5), track.height = 0.8,
bg.border = NA, panel.fun = function(x, y) {
xlim = CELL_META$xlim
circos.segments(rep(xlim[1], 9), 1:9,
rep(xlim[2], 9), 1:9,
col = "#CCCCCC")
circos.rect(rep(0, 9), 1:9 - 0.45, percent, 1:9 + 0.45,
col = color, border = "white")
circos.text(rep(xlim[1], 9), 1:9,
paste(DT, " - ", percent, "%"),
facing = "downward", adj = c(1.05, 0.5), cex = 0.8)
breaks = seq(0, 85, by = 5)
circos.axis(h = "top", major.at = breaks, labels = paste0(breaks, "%"),
labels.cex = 0.6)
})
-
有斜度的标签:
library(circlize)
circos.par("start.degree" = 90, cell.padding = c(0, 0, 0, 0))
circos.initialize("a", xlim = c(0, 100)) # 'a` just means there is one sector
circos.track(ylim = c(0.5, length(percent)+0.5), track.height = 0.8,
bg.border = NA, panel.fun = function(x, y) {
xlim = CELL_META$xlim
circos.segments(rep(xlim[1], 9), 1:9,
rep(xlim[2], 9), 1:9,
col = "#CCCCCC")
circos.rect(rep(0, 9), 1:9 - 0.45, percent, 1:9 + 0.45,
col = color, border = "white")
circos.text(xlim[1] - mm_x(2, h = 1:9), 1:9,
paste(category, " - ", percent, "%"),
facing = "downward", adj = c(1, 0.5), cex = 0.8)
breaks = seq(0, 85, by = 5)
circos.axis(h = "top", major.at = breaks, labels = paste0(breaks, "%"),
labels.cex = 0.6)
})
-
参考:
https://jokergoo.github.io/circlize_book/book/high-level-plots.html#circular-barplot