4

My X axis label is too long, so I used xlab(expression(atop(paste("Hello world"^"TM ", ":Hi"),paste("hello again"^"TM",": Hi")))) to split it into two lines. The line spacing is too big, and I applied the method axis.title.x=element_text(lineheight=0.2) to change line spacing, but it did not change anything. The method was adapted from here. My question is how to change line spacing for xlab. Thank you in advance!

1
  • Hi @just_rookie! Have you found a suitable solution meanwhile? This seems to have been addressed as an issue on ggplot2's GitHub repo as well - #2043 Jan 12, 2019 at 17:21

2 Answers 2

1

To perfectly center everything (which \n will not do), keep every piece of text the same size whatever the number of lines and at the same time being able to adjust the interlinear space, use this instead:

xlab(expression(atop(textstyle("whateverline1"),atop(textstyle("whateverline2"),atop(scriptscriptstyle(""),textstyle("whateverline3"))))))

Then use labeller=label_parsed This also works for facet_grid, title and ylab

Note the atop and textstyle to position the text whilst keeping it all the same size and the scriptscriptstyle("") to control spacing between lines. You can also use varied relative sizes of text using scriptstyle or scriptscriptstyle depending on your needs and of course use axis.title.x=element_text(size=whatevernumber) in the theme section

1

1 - Solution with two atop calls

As mentioned here, every time we call atop() the text gets smaller, but also the gap between the lines. So for your two lines example, we could call atop() two times, which forces 3 lines, but we set the first line empty so that the next atop plots the text smaller and also the a smaller gap between the lines. Then we tweak a bit the text size and the margins of the title in theme:

Here the issue is illustrated - the gap between the two lines should be reduced.

library(ggplot2)

p <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
# Your usual atop call
label_1 <- expression(atop(x = paste("Hello world"^"TM ", ": Hi"),
                           y = paste("hello again"^"TM ", ": Hi")))
p + xlab(label_1)

Proposed solution with two atop calls:


# Trick with two atop calls
label_2 <- expression(atop(x = "", # Empty first line might do the trick
                           y = atop(x = paste("Hello world"^"TM ", ": Hi"),
                                    y = paste("hello again"^"TM ", ": Hi"))))
# Since the second atop forces smaller text (and also smaller gap between
# lines), then we need to increase the text manually with axis.title.x in
# theme(). Also adjust the top margin of the title since the first line is
# empty but still takes space.
p + xlab(label_2) + 
  theme(axis.title.x = element_text(size = 15,
                                    margin = margin(t = -2, r = 0, b = 0, l = 0,
                                                    unit = "mm")))


2 - Solution with cowplot::draw_label

Another hack, would be to call cowplot::draw_label two times (suggested in this discussion). Again, we need to tweak a bit the position and making space for the custom two lines axis title.

I gave more explanations about this approach and also using ggplot2::annotation_custom() in ggplot2 two-line label with expression.

library(ggplot2)
library(cowplot)
#> 
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggplot2':
#> 
#>     ggsave

p <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
# Make enough space for custom two lines axis title
p <- p + xlab("") + 
  theme(axis.title.x = element_text(size = 15,
                                    margin = margin(t = 7, r = 0, b = 0, l = 0,
                                                    unit = "mm")))

line_1 <- expression(paste("Hello world"^"TM ", ": Hi"))
line_2 <- expression(paste("hello again"^"TM ", ": Hi"))
# Or avoid paste()
# line_1 <- expression("Hello world" ^ "TM" ~ ": Hi")
# line_2 <- expression("hello again" ^ "TM" ~ ": Hi")

# Use cowplot::draw_label to plot two lines of text. Need to tweak a bit the
# position until we get it right.
ggdraw(p) + 
  draw_label(line_1, x = 0.55, y = 0.08) +
  draw_label(line_2, x = 0.55, y = 0.027)

Created on 2019-01-14 by the reprex package (v0.2.1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.