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

ggtitle height varies with descending letters #2288

Closed
WHMan opened this Issue Oct 10, 2017 · 4 comments

Comments

Projects
None yet
3 participants
@WHMan

WHMan commented Oct 10, 2017

Hi there,

I just noticed that the height of a ggplot2 main title depends on the letters used. Although there is no difference in the title height (and therefore the dimensions of the plot itself) between titles with only typical sized letter (e.g. "arrr") or with ascenders (e.g. "brrr"), the title height increases with the inclusion of descenders (e.g. "prrr"). Although I understand that as a workaround I can use all-caps or readjust the plot height with ggplotGrob, I wonder whether I missed a parameter (within theme?) that could fixate the title height independent on the letters used.

library(ggplot2)
library(gridExtra)
grid.arrange(ggplot() + ggtitle("arrr"),
             ggplot() + ggtitle("brrr"),
             ggplot() + ggtitle("prrr"),
             nrow = 1)

Thanks in advance!

@hadley

This comment has been minimized.

Member

hadley commented Oct 30, 2017

There isn't unfortunately 😞

@hadley hadley closed this Oct 30, 2017

@clauswilke

This comment has been minimized.

Member

clauswilke commented Mar 3, 2018

I've run into this problem also and I debugged it in quite some detail. I'm writing up my findings here in the hope they may be useful to somebody. I don't have the time right now to prepare a proper pull request but may be able to do so in the future.

The problem is caused by this line:

descent <- descentDetails(text_grob)

The function descentDetails() returns different values depending on the text grob it is run on:

> descentDetails(textGrob("a"))
[1] 0.001953125inches
> descentDetails(textGrob("g"))
[1] 0.0350748697916667inches

A simple fix would be to replace the string in the text grob with a constant string that is always used. This should return the same descent height at all times, only dependent on font metrics and independent on the specific label used. So, we could replace this line:

descent <- descentDetails(text_grob)

with these two lines:

temp <- editGrob(text_grob, label = "gjpqyQ")
descent <- descentDetails(temp)

to get consistent descent values regardless of the text label shown in the plot.

@clauswilke

This comment has been minimized.

Member

clauswilke commented Mar 3, 2018

Here's the effect of this proposed patch.

Example 1:

library(ggplot2)
library(gridExtra)
grid.arrange(ggplot() + ggtitle("arrr"),
             ggplot() + ggtitle("grrr"),
             nrow = 1)

Current ggplot2:
screen shot 2018-03-02 at 10 37 18 pm

With patch:
screen shot 2018-03-02 at 10 36 46 pm

Example 2:

p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point()
grid.arrange(p + ggtitle("arrr") + scale_color_discrete(name = "abcd"),
             p + ggtitle("grrr"),
             nrow = 1)

Current ggplot2:
screen shot 2018-03-02 at 10 35 50 pm

With patch:
screen shot 2018-03-02 at 10 36 29 pm

Note that this also fixes the layout of the legend title.

@hadley hadley reopened this Apr 27, 2018

@hadley hadley closed this in #2471 May 1, 2018

hadley added a commit that referenced this issue May 1, 2018

Calculate constant descender heights, regardless of label content. (#…
…2471)

* Calculate constant descender heights, regardless of label content. Fixes #2288

* Make themes more visually consistent. Also fix spacing bug for multi-line legend titles.

* gracefully handle missing fontsize in theme (e.g., if legend.title has been set to element_blank())

* update vdiffr templates
@lock

This comment has been minimized.

lock bot commented Oct 28, 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 28, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.