避免 R 中的轴标签重叠

发布于 2024-11-25 11:20:23 字数 355 浏览 0 评论 0原文

我想在图表中绘制数据,并使用较大的标签字体。

x = c(0:10)
y = sin(x) + 10

plot (
    x, y, type="o",
    xlab = "X values",
    ylab = "Y values",
    cex.axis = "2",
    cex.lab = "2",
    las = 1
)

不幸的是,y 轴上的数字与 y 轴的标签重叠。我尝试使用 mar,但这不起作用(顺便问一下,我如何找出哪些图形参数可以直接在绘图命令中使用以及哪些必须使用 par() 方法设置?)。

如何避免标签重叠?

感谢您的帮助。

斯文

I want to plot data in a graph with larger font-size for the lables.

x = c(0:10)
y = sin(x) + 10

plot (
    x, y, type="o",
    xlab = "X values",
    ylab = "Y values",
    cex.axis = "2",
    cex.lab = "2",
    las = 1
)

Unfortunately the numbers on the y-axis overlap the label for the y-axis. I tried to use mar, but that did not work (By the way, how can I find out which graphic parameters can be directly used in the plot command and which have to be set with the par()-method? ).

How can I avoid that labels overlap?

Thanks for your help.

Sven

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

錯遇了你 2024-12-02 11:20:23

使用 par(mar) 增加绘图边距,使用 par(mgp) 移动轴标签。

par(mar = c(6.5, 6.5, 0.5, 0.5), mgp = c(5, 1, 0))
#Then call plot as before

在帮助页面 ?par 中,它解释了哪些参数可以直接在 plot 中使用,哪些参数必须通过 par 调用。

有几个参数只能通过调用“par()”来设置:

 • '“询问”',

    •“无花果”、“鳍”、

    • '“高度”',

    •“mai”、“mar”、“mex”、“mfcol”、“mfrow”、“mfg”、

    • '“新的”',

    •“oma”、“omd”、“omi”、

    •“pin”、“plt”、“ps”、“pty”、

    • '“usr”',

    •“xlog”、“ylog”

 其余参数也可以设置为参数(通常通过
 '...') 到高级绘图函数,例如 'plot.default',
 'plot.window', '点', '线', 'abline', '轴', '标题',
 '文本'、'多行文本'、'线段'、'符号'、'箭头'、'多边形'、
 “矩形”、“盒子”、“轮廓”、“填充轮廓”和“图像”。这样的
 设置将在函数执行期间激活,
 仅有的。但是,请参阅“bg”和“cex”的注释,这可能是
 作为某些绘图函数的_参数_而不是作为
 图形参数。

Use par(mar) to increase the plot margins and par(mgp) to move the axis label.

par(mar = c(6.5, 6.5, 0.5, 0.5), mgp = c(5, 1, 0))
#Then call plot as before

In the help page ?par it explains which parameters can be used directly in plot and which must be called via par.

There are several parameters can only be set by a call to ‘par()’:

    • ‘"ask"’,

    • ‘"fig"’, ‘"fin"’,

    • ‘"lheight"’,

    • ‘"mai"’, ‘"mar"’, ‘"mex"’, ‘"mfcol"’, ‘"mfrow"’, ‘"mfg"’,

    • ‘"new"’,

    • ‘"oma"’, ‘"omd"’, ‘"omi"’,

    • ‘"pin"’, ‘"plt"’, ‘"ps"’, ‘"pty"’,

    • ‘"usr"’,

    • ‘"xlog"’, ‘"ylog"’

 The remaining parameters can also be set as arguments (often via
 ‘...’) to high-level plot functions such as ‘plot.default’,
 ‘plot.window’, ‘points’, ‘lines’, ‘abline’, ‘axis’, ‘title’,
 ‘text’, ‘mtext’, ‘segments’, ‘symbols’, ‘arrows’, ‘polygon’,
 ‘rect’, ‘box’, ‘contour’, ‘filled.contour’ and ‘image’.  Such
 settings will be active during the execution of the function,
 only.  However, see the comments on ‘bg’ and ‘cex’, which may be
 taken as _arguments_ to certain plot functions rather than as
 graphical parameters.
╭⌒浅淡时光〆 2024-12-02 11:20:23

快速而肮脏的方法是使用 par 并在 ylab 中添加换行符,尽管它在概念上很糟糕。

x = 0:10
y = sin(x) + 10

par(mar=c(5,7,4,2))
plot (
    x, y, type="o",
    xlab = "X values",
    ylab = "Y values\n",
    cex.axis = "2",
    cex.lab = "2",
    las = 1
)

关于可以直接在 plot 中设置哪些参数,请查看 ?plot.default?plot.xy 因为它们将收到 ... 参数。还有一些对未记录函数的调用(据我所知),例如 localWindow 和 localBox,但我不知道它们会发生什么。我猜他们只是被忽略了。

The quick and dirty way would be to use par and add a newline in ylab, even though it's conceptually terrible.

x = 0:10
y = sin(x) + 10

par(mar=c(5,7,4,2))
plot (
    x, y, type="o",
    xlab = "X values",
    ylab = "Y values\n",
    cex.axis = "2",
    cex.lab = "2",
    las = 1
)

Concerning which parameters you can set directly in plot have a look at ?plot.default and ?plot.xy as they will recieve the ... arugments. There's also a couple of calls to undocumented functions (as far as I can find) like localWindow and localBox but I don't know what happens to them. I'd guess they're just ignored.

笑梦风尘 2024-12-02 11:20:23

您可以将 mgp 参数放入 title() 函数中,以避免随后重置默认值。这样,参数仅作用于函数添加的标签。像这样:

plot (
x, y, type="o",
xlab = "",         #Don't include xlab in main plot
ylab = "Y values",
cex.axis = "2",
cex.lab = "2",
las = 1
)
title(xlab="X values"
 ,mgp=c(6,1,0))    #Set the distance of title from plot to 6 (default is 3).

You can put the mgp parameter into the title() function to avoid having to reset your defaults afterwards. That way the parameter only acts on the label(s) added by the function. like this:

plot (
x, y, type="o",
xlab = "",         #Don't include xlab in main plot
ylab = "Y values",
cex.axis = "2",
cex.lab = "2",
las = 1
)
title(xlab="X values"
 ,mgp=c(6,1,0))    #Set the distance of title from plot to 6 (default is 3).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文