Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm making a plot in ggplot where the x-axis labels contain the names of different proteins, and I'm having trouble because some of these names are too long and the labels becomes so big that it is hard to see the plot.

Instead of "printing" a bigger graph, there is any way to reduce the number of characters in the x-axis labels?

Here's an example that shows my problem:

library(ggplot2)
dat <- mtcars
# Make the x-axis labels very long for this example
dat$car <- paste0(rownames(mtcars),rownames(mtcars),rownames(mtcars),rownames(mtcars))

ggplot(dat, aes (x=car,y=hp)) +
    geom_bar(stat ="identity", fill="#009E73",colour="black") +
    theme_bw() +
    theme(axis.text.x = element_text(angle = 90, hjust = 1))

enter image description here

I would like to convert the labels from something like this:

Thisisaveryveryveryloooooongprotein

To this

Thisisavery[...]   

So that my plot can be consistently visible

like image 328
user2245731 Avatar asked Jul 25 '13 12:07

user2245731


People also ask

How do I change the size of the X-axis labels in ggplot2?

To increase the X-axis labels font size using ggplot2, we can use axis. text. x argument of theme function where we can define the text size for axis element. This might be required when we want viewers to critically examine the X-axis labels and especially in situations when we change the scale for X-axis.

How do I reduce the size of X-axis labels in R?

Go to the menu in RStudio and click on Tools and then Global Options. Select the Appearance tab on the left. Again buried in the middle of things is the font size. Change this to 14 or 16 to start with and see what it looks like.

How do I change the X-axis text in R?

Key ggplot2 R functionsp + xlab(“New X axis label”): Change the X axis label. p + ylab(“New Y axis label”): Change the Y axis label. p + labs(x = “New X axis label”, y = “New Y axis label”): Change both x and y axis labels.

How do I make text smaller in ggplot2?

How can I change the default font size in ggplot2? Set base_size in the theme you're using, which is theme_gray() by default. The base font size is 11 pts by default. You can change it with the base_size argument in the theme you're using.


2 Answers

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.

like image 145
Peyton Avatar answered Sep 29 '22 21:09

Peyton


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

like image 37
divibisan Avatar answered Sep 29 '22 19:09

divibisan