【问题标题】:Avoid overlapping axis labels in R避免在 R 中重叠轴标签
【发布时间】:2011-10-10 08:12:55
【问题描述】:

我想在图表中为标签绘制更大字体的数据。

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,但不起作用(顺便问一下,我怎样才能知道哪些图形参数可以直接在plot命令中使用,哪些必须用par()方法设置?)。

如何避免标签重叠?

感谢您的帮助。

斯文

【问题讨论】:

标签: r plot overlap


【解决方案1】:

您可以将 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).

【讨论】:

    【解决方案2】:

    使用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()'来设置:

        • ‘"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.
    

    【讨论】:

    • 当我使用 mgp 时,y-label 和 x-label 会被移动。是否也可以只移动 y 标签?有没有教那些基本知识的好教程?我总是迷失在帮助中,... :-(
    • 如果您不喜欢默认位置,通常的方法是ylab="" 并使用axis(... , line=<integer>)。 ?轴
    • @DWin:我会试试你的建议。现在我首先将“par (mgp)”设置为一个轴的标题,然后再次设置“par(mgp)”,并将“title()”设置为下一个轴。
    【解决方案3】:

    快速而肮脏的方法是使用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,因为它们会收到... 参数。还有一些对未记录函数(据我所知)的调用,例如localWindowlocalBox,但我不知道它们会发生什么。我猜他们只是被忽略了。

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 2018-06-07
      • 2013-11-03
      • 2019-08-05
      • 1970-01-01
      • 2019-12-07
      相关资源
      最近更新 更多