vlambda博客
学习文章列表

用R语言|画个太极图

        太极图被称为“中华第一图”。从孔庙大成殿梁柱,到楼观台、三茅宫、白云观的标记物;从道士的道袍,到算命先生的卦摊;从中医、气功、武术及中国传统文化的书刊封面、会徽会标,到南韩国旗图案、新加坡空军机徽、玻尔勋章族徽……等等,太极图无不跃居其上。这种广为人知的太极图,其形状如阴阳两鱼互纠在一起,因而被习称为“阴阳鱼太极图”,早期称作“先天图”、“河图”、“先天自然河图”、“先天自然之图”、“古太极图”等。今天我们就来画个太极图。(PS:好久都没有动了,懒了

    首先需要安装circlize包:

install.packages("circlize")

或者

devtools::install_github("jokergoo/circlize")

    然后加载circlize包,开始画个太极图

library(circlize) #加载和弦包factors = 1:8circos.par(start.degree = 22.5, gap.degree = 6)circos.initialize(factors = factors, xlim = c(0, 1))
# 阳爻 is __ (a long segment) 一个长的部分add_yang_yao = function() { circos.rect(0,0,1,1, col = "black")}
# 阴爻 is -- (two short segments) 两短长的部分add_yin_yao = function() { circos.rect(0,0,0.45,1, col = "black") circos.rect(0.55,0,1,1, col = "black")}circos.track(ylim = c(0, 1), factors = factors, bg.border = NA, panel.fun = function(x, y) { i = get.cell.meta.data("sector.numeric.index") if(i %in% c(2, 5, 7, 8)) add_yang_yao() else add_yin_yao() }, track.height = 0.1)
circos.track(ylim = c(0, 1), factors = factors, bg.border = NA, panel.fun = function(x, y) { i = get.cell.meta.data("sector.numeric.index") if(i %in% c(1, 6, 7, 8)) add_yang_yao() else add_yin_yao() }, track.height = 0.1)
circos.track(ylim = c(0, 1), factors = factors, bg.border = NA, panel.fun = function(x, y) { i = get.cell.meta.data("sector.numeric.index") if(i %in% c(4, 5, 6, 7)) add_yang_yao() else add_yin_yao() }, track.height = 0.1)
# the bottom of the most recent trackr = get.cell.meta.data("cell.bottom.radius") - 0.1# draw taiji, note default order is clock wise for `draw.sector`draw.sector(center = c(0, 0), start.degree = 90, end.degree = -90, rou1 = r, col = "black", border = "black")draw.sector(center = c(0, 0), start.degree = 270, end.degree = 90, rou1 = r, col = "white", border = "black")draw.sector(center = c(0, r/2), start.degree = 0, end.degree = 360, rou1 = r/2, col = "white", border = "white")draw.sector(center = c(0, -r/2), start.degree = 0, end.degree = 360, rou1 = r/2, col = "black", border = "black")draw.sector(center = c(0, r/2), start.degree = 0, end.degree = 360, rou1 = r/8, col = "black", border = "black")draw.sector(center = c(0, -r/2), start.degree = 0, end.degree = 360, rou1 = r/8, col = "white", border = "white")circos.clear()

效果如图