Skip to content

Colorbar fixes; closes #2397 and #2398 #2415

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

Merged
merged 7 commits into from
May 2, 2018

Conversation

clauswilke
Copy link
Member

@clauswilke clauswilke commented Jan 21, 2018

This pull request closes #2397 and #2398.

Colorbar legends work in all positions and use the same spacings as regular legends:

p <- ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species, fill = Petal.Width)) + 
  geom_point(pch = 21) +
  guides(color = guide_legend(title = "Petal.Width")) +
  theme(plot.margin = margin(10, 10, 10, 10),
        plot.background = element_rect(color = "black"))

cowplot::plot_grid(p + theme(legend.position = "left"), p + theme(legend.position = "right"),
                   p + theme(legend.position = "top"), p + theme(legend.position = "bottom"), scale = .95)

screen shot 2018-01-20 at 6 14 51 pm

(I renamed the color guide as "Petal.Width" because this title has a different height than "Species", which has a descent. Without this renaming the guides don't look equivalent but that's not the fault of guide_colorbar().)

And gaps between the title and the colorbar can be set in horizontal and vertical contexts and the gap is of the size specified:

ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Petal.Width)) + geom_point() +
  scale_color_distiller(name = "gap width = 1cm") +
  theme(legend.spacing.x = grid::unit(1, "cm"),
        legend.position = "top")

screen shot 2018-01-20 at 5 18 12 pm

ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Petal.Width)) + geom_point() +
  scale_color_distiller(name = "gap width = 1cm") +
  theme(legend.spacing.y = grid::unit(1, "cm"))

screen shot 2018-01-20 at 5 18 27 pm

One problem that I see remaining is that with the default theme the legend title sits too close to the bar:

ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Petal.Width)) + geom_point()

screen shot 2018-01-20 at 5 24 33 pm

But this is a problem with how theming and title grobs work, not with the colorbar code. And the problem is the same in both guide_colorbar() and guide_legend(), only that it's slightly less obvious in the latter. And the problem exists in the same form in the current ggplot2, so this is not a regression.

@clauswilke
Copy link
Member Author

This pull request will have to be modified slightly to be compatible with pull request #2471 or vice versa. I'm happy to modify either one once the other has been integrated into master. I can't easily merge the two into one, because I didn't know that I should make a separate branch when I set up this pull request.

@clauswilke
Copy link
Member Author

Would it be possible to move ahead and integrate this patch? It fixes some rather annoying bugs in guide_colorbar(), and several other patches (#2541, #2471) will need to be modified once this fix has been made. I think the right order would be to integrate this one first and the modify and integrate the others.

@hadley
Copy link
Member

hadley commented May 1, 2018

Ooops, sorry I missed your comment and merged in a suboptimal order. This looks good to go if you want to merge/rebase, and a visual test would be lovely if you wanted to add one.

@clauswilke
Copy link
Member Author

That's fine. I'll rebase, fix the existing visual tests, and add one or two more.

@clauswilke
Copy link
Member Author

clauswilke commented May 2, 2018

I rebased and added three visual tests, one for multi-line legend titles, one for custom settings of legend.spacing.x, and one for custom settings of legend.spacing.y. In doing so, I found an inconsistency between guide_legend() and guide_colorbar(), in that guide_legend() would divide legend.spacing.x by two. I have fixed that.

I have also carefully looked over the default settings for vertical and horizontal spacing. The original settings had two problems: (i) They were too large, in my opinion; (ii) horizontal and vertical defaults were different, which caused strange layouts in some cases. I have fixed both, but arguably have made the spaces too small. I'm happy to tweak further. Below is the current default as of this commit.

p <- ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species, fill = Petal.Width)) + 
       geom_point(pch = 21) +
       theme(plot.margin = margin(10, 10, 10, 10),
         plot.background = element_rect(color = "black"))

cowplot::plot_grid(p + theme(legend.position = "left"), p + theme(legend.position = "right"),
                   p + theme(legend.position = "top"), p + theme(legend.position = "bottom"), scale = .95)

screen shot 2018-05-01 at 10 25 13 pm

(Basically, I tried 0.5*fontsize and it was too large, and now I'm using 0.25*fontsize but it may be too small. We could use something in between, but it would be a unit that isn't anywhere else in the theme.)

@clauswilke
Copy link
Member Author

Upon further reflection, I removed a division by two and replaced 0.5 with 0.25, so those two actions cancel exactly. But only for vertical gaps. For horizontal gaps the changes are more substantial.

In any case, this is what it would look like with 0.5*fontsize:
screen shot 2018-05-01 at 11 20 12 pm
Maybe that's the better option.

In general, I think spacing and layout of legends needs a lot more work and careful thought, but that's for a future release.

@hadley
Copy link
Member

hadley commented May 2, 2018

That looks a lot better, thanks!

@hadley
Copy link
Member

hadley commented May 2, 2018

Can you please add a bullet to NEWS? It should briefly describe the change (starting with name of the function), and crediting yourself with (@yourname, #issuenumber).

@clauswilke
Copy link
Member Author

I've changed the code so the default vgap and hgap is 0.5 * fontsize, and I've updated NEWS.md.

@hadley hadley merged commit d25ae78 into tidyverse:master May 2, 2018
@hadley
Copy link
Member

hadley commented May 2, 2018

Thanks!

@lock
Copy link

lock bot commented Oct 29, 2018

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 Oct 29, 2018
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.

The colorbar guide (guide_colourbar) incorrectly uses a vertical unit in a horizontal setting
2 participants