Question: How to include complete labels names in R boxplot
1
gravatar for dllopezr
13 months ago by
dllopezr40
dllopezr40 wrote:

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!

label R boxplot • 4.6k views
ADD COMMENTlink modified 13 months ago by zx87548.4k • written 13 months ago by dllopezr40
2
gravatar for Kevin Blighe
13 months ago by
Kevin Blighe51k
Kevin Blighe51k wrote:

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 COMMENTlink written 13 months ago by Kevin Blighe51k
1
gravatar for zx8754
13 months ago by
zx87548.4k
London
zx87548.4k wrote:

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 COMMENTlink modified 13 months ago • written 13 months ago by zx87548.4k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1989 users visited in the last hour