This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to include complete labels names in R boxplot

Hi everyone!

I am doing an R BoxPlot of OTU abundance trough different samples, but the labels of the x axes are incomplete: For example, one sample name is T1P1_T2_C-1, but in the plot, the labels show only from the P letter.

The code I am using is:

boxplot(OTUs[, 2:13], main="Boxplot OTU's abundance", cex.names=0.2, las=2)

The labels in the x axis are shown in vertical.

Thank you for your help!

r boxplot label

2 answers

Hey,

You likely just have to adjust the margins of your plot. I can see that you have tried to reduce cex, but that is not strictly necessary. Take a look at this reproducible example:

create random data with long labels

x <- matrix(rexp(200, rate=.1), ncol=20)
colnames(x) <- paste0("Very Very Very Very Long Label ", 1:ncol(x))

standard boxplot - labels do not fit

boxplot(x, las=2)

a

adjust the margins with par() to make labels fit

par(mar=c(15,2,1,1))
boxplot(x, las=2)

b

mar accepts 4 digits for the bottom, left, top, and right margin, respectively.

Kevin

Using ggplot2 spacing for labels is adjusted nicely by default, see example:

library(ggplot2)
library(tidyr)

# reproducible example data (borrowed from KevinBlighe's answer)
set.seed(1); x <- matrix(rexp(200, rate = 0.1), ncol = 20)
colnames(x) <- paste0("Very Very Very Very Long Label", seq(ncol(x)))
df1 <-data.frame(x)

# convert wide to long
plotDat <- gather(df1, "x", "y")

# then plot, and rotate labels 90 degrees.
ggplot(plotDat, aes(x, y)) +
  geom_boxplot() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

enter image description here

# or just rotate the plot
ggplot(plotDat, aes(x, y)) +
  geom_boxplot() +
  coord_flip()

enter image description here

Log in to answer this question.