Plotly: Discrete axis fails to remove "interior" categories in response to filter interactions

Created on 19 Aug 2016  路  6Comments  路  Source: ropensci/plotly

I am using the example from the plotly page to generate a bar plot. I am also using plotly devel version 4.3.4. Here is the code:

library(plotly)

df <- data.frame(x = as.factor(LETTERS[1:5]), 
                 y = sample(10:20, size = 5))

# First change factor levels
df$x <- factor(df$x, levels = c("C", "B", "A", "D", "E"))

# Plot
p <- ggplot(df, aes(x, y, fill = x)) + 
  geom_bar(stat = "identity") +
  ggtitle("Bar Chart with changed factor levels")

ggplotly(p)


> sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.5 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plotly_4.3.4         ggplot2_2.1.0        BiocInstaller_1.23.6

On the plot.ly page, when I click on one of the bars in the middle (blue/green) the plot gets autoscaled and the placeholders disappear. But when I generate the plot using the code in R and click on the middle bars, the placeholders are still there.

Here I have clicked A to remove that bar but as you can see the placeholder remains:

screen shot 2016-08-17 at 5 29 01 pm

When I click on the bars at either end, the plot auto-adjusts and placeholders are removed. How can I remove the placeholders when clicking one of the middle bars?

Thanks!

bug ggplotly plotly.js

Most helpful comment

I ran today into the same issue and am wondering whether this is sorted or there is a work around to have the unused categories removed/xaxis refreshed?
No much joy at SO on this as well: https://stackoverflow.com/questions/60464144/how-to-remove-empty-factors-from-xaxis.
Thanks for an update, RQ

All 6 comments

@cpsievert I know you are quite busy but just wanted to follow up on this. This is driving me crazy as this works on plot.ly webpage and doesn't work when I try it.

This should work with ggplotly(p, dynamicTicks = T) (but currently doesn't)

Actually, due to the way layout.axisid.categoryarray works, https://github.com/ropensci/plotly/issues/689#issuecomment-292345482 won't work either (at least in the near term).

BTW, categoryarray is the only way we can guarantee the ordering of categories will match what you get from ggplot2...

I've made a feature request to plotly.js to see if we can have "dynamic ticks" even when categoryarray has been specified -- https://github.com/plotly/plotly.js/issues/1618

Adding plotly.js tag since this will be an issue for plot_ly() as well:

library(crosstalk)
sd <- SharedData$new(df)
bscols(
  filter_select("id", "Categories", sd, ~x),
  plot_ly(sd, x = ~x, y = ~y) %>% add_bars(color = ~x)
)

filter

I ran today into the same issue and am wondering whether this is sorted or there is a work around to have the unused categories removed/xaxis refreshed?
No much joy at SO on this as well: https://stackoverflow.com/questions/60464144/how-to-remove-empty-factors-from-xaxis.
Thanks for an update, RQ

I closed issue #1709 since it is a duplicate of #689 (thanks @rainer-rq-koelle for pointing that out) and linked this thread to my SO question.
Any update or advice more than welcome.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

viniciusvgp picture viniciusvgp  路  5Comments

kickbox picture kickbox  路  3Comments

escorial82 picture escorial82  路  6Comments

ratnanil picture ratnanil  路  3Comments

msummersgill picture msummersgill  路  3Comments