Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axis Labels in R: p(Y=y | theta = something)

Tags:

r

axis-labels

I've searched all over and can't find the code for having a conditional sign as in

p(a|b)

The code (generic R code, not ggplot) I'm using is

ylab = bquote(Pr( Y == y |  theta == .(mytheta) , n == .(n)))

What this gives me for a label is

Pr(|(Y=y,theta=0.2), n=10)

Where the theta is a proper Greek symbol, mytheta is 0.2, and n is 10. So all that's not working is the conditional sign. It looks to me like R is taking the | for an or...

I haven't tried ggplot yet, but would like to get this working in plain R first.

Thanks for your help.

like image 319
Eric Avatar asked Sep 02 '25 17:09

Eric


2 Answers

I always just use expression. I haven't used bquote before.

edited

Sorry, I did a bunch of these and obviously exported the wrong one. Use paste not paste0.

plot(rnorm(100), rnorm(100), ylab= expression(paste("P(Y| ", theta," )")))

enter image description here

like image 103
Alex W Avatar answered Sep 04 '25 06:09

Alex W


I thought it was pretty interesting to see that 'pipe' (vs. 'OR') get parsed into Polish notation. The conditional-bar can be accessed with the Symbol font using the methods described in ?plotmath and ?points

plot(1,1, main=bquote(Pr( Y == y ~ symbol("\275") ~ theta == .(mytheta) , n == .(n))))

(I did try making a SPECIAL user-defined function using %|% as the missing conditional symbol, but failed.)

To your comment-question asking for an illustration (actually two versions of how to use substitute in an equivalent manner:

mytheta = 0.2
plot(rnorm(100), rnorm(100), ylab= substitute(P(Y~"|"~ mytheta ), list(mytheta=mytheta)) )
plot(rnorm(100), rnorm(100), ylab= substitute(P(Y~"|"~ theta == mytheta ), 
                                                    list(mytheta=mytheta))  )
 # Second version prints greek-theta == value
like image 24
IRTFM Avatar answered Sep 04 '25 06:09

IRTFM