简体   繁体   中英

Pasting value into R plot title

I would like to paste the value of the mean in a plot title using the title() function.

eg title("My plot \\n mean = mean(x)")

where x is a numeric vector of observations.

I know how to do this using plot(main = " ... ", ). Simply use paste() or substitute(expression()); however, this doesn't seem work with the title() function.

Any ideas?

See this example where title works correctly:

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

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

plot(x)
title(main=txt)

在此处输入图片说明

Answer by @MarcoSandri is correct. The correct way of using the example codes you have written are,

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

and

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM