Search code examples
rfilterplotlybar-charthtmlwidgets

Unselected entries displayed on axis - Crosstalk+Plotly bar-chart



EDIT

This seems to be an issue already known to the plotly community github plotly issue #689 and there is an analogous question here on SO.

Unfortunately, it seems no solution is available yet. Any advice would be greatly appreciated.


I am trying to use Crosstalk and Plotly to create a dashboard and I have come across an unexpected behaviour. When selecting through the Crosstalk filter, the Plotly bargraph leaves "gaps" for the unselected entries.

As a reproducible example, let's say I want to compare cities populations, what I am getting is this (code at the bottom):

gaps in selection

It might very well be that I am missing something, is there a way to get rid of the gap? any advice on viable ways to do a similar comparison avoiding the issue?

Thanks in advance.

Code:

---
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)

#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
```

Solution

  • This worked for me - on the axis where it's happening, set categoryorder = "trace".

    e.g.,

    plot_ly(...) %>% layout(yaxis = list(categoryorder = "trace"))