web-dev-qa-db-ja.com

RまたはPythonでサンバーストプロットを作成する方法は?

これまでのところ、 John Stasko のようなサンバーストプロットを作成できるRライブラリを見つけることができませんでした。 RまたはPythonでそれを達成する方法を知っている人はいますか?

Sunburst

39
dmvianna

極座標投影でmatplotlibバーを使用したサンバースト図のPythonバージョン:

import numpy as np
import matplotlib.pyplot as plt

def sunburst(nodes, total=np.pi * 2, offset=0, level=0, ax=None):
    ax = ax or plt.subplot(111, projection='polar')

    if level == 0 and len(nodes) == 1:
        label, value, subnodes = nodes[0]
        ax.bar([0], [0.5], [np.pi * 2])
        ax.text(0, 0, label, ha='center', va='center')
        sunburst(subnodes, total=value, level=level + 1, ax=ax)
    Elif nodes:
        d = np.pi * 2 / total
        labels = []
        widths = []
        local_offset = offset
        for label, value, subnodes in nodes:
            labels.append(label)
            widths.append(value * d)
            sunburst(subnodes, total=total, offset=local_offset,
                     level=level + 1, ax=ax)
            local_offset += value
        values = np.cumsum([offset * d] + widths[:-1])
        heights = [1] * len(nodes)
        bottoms = np.zeros(len(nodes)) + level - 0.5
        rects = ax.bar(values, heights, widths, bottoms, linewidth=1,
                       edgecolor='white', align='Edge')
        for rect, label in Zip(rects, labels):
            x = rect.get_x() + rect.get_width() / 2
            y = rect.get_y() + rect.get_height() / 2
            rotation = (90 + (360 - np.degrees(x) % 180)) % 360
            ax.text(x, y, label, rotation=rotation, ha='center', va='center') 

    if level == 0:
        ax.set_theta_direction(-1)
        ax.set_theta_zero_location('N')
        ax.set_axis_off()

例、この関数の使用方法:

data = [
    ('/', 100, [
        ('home', 70, [
            ('Images', 40, []),
            ('Videos', 20, []),
            ('Documents', 5, []),
        ]),
        ('usr', 15, [
            ('src', 6, [
                ('linux-headers', 4, []),
                ('virtualbox', 1, []),

            ]),
            ('lib', 4, []),
            ('share', 2, []),
            ('bin', 1, []),
            ('local', 1, []),
            ('include', 1, []),
        ]),
    ]),
]

sunburst(data)

python matplotlib sunburst diagram

32
sirex

Rを使用して、インタラクティブバージョンを非常に簡単に構築することもできます。

# devtools::install_github("timelyportfolio/sunburstR")

library(sunburstR)
# read in sample visit-sequences.csv data provided in source
# https://Gist.github.com/kerryrodden/7090426#file-visit-sequences-csv
sequences <- read.csv(
  system.file("examples/visit-sequences.csv",package="sunburstR")
  ,header=F
  ,stringsAsFactors = FALSE
)

sunburst(sequences)

enter image description here

...そしてマウスをその上に移動すると、魔法が起こります:

enter image description here

編集
このパッケージの公式サイトはここにあります(多くの例があります!): https://github.com/timelyportfolio/sunburstR

この印象的なコードを作成した@timelyportfolioへの帽子のヒント!

30
vonjd

geom_tileパッケージのggplot2を使用して、サンバーストプロットの線に沿って何かを作成できます。最初にいくつかのランダムデータを作成しましょう。

require(ggplot2); theme_set(theme_bw())
require(plyr)
dat = data.frame(expand.grid(x = 1:10, y = 1:10),
                 z = sample(LETTERS[1:3], size = 100, replace = TRUE))

次に、ラスタープロットを作成します。ここで、プロットのx軸はxdat変数に、y軸はy変数に、 z変数へのピクセルの塗りつぶし。これにより、次のプロットが生成されます。

p = ggplot(dat, aes(x = x, y = y, fill = z)) + geom_tile() 
print(p)

enter image description here

ggplot2パッケージは、すべての種類の座標変換をサポートします。そのうちの1つは、1つの軸を取り、円、つまり極座標に投影します。

p + coord_polar()

enter image description here

これはおおよそ必要なことを行いますが、datを調整して目的の結果を得ることができます。

12
Paul Hiemstra

ggsunburstというパッケージがあります。残念ながらCRANにはありませんが、Webサイトの指示に従ってインストールできます: http://genome.crg.es/~didac/ggsunburst/ggsunburst.html

enter image description here

まだこのような良いパッケージを探している人々に役立つことを願っています。

よろしく、

5
jbkunst

ggplot2 2層のサンバースト。

基本的な考え方は、レイヤーごとに異なるバーを作成し、外側のレイヤーのバーを広くすることです。また、内側の円グラフの中央に穴がないことを確認するために、x軸をいじりました。したがって、幅とx軸の値を変更することにより、サンバーストの外観を制御できます。

library(ggplot2)

# make some fake data
df <- data.frame(
    'level1'=c('a', 'a', 'a', 'a', 'b', 'b', 'c', 'c', 'c'), 
    'level2'=c('a1', 'a2', 'a3', 'a4', 'b1', 'b2', 'c1', 'c2', 'c3'), 
    'value'=c(.025, .05, .027, .005, .012, .014, .1, .03, .18))

# sunburst plot
ggplot(df, aes(y=value)) +
    geom_bar(aes(fill=level1, x=0), width=.5, stat='identity') + 
    geom_bar(aes(fill=level2, x=.25), width=.25, stat='identity') + 
    coord_polar(theta='y')

enter image description here

これがサンバースト固有のソフトウェアと比較して唯一の欠点は、外側の層をまとめて網羅する(つまり、ギャップがない)ことを前提としていることです。 「他の例の一部のように」「部分的に網羅的な」外層は確かに可能ですが、より複雑です。

完全を期すために、ここではより適切なフォーマットとラベルでクリーンアップされています。

library(data.table)

# compute cumulative sum for outer labels
df <- data.table(df)
df[, cumulative:=cumsum(value)-(value/2)]

# store labels for inner circle
inner_df <- df[, c('level1', 'value'), with=FALSE]
inner_df[, level1_value:=sum(value), by='level1']
inner_df <- unique(text_df[, c('level1', 'level1_value'), with=FALSE])
inner_df[, cumulative:=cumsum(level1_value)]
inner_df[, prev:=shift(cumulative)]
inner_df[is.na(prev), position:=(level1_value/2)]
inner_df[!is.na(prev), position:=(level1_value/2)+prev]

colors <- c('#6a3d9a', '#1F78B4', '#33A02C', '#3F146D', '#56238D', '#855CB1', '#AD8CD0', '#08619A', '#3F8DC0', '#076302', '#1B8416', '#50B74B')
colorNames <- c(unique(as.character(df$level1)), unique(as.character(df$level2)))
names(colors) <- colorNames

ggplot(df, aes(y=value, x='')) +
    geom_bar(aes(fill=level2, x=.25), width=.25, stat='identity') + 
    geom_bar(aes(fill=level1, x=0), width=.5, stat='identity') + 
    geom_text(data=inner_df, aes(label=level1, x=.05, y=position)) + 
    coord_polar(theta='y') + 
    scale_fill_manual('', values=colors) +
    theme_minimal() + 
    guides(fill=guide_legend(ncol=1)) +
    labs(title='') + 
    scale_x_continuous(breaks=NULL) + 
    scale_y_continuous(breaks=df$cumulative, labels=df$level2, 5) + 
    theme(axis.title.x=element_blank(), axis.title.y=element_blank(), panel.border=element_blank(), panel.grid=element_blank())

enter image description here

3
dmp

私が知っているのは、ネイティブにこれを行うライブラリがいくつかあるだけです:

これらのどちらもPythonまたはRにありませんが、python/Rスクリプトを取得して、JavaScriptライブラリのいずれかによってロードできる単純なJSONファイルを書き出すことは、かなり達成可能です。

3
jozzas

Jbkunstがggsunburstに言及したので、ここにsirexによってサンバーストを再現する例を投稿します。

Ggsunburstでは、ノードの角度はその子ノードの角度の合計に等しいため、まったく同じではありません。

# install ggsunburst package
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("rPython")) install.packages("rPython")
install.packages("http://genome.crg.es/~didac/ggsunburst/ggsunburst_0.0.9.tar.gz", repos=NULL, type="source")
library(ggsunburst)

# dataframe
# each row corresponds to a node in the hierarchy
# parent and node are required, the rest are optional attributes
# the attributes correspond to the node, not its parent
df <- read.table(header = T, sep = ",", text = "
parent,node,size,color,dist
,/,,B,1
/,home,,D,1
home,Images, 40,E,1
home,Videos, 20,E,1
home,Documents, 5,E,1
/,usr,,D,1
usr,src,,A,1
src,linux-headers, 4,C,1.5
src,virtualbox, 1,C,1.5
usr,lib, 4,A,1
usr,share, 2,A,1
usr,bin, 1,A,1
usr,local, 1,A,1
usr,include, 1,A,1
")

write.table(df, 'df.csv', sep = ",", row.names = F)

# compute coordinates from dataframe
# "node_attributes" is used to pass the attributes other than "size" and "dist", 
# which are special attributes that alter the dimensions of the nodes
sb <- sunburst_data('df.csv', sep = ",", type = "node_parent", node_attributes = "color")

# plot
sunburst(sb, node_labels = T, node_labels.min = 10, rects.fill.aes = "color") +
  scale_fill_brewer(palette = "Set1", guide = F)

enter image description here

2
didac