Skip to content

Axis category order for matrix-like plots #863

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

Closed
jrjhealey opened this issue Jan 26, 2017 · 1 comment
Closed

Axis category order for matrix-like plots #863

jrjhealey opened this issue Jan 26, 2017 · 1 comment
Labels

Comments

@jrjhealey
Copy link

After updating plotly recently, the behaviour of the default heatmap label order seemed to have changed.

See this SO post for the full details: http://stackoverflow.com/questions/41231318/prevent-plot-ly-reordering-matrix

I have since rectified by problems by coercing the names to all follow the form Tree##, however I never did find out whether plotly supports specified ordering of columns (e.g. an option to tell it to use the ordering of the input matrix specifically).

If this exists and I've not found it, could you point me in the right direction as I've found nothing helpful so far.

If it doesn't, could you consider it as a feature request for future versions, to give more control over axis label ordering?

@cpsievert
Copy link
Collaborator

cpsievert commented Mar 10, 2017

This was caused by a (poorly documented) intentional change. Just like in ggplot2, this change was intended to ensure categorical axis ordering either: (1) matches factor level ordering or (2) is sorted alphabetically (in the case of character strings). Thus, you can currently resolve the problem by ensuring the row/column names are factors:

library(plotly)
m <- matrix(rnorm(20 * 20), nrow = 20, ncol = 20)

rs <- rownames(m, FALSE, "r")
rsf <- factor(rs, levels = rs)
cs <- colnames(m, FALSE, "c")
csf <- factor(cs, levels = cs)
plot_ly() %>%
  add_heatmap(z = ~m, x = csf, y = rsf)

screen shot 2017-03-10 at 1 22 15 pm

That being said, I consider the current behavior when row/column names are strings to be a bug since the row/column names are reordered, but the z values aren't:

plot_ly() %>%
  add_heatmap(z = ~m, x = cs, y = rs)

screen shot 2017-03-10 at 1 20 32 pm

I see two ways to move forward:

(1) Reorder the z attribute appropriately.
(2) Avoid any reordering when a z attribute is provided.

The benefit of (1) is that the behavior would be consistent with other trace types (i.e., "scatter"), but it seems like you rarely would ever actually want that behavior, and doing it correctly in all cases seems hard, so I'm leaning towards (2).

@cpsievert cpsievert added the bug label Mar 10, 2017
@cpsievert cpsievert changed the title Ordering of axis labels (Feature request?) Axis category order for matrix-like plots Mar 10, 2017
cpsievert added a commit that referenced this issue Mar 24, 2017
avoid reordering axis categories when  is a matrix; fixes #863
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants