繁体   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