简体   繁体   English

在 R 中显示 plot 图例中的数字

[英]displaying numbers in a plot legend in R

I was wondering why numerical values of pop_mean = 10; pop_mean_sd = 5; within_sd = 3我想知道为什么pop_mean = 10; pop_mean_sd = 5; within_sd = 3 pop_mean = 10; pop_mean_sd = 5; within_sd = 3 pop_mean = 10; pop_mean_sd = 5; within_sd = 3 don't show in the legend of my plot below? pop_mean = 10; pop_mean_sd = 5; within_sd = 3不显示在我下面的 plot 的legend中? Is there a fix?有解决办法吗?

pop_mean = 10 ; pop_mean_sd = 5 ; within_sd = 3

plot(1)
legend("top",      c(expression(mu[i]*"~"*N(.(pop_mean)*","*.(pop_mean_sd))), 
                     expression(Math[ij]*"~"*N(mu[i]*","*.(within_sd)))),
                      bty = "n", cex = .8, 
                    inset = c(0, -.17), xpd = NA)

Here's an approach using bquote .这是一种使用bquote的方法。

From help(bquote) :help(bquote)

An analogue of the LISP backquote macro. LISP 反引号宏的类似物。 bquote quotes its argument except that terms wrapped in.() are evaluated in the specified where environment. bquote 引用它的参数,除了包含在.() 中的术语在指定的 where 环境中进行评估。

Therefore, anything within .() is evaluated, which should be what you're looking for.因此, .()中的任何内容都会被评估,这应该是您正在寻找的内容。 Using c() with bquote makes a list of calls, which doesn't work with legend .c()bquote一起使用会生成一个调用列表,这不适用于legend So you can use as.expression to convert to a usable expression.因此,您可以使用as.expression转换为可用的表达式。

plot(1)
legend("top",
       as.expression(c(bquote(mu[i]*"~"*N(.(pop_mean)*","*.(pop_mean_sd))),
                       bquote(Math[ij]*"~"*N(mu[i]*","*.(within_sd))))),
       bty = "n", cex = .8, 
       inset = c(0, 0), xpd = NA)

在此处输入图像描述

A similar approach could be with substitute :类似的方法可能是substitute

legend("top",
       legend = as.expression(c(substitute(mu[i]*"~"*N(pop_mean*","*pop_mean_sd),
                                                 list(pop_mean=pop_mean, pop_mean_sd = pop_mean_sd)),
                                      substitute(italic(Math)[ij]*"~"*N(mu[i]*","*within_sd),
                                                 list(within_sd=within_sd)))),
       bty = "n", cex = .8, 
       inset = c(0, 0), xpd = NA)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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