I try to change colors of trace using a sunburst chart in R plot-ly.
I put an example.
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")
)
I got the plot like this:
I try to change colors of the outside part ("win", "loss"). The win is red color and loss is a blue color. How can I change?
An easy way to do it, is to add another column to your data.frame in which you specify the desired color for the segment and pass the variable to your trace:
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