Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R plot(). Legends with multiple variable per entry

I am somewhat new to R. I have followed and implemented the results in the following two threads:

http://tolstoy.newcastle.edu.au/R/e17/help/12/03/7984.html

http://lukemiller.org/index.php/2012/10/adding-p-values-and-r-squared-values-to-a-plot-using-expression/

The 2nd link acknowledges the 1st solution, albeit in a more detailed fashion.

Here's what I have already:

sigma <- c(1,2,3)

rp <- vector('expression',3)

rp[1] <- substitute(expression(paste(sigma == Value1,"% ")), list(Value1 = sigma[1]))[2]

rp[2] <- substitute(expression(paste(sigma == Value1,"% ")), list(Value1 = sigma[2]))[2]

rp[3] <- substitute(expression(paste(sigma == Value1,"% ")), list(Value1 = sigma[3]))[2]

plot(0);legend(x="topright",legend=rp)

Now, I am not very savvy with expression(paste()). I would like to extend each entry with additional information such that I can include another variable. So for example,

mu <- c(4,5,6)

I want to now add mu to the first legend entry, to look like

(Greek) sigma=1%, (Greek) mu=4%

How do I do modify the expression(paste()) combo above to achieve this?

Also, when I plot the legend out, the text is very much 'right-justified'. How can I ensure that it is centred in the bounding box?

Thanks for your help.

Maziar

like image 876
please help Avatar asked Apr 27 '26 02:04

please help


1 Answers

Here is a not so elegant solution:

sigma <- c(1,2,3)
mu <- c(4,5,6)
rp <- vector('expression',3)
i=1
for (i in 1:length(sigma)){
  jnk=paste("paste(sigma == ", sigma[i],",', ',", "mu == ", mu[i],")")
  rp[i]=parse(text=jnk)  
}
plot(0);legend(x="bottomleft",legend=rp)

The text alignment issue is a different and pretty basic issue, you can find solutions for that elsewhere.

like image 77
Lucas Fortini Avatar answered Apr 29 '26 19:04

Lucas Fortini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!