Skip to content

Fix the margins of subplots #622

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

alyst
Copy link
Contributor

@alyst alyst commented Jun 5, 2016

In master the subplot margins are subtracted from the given subplot width/heights. Since top-left subplot should start in (0,1) and bottom-right should end at (1,0), the left margin is not subtracted from the leftmost subplots and the right margin -- from the rightmost. The same for heights. As the result the proportions of the plots drawn differ from the ones given to subplot(): in comparison to outer subplots the inner ones are more narrow than they should be. See e.g.:

p <- ggplot( data.frame(x = rnorm(1000), y = rnorm(1000),
                   a = sample(1:5, 1000, replace=TRUE),
                   b = factor(sample(1:3, 1000, replace=TRUE)),
                   c = factor(sample(1:3, 1000, replace=TRUE))) ) +
  geom_point(aes(x=x, y=y, color=b, shape=c)) + facet_wrap(~ a )
ggplotly(p)

The PR changes how the get_domains() calculates the subplot positions. Instead of subtracting the margins from the subplot widths/heights, it rescales the width/heights so that the sum of margins and width/heights does not exceed 1.0 (the margins are not scaled since they are calculated wrt ticks/labels sizes). This approach preserves the relative subplot proportions. It looks like it also fixes the free_y case in #619.

There are 2 open issues related to this PR:

  1. For get_domains() there's no difference between left/right, only the left+right sum does matter. The same for top/bottom. So instead of expecting 4-element margins vector, get_domains() could be simplified to expect 2-element spaces vector.
  2. Margins take into account the size of labels converted from pixels to (0,1) range. Since the plot size on the server side (used for this conversion) could be different from the actual plot size on the client, the resulting layout might be incorrect. The current workaround is to double the margin sizes (see the comment in ggplotly()) to avoid subplot overlaps. But certain combinations of client/server plot sizes result in overly large margins and reduced subplots. Would be nice if at least upon redraws the server can get the actual plot dimensions.

@alyst
Copy link
Contributor Author

alyst commented Jul 26, 2017

I've just tried and with the current plotly version the subplot margins issue is still there (margins too wide, subplots have uneven sizes), so I've rebased the PR.

alyst added 2 commits August 11, 2017 13:17
- don't subtract the margins from the subplots width/height
  as both margins are subtracted from the inner subplots and
  only one margin from the outer ones, so the proportions are
  broken
- automatically scale widths/heights to fit into 0..1 when
  summed with the margins
- update subplot margins tests to the new logic (+ include the
  tests for inner subplots dimensions)
- add space for the title and axis only to one of the L/R or T/B
  margins
- add panel.space/2 to each margin side, not panel.space
@avsdev-cw
Copy link

avsdev-cw commented Oct 27, 2021

Edit: replaced with issue #2057

@nlooije
Copy link

nlooije commented Jun 13, 2022

I patched the latest release with these changes and the problem with the subplot margins seems to have been fixed.
Would be nice to have them merged into master some time soon

@janlimbeck
Copy link

Is somebody still working on this? The issue still seems to be present in the current version of plotly.

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

Successfully merging this pull request may close these issues.

4 participants