Skip to content

crosstalk::filter_select() and colors for plotly pie/donuts #1503

@robertmitchellv

Description

@robertmitchellv

First, I want to say thank you for all your hard work. The R API for plotly is wonderful and I think it's a fantastic package and tell everyone to use it! Keep up the great work!

Now, the strange behavior that feels buggy. I've been running into strange behavior with pie/donut charts and plotly. I'm building flexdashboards for small projects at work (folks really want donut charts) and hitting a wall. Some projects have specific color palettes, so I'm trying to incorporate those into the plots, but I'm seeing some strange behavior that doesn't seem expected.

When I use both crosstalk::filter_select() with plotly::plot_ly(markers = list(colors = colors)), I am not able to get the colors to stick. The example below is to illustrate the problem where the orange and the blue default colors seem to creep back in and I'm not sure why.

library(tidyverse)
library(crosstalk)
library(plotly)

cars <- mtcars %>%
  rownames_to_column("car") %>%
  mutate(car = str_split_fixed(car, " ", 2)[,1]) %>%
  group_by(car) %>%
  summarise(count = n())

shared_cars <- SharedData$new(cars)
colors <- c('rgb(211,94,96)', 'rgb(128,133,133)', 'rgb(144,103,167)', 'rgb(171,104,87)', 'rgb(114,147,203)')

bscols(widths = c(3,NA),
  list(
    filter_select(id = "car_select", label = "Select cars", sharedData = shared_cars, ~car)
  ),
  shared_cars %>% 
    plot_ly(labels = ~car, values = ~count) %>%
    add_pie(
      hole = 0.4,
      marker = list(
        colors = colors
      )
    ) %>%
    layout(title = "Donut charts using Plotly",  showlegend = F,
           xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
           yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
)

If I've done anything wrong with creating this issue let me know and I'll make corrections. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions