簡體   English   中英

如何只顯示 R plotly 中值大於 0 的類別?

[英]How to show only categories with values greater than 0 in R plotly?

我正在嘗試使用反應表在 shiny 應用程序中生成一個 plot。 在生成餅圖 plot 時,類別按預期顯示。 但是,當我嘗試生成條形圖時,我看到了我不想顯示的類別標簽,這些標簽是計數為 0 的類別。目前我在 xaxis 中看到條形圖的所有標簽。

下面是我用來生成餅圖和條形圖的代碼。 我只選擇前 10 行,所以我只想查看 plot 的 10 個類別。 餅圖很好,但條形圖是問題所在。

  output$fig_pie <- renderPlotly({
    fig <- req(summarised_frame()[1:10]) %>% plot_ly(labels = ~concept_name, values = ~count) %>% add_pie(hole = 0.6)

  })
  

  output$fig_bar_plotly <- renderPlotly({
    req(summarised_frame())
    
    p <- summarised_frame()[1:10]
    
    fig <- plot_ly(p, x = ~concept_name, y = ~count, type = "bar")
    fig
  })

在此處輸入圖像描述

在此處輸入圖像描述

這是用於繪圖的表

我能夠弄清楚這個問題。 我用來重新顯示 plot 的數據表將類別作為因子,因此即使我僅過濾/選擇了 10 行,它也保留了因子級別。 一旦我刪除了未使用類別的因素,條形圖就固定了。

output$fig_bar_plotly <- renderPlotly({
req(summarised_frame())

p <- summarised_frame()[1:10]

p$concept_name <- droplevels(p$concept_name)

fig <- plot_ly(p, x = ~concept_name, y = ~count, type = "bar")
fig

})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM