1

I have a y-axis title that goes across multiple lines. Each line on the y-axis title should start at the same location (prefer it works within a function that may have axis title length vary and plot size change).

See chart:

Gist with code of function.

UPDATE:
Using str_pad, with side = "right" and text element_text(family="mono") (font with a consistent point size across characters) also works, e.g.:

library(tidyverse)
quo_name_exprs_rplcmnt <- c(strrep("a", 9), strrep("b", 6), strrep("c", 3))
y_level <- c("Box and whiskers: ", "Coarse means: ", "Coarse means:")

(y_axis <- paste0(y_level, quo_name_exprs_rplcmnt) %>% 
  str_pad(width = max(str_length(.)), side = "right") %>% 
  str_c(collapse = "\n"))
#> [1] "Box and whiskers: aaaaaaaaa\nCoarse means: bbbbbb       \nCoarse means:ccc           "

ggplot() + 
  labs(y = y_axis)+
  theme(text = element_text(family="mono"))

Created on 2019-02-23 by the reprex package (v0.2.1)

If you don't want to use the default mono font of "TT Courier New", here are threads on which fonts have the same width for every character and Changing fonts in ggplot2 . Though would still prefer a solution that does not constrain font type...

Bryan Shalloway
  • 748
  • 7
  • 15

1 Answers1

2

This seems like a bit of a hack, but then using paste0 with "\n"'s is also a bit of hack compared with using "true" plotmath, so perhaps it will be useful. Pad each of the lines with leading spaces and then use hjust=0

 # change the y value to --->
y = paste0(     "                                 ","Box and whiskers: ", quo_name(y_expr),
           "\n","                                 ","Granular means: ", quo_name(gran_expr),
           "\n","                                 ","Coarse means: ", quo_name(coarse_expr), "     ")


 flights %>% 
   box_box_box_plot(arr_delay_log, carrier, quarter, day)+
   ggtitle("Quarter 2 typically has the worst delays")+theme(axis.title.y=element_text(hjust=0))

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks for the post. This works, though I don't like having to tinker with the front spaces... though I updated the gist link with roughly this implemented. What do you mean by "true" plotmath exactly? – Bryan Shalloway Feb 23 '19 at 18:41
  • 1
    I really made a great effort to find some sort of "box alignment" capability in axis titles and came up with nothing other than `vpos` and `hpos` which didn't give your desired results. Plotmath is the use of R expressions to include mathematical symbols. See `?plotmath`. Think of it as LateX-lite. Much of ggplot's syntax seemed to be an effort to workaround people's difficulty with understanding of symbols, names and expressions in base R, so you don't see it as much in ggplot programming. Try: `plot(1, main=expression(X %~% Sigma~{bar(x)[i] %*% hat(y)[i]}))` – IRTFM Feb 23 '19 at 18:55
  • The reason for thinking of plotmath was its capacity for an invisible space function called `phantom`. The spaces in your character vectors could ahve been replaces wtih an "adjustable" entry: `paste(rep(" ", 20), collapse="")` . If you want to have a numeric handle on the padding, just adjust the 20. – IRTFM Feb 23 '19 at 19:13
  • Thanks, I adjusted the gist link to use this approach. Though the appearance of the position of the y-axis is still dependent on the size the plot is rendered, the string lengths, ... I'd like to be able to keep y-axis centered and not have to guesstimate an appropriate number of " " for each plot (while also not having to do anything too complicated). I'd initially tried using `str_pad` with `side = "right"` to try and right pad everything to a fixed length, but `ggplot` hadn't seem to treat spaces as equal in size -- I didn't know if it might be possible to adjust something here...? – Bryan Shalloway Feb 24 '19 at 00:00
  • 1
    The "hpos" argument is relative to the plot size, whereas the dimensions of a space is fixed by the pointsize. If you want to pursue this further I would suggest looking at the functions in the "grid"-package (upon which the ggplot paradigm rests.) – IRTFM Feb 24 '19 at 00:13
  • Thanks, searched for fonts with fixed point size and added **UPDATE:** above. – Bryan Shalloway Feb 24 '19 at 22:50