图中的多行 y 标签

问题描述 投票:0回答:3

有没有办法在 R 图中为 y 轴创建多线标签?

我尝试在换行符所在的位置添加

\n
,但标签的第一行被剪掉:

l <- 10
plot(0:l, (0:l), type='l',
     yaxt='n',
     xlab='Index',
     ylab='Cumulative sum\nof the sorted weights')

Result of plot

这种情况在

tikzDevice
和 RStudio 内部都会发生。另外,我尝试了一些
par()
选项,但没有成功。怎样做才是正确的呢?

(过大的上边距也让我烦恼......)

r data-visualization
3个回答
10
投票

您需要使用

mar
mgp
设置边距:

l <- 10
op <- par(mar=c(5, 6, 4, 2) + 0.1)
plot(0:l, (0:l), type='l',
     yaxt='n',
     xlab='Index',
     ylab='Cumulative sum\nof the sorted weights')
par(op)

enter image description here


2
投票

就像 @smillig 建议的那样,您可以使用

par
来完成此操作,更改
mar
mgp
参数。

但是你必须先打电话

par
然后打电话给
plot


1
投票

允许完全控制轴标题的替代方法是使用

mtext()

l <- 10
plot(0:l, (0:l),
  type = "l",
  yaxt = "n",
  xlab = "Index",
  ylab = ""
)
mtext(side = 2, line = 3, "Cumulative sum")
mtext(side = 2, line = 2, "of the sorted weights")

创建于 2023-10-06,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.