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

Approach facet_grid(scale="free") + coord_flip issue #1756

Closed
wants to merge 1 commit into from

Conversation

vlsi
Copy link

@vlsi vlsi commented Sep 16, 2016

I've run into "well known" facet_grid(scale="free") + coord_flip() issue, and it took me a several hours to find a workaround, thus I decided to implement a proper fix.

The idea is to swap panel$layout$SCALE_X and panel$layout$SCALE_Y right after train_layout

The thing is facet_grid tries to keep the same ranges for all columns and rows,
however it does not know if the axes will be flipped, so when flip occurs
the previously chosen set of facets no longer applies.

The fix is to flip SCALE_X and SCALE_Y set of facets, so the ranges are properly computed

Fixes #1492, #1470, #1393, #95

The idea is to swap panel$layout$SCALE_X and panel$layout$SCALE_Y right after train_layout

The thing is facet_grid tries to keep the same ranges for all columns and rows,
however it does not know if the axes will be flipped, so when flip occurs
the previously chosen set of facets no longer applies.

The fix is to flip SCALE_X and SCALE_Y set of facets, so the ranges are properly computed

Fixes tidyverse#1492, tidyverse#1470, tidyverse#1393, tidyverse#95
@hadley
Copy link
Member

hadley commented Sep 16, 2016

I have to say I thought I was going to hate this, but it seems so simple that it's worth doing. It doesn't solve the general problem but it's a pragmatic solution to the case that most people encounter. Could you have a look at doing it for facet_grid() too?

And all the facetting code has been rewritten in #1633 but I think the basic idea will still work.

cc @thomasp85

@vlsi
Copy link
Author

vlsi commented Sep 16, 2016

Here's sample from #1470:

Test_df <- data.frame(Policy = rep(c("A", "B", "C", "D"), 4),
                      Measure = rep(c("Mixed Use", "Walking"), each = 8),
                      PctChg = c(10 * runif(8, 0, 1), 50 * runif(8, 0, 1)),
                      Model = rep(rep(c("RPAT", "RSPM"), each = 4), 2)
)

ggplot(data = Test_df, aes(x=Policy, y = PctChg, fill = Model)) + 
  geom_bar(stat = "identity", position=position_dodge()) +
  facet_grid(. ~ Measure, scales = "free") +
  coord_flip() +
  theme_bw() + 
  labs(y = "% Change from Reference") +
  labs(x = "") +
  theme(axis.title = element_text(size = 10))

Before:
before

After:
after

@vlsi
Copy link
Author

vlsi commented Sep 16, 2016

Could you have a look at doing it for facet_grid() too?

It works for both facet_grid and facet_wrap

@vlsi
Copy link
Author

vlsi commented Sep 16, 2016

I have to say I thought I was going to hate this, but it seems so simple that it's worth doing. It doesn't solve the general problem but it's a pragmatic solution to the case that most people encounter

Exactly. I feel what you mean by "facets and coordinate systems are not well designed to play together", however I think the fix is rather trivial, so it might have its purpose

@thomasp85
Copy link
Member

Thanks for this @vlsi - I'll add it to #1633 though in a slightly different way that doesn't modify the build code itself.

@thomasp85
Copy link
Member

This was fixed in #1633

@thomasp85 thomasp85 closed this Sep 17, 2016
@lock
Copy link

lock bot commented Jan 18, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent behaviour of coord_flip() with facets
3 participants