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

ggtitle height varies with descending letters #2288

Closed
WHMan opened this issue Oct 10, 2017 · 4 comments · Fixed by #2471
Closed

ggtitle height varies with descending letters #2288

WHMan opened this issue Oct 10, 2017 · 4 comments · Fixed by #2471
Labels
feature a feature request or enhancement themes 💃 wip work in progress

Comments

@WHMan
Copy link

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
Copy link
Member

hadley commented Oct 30, 2017

There isn't unfortunately 😞

@hadley hadley closed this as completed Oct 30, 2017
@clauswilke
Copy link
Member

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
Copy link
Member

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 added feature a feature request or enhancement themes 💃 wip work in progress labels Apr 27, 2018
hadley pushed a commit that referenced this issue May 1, 2018
…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
Copy link

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.
Labels
feature a feature request or enhancement themes 💃 wip work in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants