Align text inside a plot

匆匆过客 提交于 2019-11-28 06:50:55

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)

for left justified and...

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

for right justified.

Try using legend() instead of text()

legend(3.5, 150, legend = c(paste("Mean =", round(MyMean, 1)),
                            paste("Median =",round(MyMedian, 1)),
                            paste("Std.Dev =", round(MySd, 1))), 
                  bty = "n")

You'll have to play around with the position adjustment. You might consider not using xy coordinates at all, but replacing those two arguments with "topleft"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!