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!
• 27,802 views
•
link
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)
adjust the margins with par() to make labels fit
par(mar=c(15,2,1,1))
boxplot(x, las=2)
mar accepts 4 digits for the bottom, left, top, and right margin, respectively.
Kevin
• 1 views
•
link
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))

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

• 1 views
•
link
Log in to answer this question.

