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

杀马特。学长 韩版系。学妹 提交于 2019-12-02 00:45:32

问题


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.


回答1:


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



回答2:


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," )")))



来源:https://stackoverflow.com/questions/35232798/axis-labels-in-r-py-y-theta-something

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