将值粘贴到R图标题中

时间:2017-06-30 00:22:05

标签: r

我想使用title()函数在图表标题中粘贴均值的值。

e.g。标题("我的情节\ n均值=平均值(x)")

其中x是观察的数字向量。

我知道如何使用plot(main =" ...",)。只需使用paste()或替换(expression());但是,这似乎不适用于title()函数。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

请参阅title正常工作的示例:

set.seed(1)
x <- rnorm(30)

txt <- paste("Mean =", round(mean(x),3) )   

plot(x)
title(main=txt)

enter image description here

答案 1 :(得分:1)

@MarcoSandri的回答是正确的。使用您编写的示例代码的正确方法是

title(main= paste("My plot \n mean =", mean(x)))

avg <- mean(x)
title(paste("My Plot \n Mean = ", avg))