【问题标题】:Adjust plot title (main) position调整情节标题(主要)位置
【发布时间】:2013-12-03 15:41:01
【问题描述】:

我一直无法找到一种方法来使用 par 调整 R 中的情节和主标题之间的(垂直)距离。在这个例子中:

plot(1, 1, main = "Title")

我可以使用以下方法调整轴标题的位置:

par(mgp = c(2.5, 1, 0))

但我看不到类似调整主标题的方法。我知道使用titlemtext 可以进行更多的手动控制,但我认为也有一种方法可以使用par 设置标题距离,这对我的目的来说会更优雅。

【问题讨论】:

  • 我注意到mar的第三个条目间接影响了标题的位置。例如 par(mar=c(x,y,0,z)) 应该产生一个真正接近情节的标题,而 par(mar=c(x,y,2,z)) 应该产生一个更接近情节的标题遥远。我发现没有 mtext 和 title 没有更好的方法。

标签: r plot


【解决方案1】:

我们可以使用带有负值linetitle() 函数来降低标题。

看这个例子:

plot(1, 1)
title("Title", line = -2)

【讨论】:

  • 那绝对有效 - 谢谢。我一直在寻找一种使用 par 的方法,它允许在一行中设置多个参数,然后无需手动调整即可获得所需的图。您知道是否无法使用 par 吗?
  • 对于字幕(通常在 X 轴下绘制)使用title(sub='Sub title notes', line=2)。您可能需要按par(mar=c(5+2, 4, 4, 2) + 0.1) 排列底部边距(例如增加2 行)。
【解决方案2】:

总结和直观地解释它是如何工作的。代码构造如下:

par(mar = c(3,2,2,1))
barplot(...all parameters...)
title("Title text", adj = 0.5, line = 0)

解释:

par(mar = c(low, left, top, right)) - margins of the graph area.

title("text" - title text
      adj  = from left (0) to right (1) with anything in between: 0.1, 0.2, etc...
      line = positive values move title text up, negative - down)

【讨论】:

    【解决方案3】:

    试试这个:

    par(adj = 0)
    plot(1, 1, main = "Title")
    

    或同等学历:

    plot(1, 1, main = "Title", adj = 0)
    

    adj = 0 生成左对齐文本、0.5 个(默认)居中文本和 1 个右对齐文本。 [0, 1] 中的任何值都是允许的。

    但是,问题是这也会改变x轴和y轴标签的位置。

    【讨论】:

    • However, the issue is that this will also change the position of the label of the x-axis and y-axis. ... 这可以通过使用title(main = x, adj = ...)为标题调用单独的函数来解决
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 2020-11-11
    • 1970-01-01
    • 2012-10-08
    • 2015-07-19
    • 2012-11-23
    相关资源
    最近更新 更多