以给定角度旋转 R 中箱线图的 x 轴标签

ahb*_*bon 2 r boxplot

我使用以下代码生成箱线图:

boxplot(top10threads$affect ~ top10threads$ThreadID[], data = top10threads, xlab = "10 biggest Threads", ylab = "Affect", col=(c("gold","darkgreen")), srt=45)
Run Code Online (Sandbox Code Playgroud)

但您可能会注意到 x 轴上的一些标签丢失了,所以我想将它们旋转 45 度。我添加了srt=45,但是不起作用。

通过设置las=2可以垂直旋转它们,但这并不完全是我需要的。

我怎么能这么做呢?谢谢。

在此输入图像描述

Dar*_*sai 6

首先,将 的输出存储boxplot()为对象。它包含组的名称。您可以用来$names获取它们。然后使用text()在轴上添加标签。该论证srt有效text()

bp <- boxplot(y ~ x, data = df, col = c("gold", "darkgreen"), xaxt = "n")
tick <- seq_along(bp$names)
axis(1, at = tick, labels = FALSE)
text(tick, par("usr")[3] - 0.3, bp$names, srt = 45, xpd = TRUE)
Run Code Online (Sandbox Code Playgroud)

数据

df <- data.frame(x = sample(100:110, 100, TRUE), y = rnorm(100))
Run Code Online (Sandbox Code Playgroud)

  • @ahbon我的数据范围约为4,但你的数据范围约为40。因此在你的情况下减去的数字将为5~10。即 `par("usr")[3] - 5` ~ `par("usr")[3] - 10`。 (2认同)