2

I am trying to color a by parent and then by level so that parent1 and parent2 are red and blue and then they gradient outwards by level.

I seem to be close using layout with sunburstcolorway but it doesn't quite color by level. Currently my input for colorway is a list of hex codes that are specified for each observation.

If there is a way to specify color for each individual section that would be ideal. I can't seem to figure it out, I'm very new to this

library(plotly)
clin2 <- data.frame(
  stringsAsFactors = FALSE,
  ids = c("MGH","CU","MGH - WT",
          "CU - WT","MGH - KDM","CU - KDM","MGH - G2032R",
          "MGH - S1986F","MGH - D2033N","CU - L1951R/L2026M"),
  labels = c("Gainor<br>et al. 2017",
             "McCoach<br>et al. 2018","ROS1<br>Extrinsic",
             "ROS1<br>Extrinsic","ROS1<br>Intrinsic","ROS<br>Intrinsic","G2032R",
             "S1986F","D2033N","L1951R/<br>L2026M"),
  parents = c(NA,NA,"MGH","CU","MGH",
              "CU","MGH - KDM","MGH - KDM","MGH - KDM","CU - KDM"),
  values = c(17L, 12L, 8L, 11L, 9L, 1L, 7L, 1L, 1L, 1L),
  colors = c("#3182bd", "#e6550d", "#9ecae1", "#fdae6b", "#9ecae1",
             "#fdae6b", "#deebf7", "#deebf7", "#deebf7", "#fee6ce")
)

clin2_plot <- plot_ly(clin2, ids = ~ids, labels = ~labels, parents = ~parents, 
                      values = ~values, type = 'sunburst', branchvalues = 'total'
) %>% layout(sunburstcolorway = clin2$colors)

clin2_plot

This is the current output

Thanks in advance.

Community
  • 1
  • 1
clare
  • 45
  • 1
  • 5
  • Welcome to StackOverflow, can you please add `clin2`? Please take a look at [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), to modify your question, with a smaller sample taken from your data (check `?dput()`). Posting images of your data or no data makes it difficult to impossible for us to help you! – massisenergy Apr 10 '20 at 16:50
  • 1
    Just added some photos for clin2 and the current output of the code – clare Apr 10 '20 at 17:18
  • Great. But the `clin2` in the right format please. See previous comment to properly format & paste your data here. Someone here can reproduce your problem then. – massisenergy Apr 10 '20 at 17:36
  • Okay I added the csv as a code block. Thank you for the instruction, it's helpful – clare Apr 10 '20 at 17:52
  • Unfortunately, the data you've provided is not producing any output in my system! – massisenergy Apr 10 '20 at 20:17
  • Please check my answer. – ismirsehregal Apr 14 '20 at 19:25

1 Answers1

3

I edited your code to make it reproducible. To me it seems to be working fine. I added a black trace - Please check the following:

library(plotly)

clin2 <- data.frame(
  stringsAsFactors = FALSE,
  ids = c("MGH","CU","MGH - WT",
          "CU - WT","MGH - KDM","CU - KDM","MGH - G2032R",
          "MGH - S1986F","MGH - D2033N","CU - L1951R/L2026M"),
  labels = c("Gainor<br>et al. 2017",
             "McCoach<br>et al. 2018","ROS1<br>Extrinsic",
             "ROS1<br>Extrinsic","ROS1<br>Intrinsic","ROS<br>Intrinsic","G2032R",
             "S1986F","D2033N","L1951R/<br>L2026M"),
  parents = c(NA,NA,"MGH","CU","MGH",
              "CU","MGH - KDM","MGH - KDM","MGH - KDM","CU - KDM"),
  values = c(17L, 12L, 8L, 11L, 9L, 1L, 7L, 1L, 1L, 1L),
  colors = c("#111111", "#e6550d", "#9ecae1", "#fdae6b", "#9ecae1",
             "#fdae6b", "#deebf7", "#deebf7", "#deebf7", "#fee6ce")
)

clin2_plot <- plot_ly(clin2, ids = ~ids, labels = ~labels, parents = ~parents, 
                      values = ~values, type = 'sunburst', branchvalues = 'total'
) %>% layout(colorway = ~colors)

clin2_plot

Result

Jakob
  • 45
  • 7
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78