11

I have been struggling with this issue for days if someone can help me with. I have a table data which has a column 'results' with values "High", "Medium" or "low". I am trying to create a pie chart using plotly by counting the number of High, Medium and Low in the data set and trying to assign a color to each category. Below is my code. I have tried cols1 and cols2 as my marker and several other ways but nothing seems to work. Please keep in mind that this is a dynamic table so there might be cases when there is no High or no Medium etc. so I just cannot use list(c("tomato","ornage","olivedrab") as the marker. Color has to be tied to the result category.

 tab <- count(data, results)

  tab <- transform(tab,
                   results_ord  = factor(
                     results ,
                     levels = c('High', 'Medium', 'Low'),
                     ordered = TRUE
                   ))
  cols1 <-
    c(
      High = "olivedrab",
      Medium = "orange",
      Low = "tomato"
    )
  cols2 <-
    c(ifelse(
      tab$results_ord == "High",
      "olivedrab",
      ifelse(
        tab$results_ord == "Medium",
        "orange",
        "tomato"
      )
    ))


  plot_ly(
    tab,
    labels = results_ord,
    values = n,
    marker = list(cols2),
    type = "pie"
  ) %>%
    layout(title = "Results")

  thanks,

Manoj Agrawal

Manoj Agrawal
  • 775
  • 3
  • 8
  • 20
  • I tried this but did not get a chart back cols1 <- c( "olivedrab", "orange", "tomato" ) plot_ly( tab, labels = results_ord, values = n, marker = marker(color=results_ord,colour=list(cols1)), type = "pie" ) %>% layout(title = "Results") can you please tell me the correct syntax? – Manoj Agrawal Oct 12 '16 at 17:02

1 Answers1

12

This syntax is for 4.x version of plotly, but you can adjust accordingly for older verison. Here is how you can get manual colors to show up:

df <- count(mtcars, am)
plot_ly(df, values = ~n, labels = ~factor(am), marker = list(colors = c('#FF7F0E', '#1F77B4')), type = 'pie')

You can adjust factor orders to get desired color for the desired value.

Community
  • 1
  • 1
Gopala
  • 10,363
  • 7
  • 45
  • 77
  • 1
    Gopala, as soon as one or more categories are removed the color scheme goes haywire because colors are not tied to the category.In ggplot you can do something like this cols <- c( 'High Performers' = "olivedrab", Underperformers = "orange", 'No Sales' = "steelblue" ) is there something similar that we can do in plotly? – Manoj Agrawal Oct 12 '16 at 20:23
  • 1
    Not that I am aware of. But, you can generate the 'colors' vector conditionally ahead and save it to a variable and then feed it. – Gopala Oct 13 '16 at 21:55
  • @Gopala where can the default colors in plotly be found? I want to try to match colors from a plot in plotly to another plot in ggplot – sar Apr 11 '20 at 17:06
  • 1
    Perhaps this answer can help? https://stackoverflow.com/questions/40673490/how-to-get-plotly-js-default-colors-list – Gopala Apr 12 '20 at 18:26