Hi, I have been struggling with bar charted with dropdown filter for weeks. Most solutions I found either doesn’t involved dropdown or only have very simple filter.
Here’s what I have currently:
http://rpubs.com/wiseleywu/523504
My problem is that I couldn’t find a way to create a subtotal based on category X,Y,Z, and a total based on date.
Currently I’m doing three transform (2 filters for the dropdown and 1 groupby for X,Y,Z). Am I missing something? Am I supposed to calculate subtotal/total from scratch? Below is my sample code. Thanks for the help!
p <- df %>%
plot_ly(
type = 'bar',
x = ~date,
y = ~Petal.Length,
# text = ~Species,
# letter = ~letter,
# showlegend=TRUE,
# name = ~feature,
hovertext=paste(df$feature, ": ", "y"),
# hovertext = paste("Species: ", df$Species, "<br>Letter: ", df$letter, "<br>feature: ", df$feature),
transforms = list(
list(
type = 'filter',
target = ~Species,
operation = '=',
value = unique(df$Species)[1]
),
list(
type = 'filter',
target = ~letter,
operation = '=',
value = unique(df$letter)[1]
),
list(
type = 'groupby',
groups = ~feature,
styles = list(
list(target = 'X', name='X', value = list(marker =list(color = 'blue'))),
list(target = 'Y', name='Y', value = list(marker =list(color = 'red'))),
list(target = 'Z', name='Z', value = list(marker =list(color = 'black')))
)
)
# list(
# type = 'aggregate',
# groups = ~feature,
# aggregations = list(
# list(target='y', func = 'sum', enabled = T)
# )
# )
)) %>% layout(
barmode='stack',
showlegend=T,
updatemenus = list(
list(
type = 'dropdown',
active = 0,
pad = list('r'= 0, 't'= 10, 'b' = 10),
x = 0.5,
y = 1.27,
buttons = list(
list(method = "restyle",
args = list("transforms[0].value", sort(unique(df$Species))[1]),
label = sort(unique(df$Species))[1]),
list(method = "restyle",
args = list("transforms[0].value", sort(unique(df$Species))[2]),
label = sort(unique(df$Species))[2]),
list(method = "restyle",
args = list("transforms[0].value", sort(unique(df$Species))[3]),
label = sort(unique(df$Species))[3])
)
),
list(
type = 'dropdown',
active = 0,
pad = list('r'= 0, 't'= 10, 'b' = 10),
x = 0.1,
y = 1.27,
buttons = list(
list(method = "restyle",
args = list("transforms[1].value", sort(unique(df$letter))[1]),
label = sort(unique(df$letter))[1]),
list(method = "restyle",
args = list("transforms[1].value", sort(unique(df$letter))[2]),
label = sort(unique(df$letter))[2]),
list(method = "restyle",
args = list("transforms[1].value", sort(unique(df$letter))[3]),
label = sort(unique(df$letter))[3])
)
)
)
)