10

I have some y axis labels I want to have all the same spacing. The labels are composed of 2 variables and I'd like to put the correct spacing in between to the left variable is left aligned and the right variable is right aligned. I assumed this to be a trivial task and in fact doing so in a data.frame is easy using string padding functions from stringi package. However the plot does not have the desired alignment.

library(stringi)
library(tidyverse)

paster <- function(x, y, fill = ' '){
    nx <- max(nchar(x))
    ny <- max(nchar(y))
    paste0(
        stringi::stri_pad_right(x, nx, fill),
        stringi::stri_pad_left(y, ny, fill)
    )
}

plot_dat <- mtcars %>%
    group_by(gear) %>%
    summarize(
        n = n(),
        drat = mean(drat)
    ) %>%
    mutate(
        gear = case_when(
            gear == 3 ~ 'three and free', 
            gear == 4 ~ 'four or more',  
            TRUE ~ 'five'
        ),
        label = paster(gear, paste0(' (', n, ')'))
    )

plot_dat

## # A tibble: 3 x 4
##             gear     n     drat               label
##            <chr> <int>    <dbl>               <chr>
## 1 three and free    15 3.132667 three and free (15)
## 2   four or more    12 4.043333 four or more   (12)
## 3           five     5 3.916000 five            (5)

plot_dat %>%
    ggplot(aes(x = drat, y = label)) +
        geom_point() 

Gives:

enter image description here

What I want is:

enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • Possible duplicate of [Left-align tick mark labels in R ggplot](https://stackoverflow.com/questions/30510653/left-align-tick-mark-labels-in-r-ggplot) – neilfws Aug 22 '17 at 13:08
  • Yes I realised all of that on more careful reading of the question :) – neilfws Aug 22 '17 at 13:33
  • Not what you want, but maybe use `n` as main y-axis and text as [secondary y-axis](http://ggplot2.tidyverse.org/reference/sec_axis.html)? Not big fan of mono fonts on plots. – zx8754 Aug 22 '17 at 13:41
  • 1
    @zx8754 That was my initial thought as well but Hadley has only allowed the `scale_y_continuous` to have the 2nd axis not `scale_y_discrete` as seen here: http://ggplot2.tidyverse.org/reference/scale_discrete.html – Tyler Rinker Aug 22 '17 at 14:30

1 Answers1

11

Your text strings are nicely spaced based on a monospace font (which is what R console uses).

Setting the axis label's font family to a monospace font will give the correct alignment:

ggplot(mtcars,
       aes(x = drat, y = label)) +
  geom_point() +
  theme(axis.text.y = element_text(family = "mono"))

plot with properly aligned y axis labels

(Not the prettiest look, I know... But you get the basic idea. I haven't worked much with fonts in R & this is the only monospace font I can think of right now.)

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • 6
    You can use the `extrafont` package to make other faces available for plotting. There are some nice monospace ones out there. – Brian Aug 22 '17 at 05:05