gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:45:55 25 4
gpt4 key购买 nike

在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com