r箱形图倾斜的标签x轴 [英] r boxplot tilted labels x axis

查看:305
本文介绍了r箱形图倾斜的标签x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何旋转r中的箱形图的x轴标签?我知道要使用哪个代码,但无法应用:

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:

使用此代码:

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()

推荐答案

原始text表达式之后的替代方案:

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)

为什么将x = seq_along(labels)用于标签位置? text中的x是放置标签的坐标向量.如果查看?boxplot,您会发现at参数是一个数字向量,给出了应绘制箱线图的位置;默认为1:n,其中n是箱数".因为我们没有在boxplot调用中指定at参数,所以将使用默认的"1:n位置".盒子的数量当然是您的解释变量的级别数,@ Josh O'Brien在回答中使用了该变量.为了给您显示一个替代方案,我改用了您自定义的标签向量(当然,它的长度必须与因子水平的数量相同). seq_along生成从参数的1到length的规则序列,对应于默认为1:n" at位置.

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.

一个旁注:您的数据似乎采用宽"格式.在R中的许多实例中,以长"格式存储数据更为方便.在plot函数中,您只需要指定x变量(例如位置)和y变量(例如土壤温度),而不是为每个x级别指定数据.

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.

这篇关于r箱形图倾斜的标签x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆