8

how can you rotate the labels of the x axis for boxplot in r? I know which code to use but I can't apply it:

text(**????**, par("usr")[3] - 0.25, srt = 45, adj = 1, labels = labels, xpd = TRUE)

What variable goes where I have the question marks? I created this boxplot:

enter image description here

using this code:

soil=read.csv("soil_temp_boxplot.csv", header=TRUE, sep=";")    
tiff("soil_boxplot.tiff")
par(mar=c(5.5,3.5,0.5,0.5))
labels<-paste(c("RB-GL830-[16]-10","RB-GL830-[16]-30", "SB-GL834-[11]-10","SB-GL834-[11]-30", "RB-GL843-[17]-10","RB-GL843-[17]-30","SB-GL864-[12]-10","SB-GL864-[12]-30","SB-GL989-[10]-30", "RB-F844-[18]-10", "RB-F844-[18]-30", "SBB-F-864-[14]-10","SB-F991-[13]-10", "SB-F991-[13]-30"))
boxplot(soil$rb.gl.10.830.16, soil$rb.gl.30.830.16, soil$sb.gl.10.834.11, soil$sb.gl.30.834.11, soil$rb.gl.10.843.17, soil$rb.gl.30.843.17, soil$sb.gl.10.864.12, soil$sb.gl.30.864.12, soil$sb.gl.30.989.10, soil$rb.f.10.844.18, soil$rb.f.30.844.18, soil$sbb.f.10.864.14, soil$sb.f.10.991.13, soil$sb.f.30.991.13, yaxt="n", col=c("darkolivegreen1","darkolivegreen4","darkolivegreen1","darkolivegreen4","darkolivegreen1","darkolivegreen4","darkolivegreen1","darkolivegreen4","darkolivegreen1","burlywood2","burlywood4","burlywood2","burlywood2", "burlywood4"))
axis(1, labels = TRUE)
axis(2, c(0, 8, c(1, 2, 3, 4, 5,6,7)), las=1)
text(labels, par("usr")[3] - 0.25, srt = 45, adj = 1, labels = labels, xpd = TRUE)
mtext(2, text="Soil Temperature [°C]", line=2.2)
mtext(1, text="Location", line=4.5)
dev.off()
samjam
  • 199
  • 2
  • 4
  • 11

2 Answers2

14

An alternative following your original text expression:

par(mar=c(6, 4.1, 4.1, 2.1))

labels <- paste(c("RB-GL830-[16]-10", 
                  "RB-GL830-[16]-30",
                  "SB-GL834-[11]-10",
                  "SB-GL834-[11]-30",
                  "RB-GL843-[17]-10",
                  "RB-GL843-[17]-30"))

boxplot(count ~ spray, data = InsectSprays,
        col = "lightgray", xaxt = "n",  xlab = "")

# x axis with ticks but without labels
axis(1, labels = FALSE)

# Plot x labs at default x position
text(x =  seq_along(labels), y = par("usr")[3] - 1, srt = 45, adj = 1,
     labels = labels, xpd = TRUE)

Why use x = seq_along(labels) for label positions? The x in text is a vector of coordinates where to put the labels. If you look at ?boxplot, you find that the at argument is a "numeric vector giving the locations where the boxplots should be drawn [...]; defaults to 1:n where n is the number of boxes." Because we haven't specified the at argument in the boxplot call, the default "1:n positions" will be used. The number of boxes is of course the number of levels of your explanatory variable, which @Josh O'Brien used in his answer. To show you an alternative, I used your customized label vector instead (which of course must have the same length as the number of factor levels). seq_along generates a regular sequence from 1 to length of the argument, which corresponds to the "defaults to 1:n" at positions.

A side-note: your data seem to be in a 'wide' format. In many instances in R, it is more convenient to have the data in a 'long' format. In the plot function, you then only need to specify your x variable (e.g. location) and y variable (e.g. soil temp), instead of specifying data for every single level of x. enter image description here

Henrik
  • 65,555
  • 14
  • 143
  • 159
  • Great to hear that @samjam! Please see my little side-note regarding the format of your data in my updated answer. – Henrik Sep 08 '13 at 09:39
  • 2
    this is good! But the `y = par("usr")[3] - 1` doesn't work well in different scales. It is much better to use `y = par("usr")[3] - (par("usr")[4] - par("usr")[3])/30`, where the `30` is the proportion of the height to be used as space between labels and axis. Works the same way in any scale :) – Tomas Feb 07 '19 at 08:25
2

Look at the staxlab function in the plotrix package, it makes this (and an alternative) fairly straight forward.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • @Henrik, I believe that my answer addresses the "main problem" of the OP, but I do not agree that the main problem is the same as the specific question of how to specify the x coordinates of the labels (which I admit my answer does not address). The beauty of a forum like this is that multiple answers can address the main question as well as implied and other related questions that may be of interest to the OP and future searchers. I also do not look at my watch and say "yes" when asked "do you know what time it is?". – Greg Snow Sep 09 '13 at 14:30