上篇博客,我們講到了使用R包繪制不超過五組數(shù)據(jù)集的維恩圖,末尾留了個尾巴:就是數(shù)據(jù)集超過五組數(shù)據(jù)集怎么繪制呢,當(dāng)然可以選擇繪制花瓣圖進行數(shù)據(jù)的分析和可視化距帅。下面我就帶了大家使用R繪制花瓣圖!
廢話不多說括堤,直接上R繪圖代碼:
測試數(shù)據(jù)文件內(nèi)容長這樣碌秸!需要的請在該博文下面留言聯(lián)系我!
## 1.導(dǎo)入R包(R包如果沒有安裝悄窃,導(dǎo)入前需要安裝讥电,使用install.packages("R包名稱")進行安裝)
library(rio)
library(plotrix)
## 2.讀取數(shù)據(jù)
venn_data_index <- list.files(path = "./", pattern = "^venn_data")
venn_data <- import(venn_data_index)
sample_id <- colnames(venn_data)
otu_id <- unique(venn_data[,1])
otu_id <- otu_id[otu_id != '']
core_otu_id <- otu_id
otu_num <- length(otu_id)
## 3.遍歷數(shù)據(jù)文件獲取數(shù)據(jù)
for (i in 2:ncol(venn_data)) {
? otu_id <- unique(venn_data[,i])
? otu_id <- otu_id[otu_id != '']
? core_otu_id <- intersect(core_otu_id, otu_id)
? otu_num <- c(otu_num, length(otu_id))
}
core_num <- length(core_otu_id)
## 4.定義備選顏色(示例數(shù)據(jù)20個樣本,所以我設(shè)置了20種顏色)
ellipse_col <- c('#6181BD4E','#F348004E','#64A10E4E','#9300264E','#464E044E','#049a0b4E','#4E0C664E','#D000004E','#FF6C004E','#FF00FF4E','#c7475b4E','#00F5FF4E','#BDA5004E','#A5CFED4E','#f0301c4E','#2B8BC34E','#FDA1004E','#54adf54E','#CDD7E24E','#9295C14E')
## 5.創(chuàng)建繪圖函數(shù):flower_plot()
flower_plot <- function(sample, otu_num, core_otu, start, a, b, r, ellipse_col, circle_col) {
? par( bty = 'n', ann = F, xaxt = 'n', yaxt = 'n', mar = c(1,1,1,1))
? plot(c(0,10),c(0,10),type='n')
? n? <- length(sample)
? deg <- 360 / n
? res <- lapply(1:n, function(t){
? ? draw.ellipse(x = 5 + cos((start + deg * (t - 1)) * pi / 180),
? ? ? ? ? ? ? ? y = 5 + sin((start + deg * (t - 1)) * pi / 180),
? ? ? ? ? ? ? ? col = ellipse_col[t],
? ? ? ? ? ? ? ? border = ellipse_col[t],
? ? ? ? ? ? ? ? a = a, b = b, angle = deg * (t - 1))
? ? text(x = 5 + 2.5 * cos((start + deg * (t - 1)) * pi / 180),
? ? ? ? y = 5 + 2.5 * sin((start + deg * (t - 1)) * pi / 180),
? ? ? ? otu_num[t])
? ? if (deg * (t - 1) < 180 && deg * (t - 1) > 0 ) {
? ? ? text(x = 5 + 3.3 * cos((start + deg * (t - 1)) * pi / 180),
? ? ? ? ? y = 5 + 3.3 * sin((start + deg * (t - 1)) * pi / 180),
? ? ? ? ? sample[t],
? ? ? ? ? srt = deg * (t - 1) - start,
? ? ? ? ? adj = 1,
? ? ? ? ? cex = 1
? ? ? )
? ? } else {
? ? ? text(x = 5 + 3.3 * cos((start + deg * (t - 1)) * pi / 180),
? ? ? ? ? y = 5 + 3.3 * sin((start + deg * (t - 1)) * pi / 180),
? ? ? ? ? sample[t],
? ? ? ? ? srt = deg * (t - 1) + start,
? ? ? ? ? adj = 0,
? ? ? ? ? cex = 1
? ? ? )
? ? }
? })
? draw.circle(x = 5, y = 5, r = r, col = circle_col, border = NA)
? text(x = 5, y = 5, paste('Core:', core_otu))
}
## 6.調(diào)用上述函數(shù)作圖(保存圖片到本地文件夾中)
png('flower.png', width = 1500, height = 1500, res = 200, units = 'px')
flower_plot(sample = sample_id, otu_num = otu_num, core_otu = core_num,
? ? ? ? ? ? start = 90, a = 0.5, b = 2, r = 1, ellipse_col = ellipse_col, circle_col = 'white')
dev.off()
pdf('flower.pdf', width = 15, height = 15)
flower_plot(sample = sample_id, otu_num = otu_num, core_otu = core_num,
? ? ? ? ? ? start = 90, a = 0.5, b = 2, r = 1, ellipse_col = ellipse_col, circle_col = 'white')
dev.off()