# Creating subtotal and total on a stacked bar chart with dropdown filter

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)
),
list(
type = 'filter',
target = ~letter,
operation = '=',
value = unique(df\$letter)
),
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,
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.value", sort(unique(df\$Species))),
label = sort(unique(df\$Species))),
list(method = "restyle",
args = list("transforms.value", sort(unique(df\$Species))),
label = sort(unique(df\$Species))),
list(method = "restyle",
args = list("transforms.value", sort(unique(df\$Species))),
label = sort(unique(df\$Species)))
)
),
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.value", sort(unique(df\$letter))),
label = sort(unique(df\$letter))),
list(method = "restyle",
args = list("transforms.value", sort(unique(df\$letter))),
label = sort(unique(df\$letter))),
list(method = "restyle",
args = list("transforms.value", sort(unique(df\$letter))),
label = sort(unique(df\$letter)))
)
)
)
)``````