In my Shiny app, plotly produces a trace 0
in the legend that imbalances my graph.
This is what the graph looks like (notice the trace 0
in the legend).
However on clicking on trace 0
in the legend, the graph returns back to normal
Is there a way of removing this trace 0
from my plot completely?
Here is my code:
1) My dataframe is first filtered inside a reactive
function
global_evolution=reactive({
results_combined %>%
filter(!is.na(SVM_LABEL_QOL) & SVM_LABEL_QOL=='QoL' & globalsegment==input$inp_pg1segment & Account==input$inp_pg1clientsfiltered & Date >=input$inp_pg1daterange[1] & Date <=input$inp_pg1daterange[2]) %>% #Input: Account
select(Account,Date,SVM_LABEL_DIMENSION) %>%
mutate(Month=month(as.Date(format(as.POSIXct(Date),format = "%d/%m/%Y"),"%d/%m/%Y"))) %>%
select(Account,Month,SVM_LABEL_DIMENSION,-Date) %>%
group_by(Month,SVM_LABEL_DIMENSION) %>%
summarise(Monthly_Count=n()) %>%
spread(SVM_LABEL_DIMENSION,Monthly_Count) %>%
ungroup() %>%
mutate(Month=month.abb[Month]) %>%
mutate_all(funs(replace(., is.na(.), 0)))
})
2) Then some more changes are made to the filtered dataframe inside another reactive
function
global_evolution_final=reactive({
global_evolution() %>%
mutate(Month=factor(Month,levels=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")))
})
3) Finally I use plot_ly
to construct the bar chart. However the trace 0
cannot be removed
output$pg1evolution <- renderPlotly({
colNames <- names(global_evolution_final())[-1] #Assuming Month is the first column
p <- plotly::plot_ly(data = global_evolution_final(), x = ~Month, type = "bar")
for(trace in colNames){
p <- p %>% plotly::add_trace(y = as.formula(paste0("~`", trace, "`")), name = trace)
}
p %>%
layout(title = "Trend Over Time",showlegend = FALSE,
xaxis = list(title = ""),
yaxis = list (title = "Monthly Count of QoL Tweets"))
})
Any help with this would be greatly appreciated. My apologies in advance for not being able to include reproducible data.
There's a problem with your approach.
Check this below reproducible code to fix yours.
df <- iris
p <- plotly::plot_ly()
colNames <- names(df)
colNames <- colNames[-which(colNames == 'Species')]
for(trace in colNames){
p <- p %>% plotly::add_trace(data= df, x = ~ Species, y = as.formula(paste0("~`", trace, "`")), name = trace)
print(paste0("~`", trace, "`"))
}
p
Ideally, Your modified code should be something like this:
output$pg1evolution <- renderPlotly({
colNames <- names(global_evolution_final())[-1] #Assuming Month is the first column
p <- plotly::plot_ly()
for(trace in colNames){
p <- p %>% plotly::add_trace(data = global_evolution_final(), x = ~Month, y = as.formula(paste0("~`", trace, "`")), name = trace, type = "bar")
}
p %>%
layout(title = "Trend Over Time",showlegend = FALSE,
xaxis = list(title = ""),
yaxis = list (title = "Monthly Count of QoL Tweets"))
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With