Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discrete axis fails to remove "interior" categories in response to filter interactions #689

Open
komalsrathi opened this issue Aug 19, 2016 · 9 comments

Comments

@komalsrathi
Copy link

komalsrathi commented Aug 19, 2016

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!

@komalsrathi
Copy link
Author

@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.

@cpsievert
Copy link
Collaborator

cpsievert commented Apr 6, 2017

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

@cpsievert
Copy link
Collaborator

Actually, due to the way layout.axisid.categoryarray works, #689 (comment) 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 -- plotly/plotly.js#1618

@cpsievert cpsievert changed the title R plotly ggplot2: Remove placeholders when removing middle bars in geom_bar Discrete axis fails to remove "interior" categories in response to filter interactions Apr 21, 2017
@cpsievert
Copy link
Collaborator

cpsievert commented Apr 21, 2017

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

@rainer-rq-koelle
Copy link

rainer-rq-koelle commented Feb 29, 2020

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

@dgdi
Copy link

dgdi commented Mar 5, 2020

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.

@sebastianrosado
Copy link

Hey @dgdi is there a droplevels() equivalent in pandas for this purpose? I'm having the same issue and not sure where to fit that in for testing. Thanks.

@dgdi
Copy link

dgdi commented Mar 12, 2022

My SO question got an answer solving this issue. All that is needed is to set categoryorder = "trace" in the layout of the axis you are interested in.
For the example of @komalsrathi & @cpsievert that is:

library(plotly)
library(crosstalk)
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"))
sd <- SharedData$new(df)
# arrange plot
bscols(
  filter_select("id", "Categories", sd, ~x),
  plot_ly(sd, x = ~x, y = ~y) %>% 
    add_bars(color = ~x) %>% 
    layout(xaxis = list(categoryorder = "trace"))
)

@BnY
Copy link

BnY commented Sep 13, 2023

I have the same issue with multicategory yaxis from python. setting categoryorder doen't do the trick for me...

fig.update_yaxes(
        categoryorder='trace',
 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants