How to remove empty factors from xaxis

 ̄綄美尐妖づ 提交于 2020-03-22 08:48:14

问题


Trying to make interactive plotly barchart with filter_select() and no-shiny work. I am working with data for a lot of airports (> 100). A barchart is typically too crowded to support the user to compare the performance observed (value VAL) at one airport (APT_x) to a subset of peers. The idea is to use a filter to have the user select the subset of airports.

# create a dummy table with data for year, airport, and oberved value
yr <- c(2017, 2018, 2019)
ap <- c("APT_1", "APT_2", "APT_3", "APT_N")

df <- expand.grid(YEAR = yr, APT = ap)
df$VAL <- c(10, 11, 12, 14, 9, 8, 7, 6, 2, 10, 12, 13)

library(plotly)
# shared data
df_sh <- highlight_key(df, key=~APT)

# filters
ap_filter <- filter_select(id="airport",label="filter airport", sharedData=df_sh, group=~APT)

# stacked bar chart
bc <- df_sh %>% plot_ly(x=~APT, y=~VAL, color=~factor(YEAR)) %>%
  group_by(APT) %>%
  add_bars() %>%
  layout(barmode = "stack")

# arrange plot
bscols(widths = c(3, 9)
       , ap_filter
       , bc
       )

Whenever more than one airport APT is selected, the x-axis shows all the entity-ticks between the bars. How can this be removed/surpressed? Obviously, in the following example, APT_2 should not be shown. Thanks for any pointers.

来源:https://stackoverflow.com/questions/60464144/how-to-remove-empty-factors-from-xaxis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!