如何在R或Python中制作太阳图?

50

到目前为止,我没有找到任何一个R库可以像John Stasko的那些旭日图一样创建旭日图。 有人知道如何在R或Python中实现吗?

Sunburst


2
这些被称为“树状图”的矩形等效物非常受欢迎。如果您搜索“圆形树状图”或类似内容,可能会有更好的运气。 - fmark
9个回答

46

使用matplotlib库在极坐标系中绘制sunburst图的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


3
这是最优雅的回答!喜欢这个递归。 - dmvianna
1
简单易处理,可扩展性强,无需额外的库;纯粹的天才。这值得更多的赞。 - Ébe Isaac
这种交互式缩放可视化能否在R中完成? - kRazzy R

35

现在你甚至可以很容易地使用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)

输入图像描述

...当你将鼠标悬停在它上面时,魔法就发生了:

输入图像描述

编辑
此软件包的官方网站可以在此处找到(有许多示例!):https://github.com/timelyportfolio/sunburstR

感谢 @timelyportfolio 创建了这个令人印象深刻的代码!


@Dror:这可能会引起你的兴趣 :-) - vonjd
4
感谢更新。当然,我更喜欢交互版本。任何人都可以在 https://github.com/timelyportfolio/sunburstR 上提供反馈、想法、批评、应用场景和示例。请随意发表意见。 - timelyportfolio
@timelyportfolio:谢谢,我已经在答案中添加了链接 :-) - vonjd
如何将图例分成两列以确保所有标签都能正确显示? - kRazzy R
1
@kRazzyR:我不是这段代码的作者。最好在这里提出你的问题:https://github.com/timelyportfolio/sunburstR/issues - vonjd
显示剩余2条评论

14

您可以使用ggplot2包中的geom_tile创建类似于太阳辐射图的东西。让我们首先创建一些随机数据:

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 轴与 dat 中的 x 变量相对应,y 轴与 y 变量相对应,像素的填充与 z 变量相对应。 得到以下图形:

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

enter image description here

< p > < code > ggplot2 软件包支持各种坐标变换,其中一种将一个轴投射到圆上,即极坐标:

p + coord_polar()

enter image description here

这大致可以满足您的需求,现在您可以调整dat以获得所需结果。

1
当您将图形转储到文件时,问题可能会消失。 - Paul Hiemstra
你介意给我讲解一下如何使用ggplot2制作这个图表吗?我仍然很难理解数据表格在ribbon中的表示方式。 - dmvianna
我对我的代码进行了相当大的修改,我发现我的原始答案不够优化甚至是错误的。看看新代码,如果你仍然有理解上的困难,请留言评论。 - Paul Hiemstra
1
那是一个极坐标网格,不是太阳辐射图! - PAC
@PAC,您能解释一下为什么这是个问题吗?在我看来,Sunburst图是一个极坐标图,生成的图看起来很好。 - Paul Hiemstra
显示剩余6条评论

7
有一个叫做ggsunburst的软件包。可惜它不在CRAN上,但你可以从Github上安装它:didacs/ggsunburst

enter image description here


4
这是一个包含两个层级的 ggplot2 旭日图。
基本思路是为每个层级制作不同的条形图,并将外部层级的条形图变宽。我还更改了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')

在此输入图片描述

与专门针对Sunburst的软件相比,唯一的缺点是它假设您希望外层是完全穷尽的(即没有空白)。部分穷尽的外层(如其他示例中的一些)肯定是可能的,但更加复杂。

为了完整起见,这里进行了清理,使用了更好的格式和标签:

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

由于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


3
我知道的仅有几个库能够原生地进行此操作:

这些库都不是Python或R语言的,但让python/R脚本编写一个简单的JSON文件,并能够被任意javascript库加载,应该是很容易实现的。


1
这个答案有更新吗?已经过去2年了。 - Dror

2
这是一个使用 R 和 plotly 的例子(基于我的答案此处):
library(datasets)
library(data.table)
library(plotly)

as.sunburstDF <- function(DF, valueCol = NULL){
  require(data.table)
  
  colNamesDF <- names(DF)
  
  if(is.data.table(DF)){
    DT <- copy(DF)
  } else {
    DT <- data.table(DF, stringsAsFactors = FALSE)
  }
  
  DT[, root := names(DF)[1]]
  colNamesDT <- names(DT)
  
  if(is.null(valueCol)){
    setcolorder(DT, c("root", colNamesDF))
  } else {
    setnames(DT, valueCol, "values", skip_absent=TRUE)
    setcolorder(DT, c("root", setdiff(colNamesDF, valueCol), "values"))
  }
  
  hierarchyCols <- setdiff(colNamesDT, "values")
  hierarchyList <- list()
  
  for(i in seq_along(hierarchyCols)){
    currentCols <- colNamesDT[1:i]
    if(is.null(valueCol)){
      currentDT <- unique(DT[, ..currentCols][, values := .N, by = currentCols], by = currentCols)
    } else {
      currentDT <- DT[, lapply(.SD, sum, na.rm = TRUE), by=currentCols, .SDcols = "values"]
    }
    setnames(currentDT, length(currentCols), "labels")
    hierarchyList[[i]] <- currentDT
  }
  
  hierarchyDT <- rbindlist(hierarchyList, use.names = TRUE, fill = TRUE)
  
  parentCols <- setdiff(names(hierarchyDT), c("labels", "values", valueCol))
  hierarchyDT[, parents := apply(.SD, 1, function(x){fifelse(all(is.na(x)), yes = NA_character_, no = paste(x[!is.na(x)], sep = ":", collapse = " - "))}), .SDcols = parentCols]
  hierarchyDT[, ids := apply(.SD, 1, function(x){paste(x[!is.na(x)], collapse = " - ")}), .SDcols = c("parents", "labels")]
  hierarchyDT[, c(parentCols) := NULL]
  return(hierarchyDT)
}

DF <- as.data.table(Titanic)
setcolorder(DF, c("Survived", "Class", "Sex", "Age", "N"))
sunburstDF <- as.sunburstDF(DF, valueCol = "N")

# Sunburst
plot_ly(data = sunburstDF, ids = ~ids, labels= ~labels, parents = ~parents, values= ~values, type='sunburst', branchvalues = 'total')

# Treemap
# plot_ly(data = sunburstDF, ids = ~ids, labels= ~labels, parents = ~parents, values= ~values, type='treemap', branchvalues = 'total')

result

一些额外的信息可以在这里找到。

0

您也可以在Python中使用plotly Sunburst,如此处所示。

相同的输入也可用于创建Icicle和Treemap图表(由plotly支持),这也可能适合您的需求。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接