How to make a sunburst plot in R or Python?

前端 未结 8 1637
梦如初夏
梦如初夏 2020-11-28 22:54

So far I have been unable to find an R library that can create a sunburst plot like those by John Stasko. Anyone knows how to accomplish that in R or Python?

8条回答
  •  佛祖请我去吃肉
    2020-11-28 23:33

    Since jbkunst mentioned ggsunburst, here I post an example for reproducing the sunburst by sirex.

    It is not exactly the same because in ggsunburst the angle of a node is equal to the sum of the angles of its children nodes.

    # 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)
    

提交回复
热议问题