Superscript R squared for legend

拥有回忆 提交于 2019-12-09 06:24:33

问题


I want to write a R-squared term for my legend but I do not know how. Could someone help me please? My legend syntax is:

legend(2,10, c("BW (MPE=3%, R-squared=0.77)", 
       "MY (MPE=5%, R-squared=0.80)", pch=c(2,3))

I would liek to express R-squared as R2 as we normally have in the text.


回答1:


It will work if you combine bquote and as.expression:

plot(1:10)
legend(2, 10, c(as.expression(bquote("BW (MPE = 3%," ~ R^2 ~ "= 0.77)")),
                as.expression(bquote("MY (MPE = 5%," ~ R^2 ~ "= 0.80)"))), 
       pch=c(2,3))




回答2:


This is less complex than using c( as.expression ( bquote... multiple times:

plot(1:10)
legend(2, 10, expression("BW (MPE = 3%," ~ R^2 ~ "= 0.77)",
                 "MY (MPE = 5%," ~ R^2 ~ "= 0.80)"), 
        pch=c(2,3))

It is useful to understand that the expression function is really a way to make lists of expressions and that commas are then reserved as separators for that process. This means you cannot have a "naked" comma in something you want to be inside one of the distinct elements. The commas immediately after the %-signs are protected from parsing by the quotes. This could fully plotmath()-ified with:

plot(1:10)
legend(2, 10, expression(BW * list(MPE == 3*'%',
                                   R^2 == 0.77),
                         MY * list( MPE == 5*'%',
                                   R^2 == 0.80)
                         ), 
       pch=c(2,3))

That way the only character needing special attention is the '%'-sign which plotmath() uses to delimit its specials.



来源:https://stackoverflow.com/questions/20453408/superscript-r-squared-for-legend

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