对齐绘图内的文本 [英] Align text inside a plot

查看:19
本文介绍了对齐绘图内的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 R 新手,有一个问题.我正在尝试将一些文本放入 R 图中.下面是使用 UsingR 包中的亮度数据集的一些代码.

I am an R newbie and had a question. I am trying to place some text into an R plot. Here's some code using the brightness dataset in the UsingR package.

    library(UsingR)
    brightness

    MyMean <- mean(brightness)
    MyMedian <- median(brightness)
    MySd <- sd(brightness)

    hist(brightness, breaks=35, main="This is a Histogram", 
         xlab="Brightness", ylab="Frequency", xlim=c(0,15), ylim=c(0, 200))

    text(3.5, 150, paste("Mean =", round(MyMean, 1), "\n Median =", 
         round(MyMedian, 1), "\n Std.Dev =", round(MySd, 1)))

此代码产生:

这个输出的问题是文本没有左对齐.有谁知道如何使文本左对齐.

The issue with this output is that the text is not left left alligned. Does anyone know how to make the text left alligned.

谢谢.

推荐答案

虽然 Legend() 当然适用于图例,但对于所有文本都有一个通用的解决方案.诀窍是 pos 选项不仅设置文本相对于当前位置的位置,而且还设置对齐方式.上方和下方是居中对齐.将 pos 设置为 2 使文本右对齐.当它设置在位置的右侧 (pos = 4) 时,它左对齐.

While legend() is of course appropriate for legends, there is a general solution for all text. The trick is that the pos option not only sets the position of the text relative to the current location but it also sets justification. Above and Below are center justified. Setting pos to 2 makes the text right justified. When it is set to the right of the position (pos = 4) then it is left justified.

用...替换您的文本代码

Replace your text code with...

text(1.5, 150, paste("Mean =", round(MyMean, 1), "\nMedian =", 
         round(MyMedian, 1), "\nStd.Dev =", round(MySd, 1)), pos = 4)

用于左对齐和...

text(5.0, 150, paste("Mean = ", round(MyMean, 1), "\nMedian = ", 
        round(MyMedian, 1), "\nStd.Dev = ", round(MySd, 1), sep = ''), pos = 2)

右对齐.

这篇关于对齐绘图内的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆