You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使R语言Plotly树形图中同一名称对应固定颜色?

问题:Plotly树形图固定同一类别颜色,保留原有配色方案

我使用R语言的Plotly库绘制树形图,初始数据df1中,Merc占比22%,显示为蓝色;但将数据修改为df2(AMC的count值改为25)后,Merc的颜色变为橙色,无法保持固定。需要实现同一manuf名称对应固定颜色,同时保留Plotly默认的配色方案。

初始代码(df1)

df1<-structure(list(manuf = c("AMC", "Cadillac", "Camaro", "Chrysler", 
                                  "Datsun", "Dodge", "Duster", "Ferrari", "Fiat", "Ford", "Honda", 
                                  "Hornet", "Lincoln", "Lotus", "Maserati", "Mazda", "Merc", "Pontiac", 
                                  "Porsche", "Toyota", "Valiant", "Volvo"), count = c(1L, 1L, 1L, 
                                                                                      1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 7L, 1L, 1L, 
                                                                                      2L, 1L, 1L)), row.names = c(NA, -22L), class = c("tbl_df", "tbl", 
                                                                                                                                       "data.frame"))

# 补充df1的parents列(原代码未定义test,此处补上)
df1$test <- 1

treemap<-plot_ly(data = df1,
                 type= "treemap",
                 values= ~count,
                 labels= ~manuf,
                 parents=  ~test,
                 domain= list(column=0),
                 name = " ",
                 textinfo="label+value+percent parent")  %>%
  layout(title="", 
         annotations =
           list(x = 0, y = -0.1,
                title = "", 
                text = " ",
                showarrow = F,
                xref='paper',
                yref='paper'))

treemap

修改后代码(df2)

df2<-structure(list(manuf = c("AMC", "Cadillac", "Camaro", "Chrysler", 
                              "Datsun", "Dodge", "Duster", "Ferrari", "Fiat", "Ford", "Honda", 
                              "Hornet", "Lincoln", "Lotus", "Maserati", "Mazda", "Merc", "Pontiac", 
                              "Porsche", "Toyota", "Valiant", "Volvo"), count = c(25L, 1L, 1L, 
                                                                                  1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 7L, 1L, 1L, 
                                                                                  2L, 1L, 1L)), row.names = c(NA, -22L), class = c("tbl_df", "tbl", 
                                                                                                                                   "data.frame"))

df2$test<-1

treemap<-plot_ly(data = df2,
                 type= "treemap",
                 values= ~count,
                 labels= ~manuf,
                 parents=  ~test,
                 domain= list(column=0),
                 name = " ",
                 #marker = list(colors = ~colr),
                 textinfo="label+value+percent parent")  %>%
  layout(title="", 
         annotations =
           list(x = 0, y = -0.1,
                title = "", 
                text = " ",
                showarrow = F,
                xref='paper',
                yref='paper'))

treemap

解决方案

核心思路是为每个manuf预先绑定Plotly默认配色中的固定颜色,确保数据变化时颜色映射不改变:

  1. 提取Plotly默认配色序列
  2. 为所有唯一manuf分配固定颜色,生成映射表
  3. 绘图时通过marker参数指定该映射表

完整实现代码

# 1. 获取Plotly默认配色
default_colors <- plotly::plotly_colors

# 2. 生成固定颜色映射表:按manuf排序后分配颜色
unique_manuf <- sort(unique(c(df1$manuf, df2$manuf)))
color_mapping <- setNames(default_colors[1:length(unique_manuf)], unique_manuf)

# --- 绘制df1的树形图(颜色固定)---
treemap_df1 <- plot_ly(data = df1,
                       type= "treemap",
                       values= ~count,
                       labels= ~manuf,
                       parents=  ~test,
                       domain= list(column=0),
                       name = " ",
                       marker = list(colors = color_mapping[manuf]), # 指定固定颜色
                       textinfo="label+value+percent parent") %>%
  layout(title="", 
         annotations = list(x = 0, y = -0.1,
                            title = "", 
                            text = " ",
                            showarrow = F,
                            xref='paper',
                            yref='paper'))

treemap_df1

# --- 绘制df2的树形图(颜色与df1一致)---
treemap_df2 <- plot_ly(data = df2,
                       type= "treemap",
                       values= ~count,
                       labels= ~manuf,
                       parents=  ~test,
                       domain= list(column=0),
                       name = " ",
                       marker = list(colors = color_mapping[manuf]), # 复用同一颜色映射
                       textinfo="label+value+percent parent") %>%
  layout(title="", 
         annotations = list(x = 0, y = -0.1,
                            title = "", 
                            text = " ",
                            showarrow = F,
                            xref='paper',
                            yref='paper'))

treemap_df2

说明

  • 用plotly::plotly_colors获取Plotly默认的配色方案,保证风格统一
  • 通过sort(unique(...))确保manuf的排序固定,对应颜色位置不变
  • 不管数据中count如何变化,同一manuf始终使用映射表中绑定的颜色

内容的提问来源于stack exchange,提问作者silent_hunter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 05:40:41