gpt4 book ai didi

r - 如何在 R sunburst 中为每个类别指定颜色和切换标签?

转载 作者:行者123 更新时间:2023-12-04 16:11:44 24 4
gpt4 key购买 nike

我似乎无法理解 sunburstsunburstR包处理某些参数,即赋予它的颜色向量以及它如何将颜色应用于不同的类别,以及如何显示不同分区的标签。

它是否有逻辑,是否有更好的方法来手动指定哪种颜色应该与哪个根/叶搭配?

来自 ?sunburst :(不知道他们所说的“提供具有范围和/或域的列表”是什么意思。

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.

谢谢!
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

创建旭日:
 sunburst(sourcestatus, count = TRUE, percent= T, 
colors = c("#6b5b95", "#feb236", "#d64161", "#ff7b25"))

enter image description here

最佳答案

用颜色列表简单地指定颜色:

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

将逐层重复顺时针移动的颜色。这确保同一层中没有相邻的叶子具有相同的颜色(子/父叶子可以)。如果仅指定一种颜色,则忽略该颜色并使用默认调色板。

虽然不清楚如何使用事物的 r 侧的域和范围指定颜色,但您可以指定您希望每个叶子填充的颜色。如果你有很多叶子,你会想要构建一个函数来填充这些数据,但这里有一个使用快速和脏数据框和几种颜色的演示:
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))

给每个一级 child 自己的配色方案:

enter image description here

提供给颜色的列表包含一系列输出颜色和输入叶子名称域,这允许将特定颜色与特定叶子配对。

关于r - 如何在 R sunburst 中为每个类别指定颜色和切换标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49993198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com