How to include complete labels names in R boxplot
2
3
Entering edit mode
5.5 years ago
dllopezr ▴ 120

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 • 24k views
ADD COMMENT
3
Entering edit mode
5.5 years ago

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

ADD COMMENT
2
Entering edit mode
5.5 years ago
zx8754 11k

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

ADD COMMENT

Login before adding your answer.

Traffic: 1957 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6