如何使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默认配色中的固定颜色,确保数据变化时颜色映射不改变:
- 提取Plotly默认配色序列
- 为所有唯一
manuf分配固定颜色,生成映射表 - 绘图时通过
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
相关产品推荐
相关产品推荐

