How to remove 0 y values plotly R?

Viewed 20

I have the following code and dataframe :

datasku = data.frame(matrix(ncol = 3, nrow = 12))
names_col = c("product", "Bad Waitress", "Black Pumas")
colnames(datasku)  = names_col
datasku$product <- c("x","y","s","u","i","o","l","m","n","k","b","c")
artists <- c("Bad Waitress", "Black Pumas")
datasku$`Bad Waitress`<- c(23,40,0,0,0,0,0,0,10,0,0,0)
datasku$`Black Pumas` <- c(0,40,0,0,0,65,0,0,10,0,0,0)

 product Bad Waitress Black Pumas
1        x           23           0
2        y           40          40
3        s            0           0
4        u            0           0
5        i            0           0
6        o            0          65
7        l            0           0
8        m            0           0
9        n           10          10
10       k            0           0
11       b            0           0
12       c            0           0

show_vec = c()

for (i in 1:length(artists)){
  show_vec = c(show_vec,FALSE)
}
get_menu_list <- function(artists){
  n_names = length(artists)
  buttons = vector("list",n_names)
  
  for(i in seq_along(buttons)){
    show_vec[i] = TRUE
    buttons[i] = list(list(method = "restyle",
                           args = list("visible", show_vec),
                           label = artists[i]))
    print(list(show_vec))
    show_vec[i] = FALSE
  }
  
  return_list = list(
    list(
      type = 'dropdown',
      active = 0,
      buttons = buttons
    )
  )
  
  return(return_list)
}
print(get_menu_list(artists)) 
fig <- plot_ly(data=datasku, x = ~product, y = ~`Bad Waitress`,type = 'bar',
               transforms = list(
                 list(
                   type = 'filter',
                   target = 'y',
                   operation = '>',
                   value = 0
                 )),
               hovertemplate = paste('<i>Popularity</i>: %{y:.2f}%',
                                     '<br><i>Product</i>: %{x}<extra></extra><br>'))
fig <- fig %>% add_trace(y = ~`Black Pumas`)
fig <- fig %>% layout(showlegend = F,yaxis = list(title = 'Count'), barmode = 'group',
                      updatemenus = get_menu_list(artists))
fig

What this code does is basically create a plotly barchart with dropdown menus corresponding to the artists names. When clicking an artist it should only show the products which have a values >0 but it is partially working and I can't understand why. If I run the code and select Bad Waitress this is what I obtain :

enter image description here

As you can see some columns are removed (since they have 0 value) but others, which have 0 value they are still shown despite the filter. How can I solve this problem?

Thank you

0 Answers
Related