如何在R的sunburstR包中指定分类颜色与标签及理解相关参数?
Great questions about sunburstR's sunburst() function—let's break down each part clearly, since the docs can be a bit vague on these details!
sunburst() Color Mapping Logic How Color Vectors Are Applied to Categories
When you pass a simple color vector (like colors = c("#ff0000", "#00ff00", "#0000ff")) to sunburst(), the function uses D3's ordinal color scaling under the hood. Here's the exact flow:
- It extracts all unique node values from your hierarchical data (from root to every leaf node).
- It maps these unique nodes to your color vector in the order the nodes first appear in your data (or sorted order, depending on your data structure).
- If your color vector is shorter than the number of unique nodes, it will cycle through the colors repeatedly.
For example, if your data has root node "Total", first-level nodes "A" / "B", and second-level nodes "A1" / "A2" / "B1", a vector c("red", "blue") would assign:
- "Total" → red
- "A" → blue
- "B" → red (cycles back)
- "A1" → blue
- "A2" → red
- "B1" → blue
Manual Color Assignment for Root/Leaf Nodes (Better Approaches)
A simple color vector works for general cases, but for targeting root or leaf nodes specifically, using a custom color mapping list is far more reliable. Here's how to do it:
Targeting the Root Node
If your root node has a fixed name (e.g., "Root"), define a list with domain (the nodes you want to target) and range (their corresponding colors):
root_color_map <- list( domain = c("Root"), range = c("#000000") # Black root node ) sunburst(your_data, colors = root_color_map)
Any nodes not in domain will use the default D3 categorical color scheme.
Targeting Leaf Nodes
First, identify all leaf nodes from your data (this depends on your data format—for CSV-based hierarchies, leaves are the deepest non-NA values). Then add them to your color map:
# Example: Data has columns level1, level2, level3; leaves are level3 values leaf_nodes <- unique(your_data$level3[!is.na(your_data$level3)]) leaf_color_map <- list( domain = c("Root", leaf_nodes), range = c("#000000", rep("#ff0000", length(leaf_nodes))) # Black root, red leaves ) sunburst(your_data, colors = leaf_color_map)
Pro Tip: Override All Node Colors
If you want full control, include every unique node in domain and map each to a specific color in range.
By default, sunburst() displays labels for all nodes, but you can tweak this to avoid clutter or highlight specific partitions:
- Hide labels entirely: Set
labels = FALSE. - Show labels only for large partitions: Use
labelThresholdto set a minimum percentage of the total. For example,labelThreshold = 0.05will only show labels for partitions making up 5% or more of the total. - Adjust label appearance: Use
labelSize(font size) andlabelAngle(rotation) to improve readability for nested partitions.
Example:
sunburst(your_data, labelThreshold = 0.03, labelSize = 12, labelAngle = 45)
This line in the docs refers to the flexible color mapping options when you pass a list to the colors parameter:
- Only
range: If you provide just arange(e.g.,list(range = c("red", "blue"))), the function automatically uses all unique nodes as thedomain, mapping them to yourrangecolors (cycling if needed). This is equivalent to passing a simple color vector, but more explicit. - Only
domain: If you provide just adomain(e.g.,list(domain = c("Root", "A"))), the function uses the default D3 color scheme for those nodes, and falls back to defaults for others. - Both
domainandrange: This is the most powerful option—you explicitly map specific node values to specific colors, which is exactly what you need for targeting root/leaf nodes.
内容的提问来源于stack exchange,提问作者Carmen Sandoval

