R Plotly 无法从条形图中删除跟踪 0

标签 r shiny plotly

在我的 Shiny 应用程序中, plotly 生成 trace 0在使我的图表不平衡的图例中。

这就是图表的样子(注意图例中的 trace 0)。
enter image description here

但是点击 trace 0在图例中,图表恢复正常

enter image description here

有没有办法去除这个trace 0从我的 plotly 完全?

这是我的代码:

1) 我的数据框首先在 reactive 中过滤功能

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) 然后对另一个 reactive 中的过滤数据帧进行更多更改功能
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) 最后我使用 plot_ly构建条形图。然而trace 0无法移除
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"))
})

对此的任何帮助将不胜感激。
对于无法包含可重现的数据,我提前表示歉意。

最佳答案

你的方法有问题。

检查以下可重现的代码以修复您的代码。

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

enter image description here

理想情况下,您修改后的代码应该是这样的:
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"))
})

关于R Plotly 无法从条形图中删除跟踪 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46606067/

相关文章:

r - 将列排序到类别中

r - 使用自定义图像而不是 R 折线图标记的标准形状

sql - 如何根据时间戳和值查找同步ID

r - 避免代码块破坏 Knitr? (最好使用 block 选项)

R Shiny : Unable to dispatch task for application, 有 1 个任务正在进行中

r - Shiny 中带有 htmltools 的 dygraphs 列表

r - plotly - 不同表面的不同颜色

javascript - 在 x = 0 时执行 Math.pow(10,x) 时使用 javascript 更改 R Shiny 错误中的 noUIslider 标签

在 Shiny 的应用程序中异步渲染绘图

r - 使用 R 中的plot_ly 单独更改图例