【R】circlize

1. はじめに

circlizeは、その名の通り、円形のグラフ、チャートを表示してくれるパッケージです。

2. インストール

CRANからインストールできます。

install.packages("circlize")

3. 使ってみる

通常のbarplotのようにデータを準備しtてプロットします。

library(circlize)
par(mfrow = c(1, 2))

circos.initialize(fa = letters[1:4], xlim = c(0, 8))
circos.track(ylim = c(0, 4), panel.fun = function(x, y) {
  value = matrix(runif(10*4), ncol = 4)
  circos.barplot(value, 1:10 - 0.5, col = 2:5)
})

circos.clear()

circos.initialize(fa = letters[1:4], xlim = c(0, 10))
circos.track(ylim = c(0, 1), panel.fun = function(x, y) {
  value = runif(10)
  circos.barplot(value, 1:10 - 0.5, col = 1:10)
})
circos.track(ylim = c(-1, 1), panel.fun = function(x, y) {
  value = runif(10, min = -1, max = 1)
  circos.barplot(value, 1:10 - 0.5, col = ifelse(value > 0, 2, 3))
})
circos.track(ylim = c(-1, 1), panel.fun = function(x, y) {
  value = runif(5, min = -1, max = 1)
  circos.barplot(value, 1:5 - 0.5, col = ifelse(value < 0, 1, 4))
})

boxplotもOKです。

circos.initialize(fa = letters[1:6], xlim = c(0, 5))
circos.track(ylim = c(0, 1), panel.fun = function(x, y) {
  value = replicate(runif(5), n = 5, simplify = FALSE)
  circos.boxplot(value, 1:5 - 0.5, col = 1:5)
})

Multiple-group Chord diagram

mat1 = matrix(rnorm(25), nrow = 5)
rownames(mat1) = paste0("A", 1:5)
colnames(mat1) = paste0("B", 1:5)

mat2 = matrix(rnorm(25), nrow = 5)
rownames(mat2) = paste0("A", 1:5)
colnames(mat2) = paste0("C", 1:5)

mat3 = matrix(rnorm(25), nrow = 5)
rownames(mat3) = paste0("B", 1:5)
colnames(mat3) = paste0("C", 1:5)

mat = matrix(0, nrow = 10, ncol = 10)
rownames(mat) = c(rownames(mat2), rownames(mat3))
colnames(mat) = c(colnames(mat1), colnames(mat2))
mat[rownames(mat1), colnames(mat1)] = mat1
mat[rownames(mat2), colnames(mat2)] = mat2
mat[rownames(mat3), colnames(mat3)] = mat3
mat


nm = unique(unlist(dimnames(mat)))
group = structure(gsub("\\d", "", nm), names = nm)
group

grid.col = structure(c(rep(2, 5), rep(3, 5), rep(4, 5)),
                     names = c(paste0("A", 1:5), paste0("B", 1:5), paste0("C", 1:5)))
chordDiagram(mat, group = group, grid.col = grid.col)

4. さいごに

これ、使いこなすとなかなかクールな表現ができそうです。

Add a Comment

メールアドレスが公開されることはありません。