Reputation: 8447
I use plotly
to make some bar charts. Sometimes there are zero value bars. Because I use plotly in an interactive flexdashbard I don't know in advance whether there are zero value bars or not. How can I let plotly remove those bars from the final plot?
This is een example of code I use:
library(plotly)
set.seed(123)
variables <- c("var1","var2","var3","var4")
df <- data.frame(number = sample(1:10,100,replace = TRUE),
group1 = sample(1:4, 100, replace = TRUE),
group2 = sample(1:6,100, replace = TRUE),
variable = variables[sample(1:4, 100 ,replace = TRUE)],
score = sample(1:20, 100, replace = TRUE), stringsAsFactors = FALSE) %>%
mutate(score=ifelse(variable == "var1", 0, score))
fig <- plot_ly(df,
y = ~variable, x = ~score,
type = 'bar',
orientation = 'h',
mode = 'markers',
transforms = list(
list(
type = 'aggregate',
groups = ~variable,
aggregations = list(
list(
target = 'x', func = 'sum', enabled = T
)
)
)
)
)
fig
As you can see the var1 bar has value 0. How can I let plotly remove the var1 bar?
Upvotes: 2
Views: 2445
Reputation: 8447
Okay, I figured it out myself.
The thing I need to do is add this piece of code in the transforms list:
list(
type = 'filter',
target = ~score,
operation = '>',
value = 0
),
Upvotes: 1