【问题标题】:Rotate text in outer margin of device region旋转设备区域外边缘的文本
【发布时间】:2018-06-26 17:14:34
【问题描述】:

我想在平行于轴但朝向绘图中心的多个绘图的右外边缘添加文本(下图中“红色”和“蓝色”两个词的方向:

par(mfcol=2:1)

curve(sin,-2*pi,2*pi,col=2)
limits <- par("usr")
text(limits[2]+.25, mean(limits[3:4]),
     "red", srt=270, xpd=T)
curve(sin,-2*pi,2*pi,col=4)
text(limits[2]+.25, mean(limits[3:4]),
     "blue", srt=270, xpd=T)
mtext("Color of line",side=4,outer=T)

如果mtext 函数使用srt 参数而不是las(这显然是S plus 的情况),这将是微不足道的,并且上述使用usr 的解决方法将是不必要的。但我希望能够以同样的方式在外边距(上面的“线条颜色”)中定位文本,我似乎甚至无法手动处理文本(使用xpd=T 仍然将文本限制为最近的图形区域而不是设备区域)。

有没有一种方法不需要像the answer of @mrflick here 那样使用layout?这似乎应该是微不足道的,但我不明白它是如何做到的。

【问题讨论】:

    标签: r plot


    【解决方案1】:

    要查找设备中心的 y 坐标,您可以使用grconvertY 将“标准化设备坐标”("ndc";范围从 0 到 1)转换为用户坐标。

    这里的 x 值只是用适当的因子调整(例如limits[2] * 1.2)。

    windows()
    par(mfrow = c(2, 1), oma = c(0, 0, 0, 2))
    
    curve(sin, -2*pi, 2*pi, col = 2)
    limits <- par("usr")
    text(limits[2] + 0.25, mean(limits[3:4]),
         "red", srt = 270, xpd = TRUE)
    curve(sin, -2*pi, 2*pi, col = 4)
    text(limits[2] + 0.25, mean(limits[3:4]),
         "blue", srt = 270, xpd = TRUE)
    
    text(x = limits[2] * 1.2, y = grconvertY(0.5, from = "ndc"),
         labels = "color of line", xpd = NA, srt = 270)
    


    如果您想根据用户坐标 ("usr") 和绘图边距 ("mai") 计算 y 位置,请参阅以前的修订版。

    【讨论】:

    • @Richard Border 忘记了 "ndc" - 请查看更新后的答案。
    • @Richard Border 如果我的回答没有提供预期的结果,请告诉我。干杯
    猜你喜欢
    • 2015-04-25
    • 2011-02-26
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    相关资源
    最近更新 更多