How can I shorten x-axis label text in ggplot?

Try the abbreviate function:

qplot(Species, Sepal.Length, data=iris, geom="boxplot") +
  scale_x_discrete(label=abbreviate)

example of label abbreviation

If the defaults won't do in your case, you can define your own function:

qplot(Species, Sepal.Length, data=iris, geom="boxplot") +
  scale_x_discrete(label=function(x) abbreviate(x, minlength=7))

You can also try rotating the labels.


Since abbreviate works by removing spaces and lower-case vowels from the string, it can lead to some strange abbreviations. For many cases, it would be better to truncate the labels instead.

You can do this by passing any string truncation function to the label= argument of scale_* function: some good ones are stringr::str_trunc and the base R strtrim

mtcars$name <- rownames(mtcars)

ggplot(mtcars, aes(name, mpg)) +
    geom_col() +
    scale_x_discrete(label = function(x) stringr::str_trunc(x, 12)) +
    theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

enter image description here