How to specify the colors and toggle labels for each category in R sunburst?

為{幸葍}努か 提交于 2021-01-28 05:49:53

问题


I can't seem to understand how sunburst in the sunburstR package handles certain paramenters, namely the vector of colors given to it and how it applies the colors towards the different categories, as well as how to show the labels for the different partitions.

Is there a logic to it, and is there a better way to manually specify which color should go with which root/leaf?

From ?sunburst : (No idea what they mean by "supply a list with range and/or domain."

colors  
vector of strings representing colors as hexadecimal for manual colors. 
If you want precise control of colors, supply a list with range and/or domain. 
For advanced customization, supply a JavaScript function.

Thanks!

sourcestatus

# A tibble: 5 x 2
  SourceStatus         count
  <chr>                <int>
1 blood-affected        4369
2 blood-unaffected      2848
3 blood-unknown            1
4 cell_line-affected    1797
5 cell_line-unaffected   151

Create sunburst:

 sunburst(sourcestatus, count = TRUE, percent= T, 
          colors = c("#6b5b95", "#feb236", "#d64161", "#ff7b25"))


回答1:


Colors specified simply with a list of colors:

colors = c("#6b5b95", "#feb236", "#d64161", "#ff7b25")

Will repeat those colors moving clockwise layer by layer. This ensures no adjacent leafs in the same layer have the same color (child/parent leaves can). If specifying only one color, the color is ignored and the default color palette is used.

While it is not clear how to specify colors using the domain and range on the r side of things, you can specify what color you would like each leaf to be filled with. If you have a lot of leafs you'd want to build a function to fill this data in, but here's a demonstration using a quick and dirty dataframe and a few colors:

library(sunburstR)

leafs <- c("base","base-child1-grandchild1","base-child1-grandchild2","base-child1","base-child2-grandchild3","base-child2","base-child3-grandchild4","base-child3-grandchild5","base-child3-grandchild6","base-child3-grandchild7","base-child3")
values <- c(200,15,10,20,55,10,120,30,30,20,10)  

# colors
colors <- c("#c994c7","#6a51a3","#807dba","#bcbddc","#74c476","#c7e9c0","#fcbba1","#fc9272","#ef3b2c","#cb181d","#99000d")
# match those colors to leaf names, matched by index
labels <- c("base","grandchild1","grandchild2","child1","child2","grandchild3","grandchild4","grandchild5","grandchild6","grandchild7","child3")

df = data.frame(v1=leafs, v2=values);

sunburst(df, 
         colors = list(range = colors, domain = labels))

Giving each first level child its own color scheme:

The list fed to colors contains a range of output colors and a domain of input leaf names, which allows pairing of specific colors to specific leafs.



来源:https://stackoverflow.com/questions/49993198/how-to-specify-the-colors-and-toggle-labels-for-each-category-in-r-sunburst

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!