首页 > 解决方案 > 未选择的条目显示在轴上 - Crosstalk+Plotly 条形图

问题描述


编辑

这似乎是 plotly 社区 github plotly issue #689已经知道的问题,在SO 上有一个类似的问题。

不幸的是,似乎还没有可用的解决方案。任何建议将不胜感激。


我正在尝试使用 Crosstalk 和 Plotly 创建仪表板,但遇到了意想不到的行为。通过 Crosstalk 过滤器进行选择时,Plotly 条形图会为未选择的条目留下“间隙”。

作为一个可重复的例子,假设我想比较城市人口,我得到的是这个(底部的代码):

选择差距

很可能是我遗漏了一些东西,有没有办法摆脱差距?关于避免该问题的类似比较的可行方法的任何建议?

提前致谢。

代码:

---
title: "Crosstalk+Plotly bargraph selection"
---

```{r setup, include=FALSE}
options(stringsAsFactors = FALSE)
library(crosstalk) 
library(dplyr)
library(plotly)  

#data on cities' population
city_pop <- data.frame("City" = c("Florence",  "Milan", "Venice"),
                    "Population" = c(382258, 1352000, 261905))

#setting up Crosstalk shared data
sd <- SharedData$new(city_pop, key = city_pop$city)

#filter for the cities
filt <- filter_select(
  id = "select_name",
  label = "Selected City",
  sharedData = sd,
  group = ~City,
  selected = "")

#barplot of cities' population
bars_pop <- plot_ly(sd, x = ~City, y = ~Population) %>% 
add_bars(width=0.2,
           x =  ~City,
       y =  ~Population,
       color = I("#89CFF0"),
       name = "",
       opacity=.9,
       hoverinfo = 'y',
       hovertemplate = paste('%{x} <br> number of Residents: %{y}<extra></extra>')
       ) 



```

```{r, echo=FALSE}
filt

bars_pop
```

标签: rfilterplotlybar-charthtmlwidgets

解决方案


推荐阅读