plotly.R
plotly.R copied to clipboard
Discrete axis fails to remove "interior" categories in response to filter interactions
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:

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!
@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)
)
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.
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.
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"))
)
I have the same issue with multicategory yaxis from python. setting categoryorder doen't do the trick for me...
fig.update_yaxes(
categoryorder='trace',
)