【问题标题】:How can I change colors of segments in trace?如何更改跟踪中段的颜色?
【发布时间】:2019-07-16 20:16:13
【问题描述】:

我尝试使用 R plot-ly 中的旭日形图更改迹线的颜色。

我举个例子。

data1 <- data.frame(
ids=c("up","down","noCh","up-win","down-win","up-loss","down-loss",   "noCh-win","noCh-loss"),
labels= c("up","down","noCh","win","loss","win","loss","win","loss"),
parents=c("","","","up","up","down","down","noCh","noCh" ),
stringsAsFactors = FALSE)

p <- plot_ly() %>%
 add_trace(
   ids = data1$ids,
   labels = data1$labels,
   parents =data1$parents,
   type = 'sunburst',
   maxdepth = 3,
   domain = list(column = 1)
 )%>% 
 layout(
 grid = list(columns =1, rows = 1),
 margin = list(l = 0, r = 0, b = 0, t = 0),
 colorway=c("orange","yellow","green")
 )

我得到了这样的情节:

我尝试改变外部部分的颜色(“赢”、“输”)。胜利是红色,失败是蓝色。我该如何改变?

【问题讨论】:

    标签: r plotly sunburst-diagram


    【解决方案1】:

    一种简单的方法是在 data.frame 中添加另一列,在其中为段指定所需的颜色并将变量传递给跟踪:

    data1 <- data.frame(
      ids=c("up","down","noCh","up-win","down-win","up-loss","down-loss",   "noCh-win","noCh-loss"),
      labels= c("up","down","noCh","win","loss","win","loss","win","loss"),
      parents=c("","","","up","up","down","down","noCh","noCh" ),
      col = c("orange","yellow","green", rep(c("red", "blue"), 3)),
      stringsAsFactors = FALSE)
    
    p <- plot_ly() %>%
      add_trace(
        ids = data1$ids,
        labels = data1$labels,
        parents =data1$parents,
        type = 'sunburst',
        maxdepth = 3,
        marker = list(colors = data1$col),
        domain = list(column = 1)
      )%>% 
      layout(
        grid = list(columns =1, rows = 1),
        margin = list(l = 0, r = 0, b = 0, t = 0)
      )
    
    p
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 2022-05-26
      • 1970-01-01
      相关资源
      最近更新 更多