为 R 中的箱线图以给定度数旋转 x 轴标签
Rotate x-axis labels at a given degree for boxplot in R
我使用以下代码生成箱线图:
boxplot(top10threads$affect ~ top10threads$ThreadID[], data = top10threads, xlab = "10 biggest Threads", ylab = "Affect", col=(c("gold","darkgreen")), srt=45)
但是你可能注意到 x 轴上的一些标签丢失了,所以我想将它们旋转 45 度。我添加了 srt=45
,但它不起作用。
通过设置las=2
可以垂直旋转它们,但这不是我需要的。
我该怎么做?谢谢
部分测试数据:
mydata=lapply(1:5,function(i) rnorm(100,mean=i))
names(mydata)=c("first","second","third","fourth","fifth")
首先,绘制没有 x 轴的箱线图:
boxplot(mydata,xaxt="n",xlab="")
然后,我们创建一个函数来添加文本 x 轴标签:
x_axis_labels=function(labels,every_nth=1,...) {
axis(side=1,at=seq_along(labels),labels=F)
text(x=(seq_along(labels))[seq_len(every_nth)==1],
y=par("usr")[3]-0.075*(par("usr")[4]-par("usr")[3]),
labels=labels[seq_len(every_nth)==1],xpd=TRUE,...)
}
# axis() draws the axis with ticks at positions specified by at. Again, we don't plot the labels yet.
# text() plots the labels at positions given by x and y.
# We estimate the y-positions from the values of the y-axis (using par("usr")),
# and specify xpd=TRUE to indicate that we don't want to crop plotting to within the plot area
# Note that we select the [seq_len(every_nth)==1] elements of both the x positions and the labels,
# so we can easily skip labels if there would be too many to cram in otherwise.
# Finally, we leave a ... in the function so we can pass additional arguments to text()
最后,我们调用新函数来绘制轴刻度标签:
x_axis_labels(labels=names(mydata),every_nth=1,adj=1,srt=45)
这里我们利用函数中的...传递rotation/justification参数:adj=1表示将文字标签右对齐,srt=45表示旋转45度.
首先,将boxplot()
的输出存储为一个对象。它包含组的名称。您可以使用 $names
来获取它们。然后使用 text()
添加轴的标签。参数 srt
适用于 text()
.
tmp <- boxplot(y ~ x, data = df, col = c("gold","darkgreen"), xaxt = "n")
tick <- seq_along(tmp$names)
axis(1, at = tick, labels = F)
text(tick, par("usr")[3] - 0.3, tmp$names, srt = 45, xpd = T)
数据
df <- data.frame(x = sample(100:110, 100, T), y = rnorm(100))
我使用以下代码生成箱线图:
boxplot(top10threads$affect ~ top10threads$ThreadID[], data = top10threads, xlab = "10 biggest Threads", ylab = "Affect", col=(c("gold","darkgreen")), srt=45)
但是你可能注意到 x 轴上的一些标签丢失了,所以我想将它们旋转 45 度。我添加了 srt=45
,但它不起作用。
通过设置las=2
可以垂直旋转它们,但这不是我需要的。
我该怎么做?谢谢
部分测试数据:
mydata=lapply(1:5,function(i) rnorm(100,mean=i))
names(mydata)=c("first","second","third","fourth","fifth")
首先,绘制没有 x 轴的箱线图:
boxplot(mydata,xaxt="n",xlab="")
然后,我们创建一个函数来添加文本 x 轴标签:
x_axis_labels=function(labels,every_nth=1,...) {
axis(side=1,at=seq_along(labels),labels=F)
text(x=(seq_along(labels))[seq_len(every_nth)==1],
y=par("usr")[3]-0.075*(par("usr")[4]-par("usr")[3]),
labels=labels[seq_len(every_nth)==1],xpd=TRUE,...)
}
# axis() draws the axis with ticks at positions specified by at. Again, we don't plot the labels yet.
# text() plots the labels at positions given by x and y.
# We estimate the y-positions from the values of the y-axis (using par("usr")),
# and specify xpd=TRUE to indicate that we don't want to crop plotting to within the plot area
# Note that we select the [seq_len(every_nth)==1] elements of both the x positions and the labels,
# so we can easily skip labels if there would be too many to cram in otherwise.
# Finally, we leave a ... in the function so we can pass additional arguments to text()
最后,我们调用新函数来绘制轴刻度标签:
x_axis_labels(labels=names(mydata),every_nth=1,adj=1,srt=45)
这里我们利用函数中的...传递rotation/justification参数:adj=1表示将文字标签右对齐,srt=45表示旋转45度.
首先,将boxplot()
的输出存储为一个对象。它包含组的名称。您可以使用 $names
来获取它们。然后使用 text()
添加轴的标签。参数 srt
适用于 text()
.
tmp <- boxplot(y ~ x, data = df, col = c("gold","darkgreen"), xaxt = "n")
tick <- seq_along(tmp$names)
axis(1, at = tick, labels = F)
text(tick, par("usr")[3] - 0.3, tmp$names, srt = 45, xpd = T)
数据
df <- data.frame(x = sample(100:110, 100, T), y = rnorm(100))