R inclined stickers x axis x

how can you rotate the x axis labels 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) 

Which variable includes the question mark? 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() 
+7
r layout axis-labels boxplot
source share
2 answers

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? x in text is the coordinate vector where to place labels. If you look at ?boxplot , you will find that the argument at is "a numeric vector indicating where rectangles should be drawn" [...]; the default is 1: n, where n is the number of boxes. "Since we did not specify the at argument in the boxplot call, the default position is 1: n. The number of boxes is, of course, the number of levels of your explanatory variable that @Josh O'Brien used in his answer. To show you an alternative, instead I used my own label vector (which, of course, should be the same length as the number of factor levels). seq_along generates a regular sequence from 1 to length argument that matches the line items " default is 1: n " at .

Note: your data seems to be in a "wide" format. In many cases in R, it is more convenient to have data in a "long" format. In the plot function, you need to specify only the variable x (for example, location) and the variable y (for example, soil tempo) instead of specifying data for each level x. enter image description here

+10
source share

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

+2
source share

All Articles