使用带有动态下标的希腊字母

Mik*_*ike 2 plot expression r

我需要在 rplot 主标题中使用带有动态下标的希腊字母。如果我使用表达式和粘贴,就像我在下面代码中的“main”语句中所做的那样,那么下标的值不会显示为数字,而是显示为字母。我只能使用 paste 来获取下标的值,请参阅下面代码中的“sub”语句,但在这种情况下,不会出现希腊字母!!

任何的想法..

谢谢

#  Generating Random Variables

#  Exponential(1)

inv.cdf=function(n, lambda){
  u=runif(n,0,1)
  x=-log(1-u)/lambda
}


nu=sample(1:15,6)
y=matrix(rep(NA),10000,length(nu))
for(j in 1:length(nu)){
for (i in 1:10000){
  y[i,j]=2*sum(inv.cdf(nu[j], 1))
}
}

par(mfrow = c(3, 2))
for(j in 1:6){
hist(y[,j], freq=F, main=expression(paste('Histogram of Generated ',chi[2*nu[j]], ' Data')), sub= paste('Degrees of Freedom = ', nu[j]), xlab=expression(paste('Generated ' ,chi^2 ,' Data')))
curve(dchisq(x, df=2*nu[j]),add=T, col=2, lwd=2)
}
Run Code Online (Sandbox Code Playgroud)

nog*_*pes 5

我认为您正在寻找substitute允许您j用您喜欢的任何值替换表达式中的某些值(在您的情况下)。所以,如果你使用这个:

main=substitute(
        expression(paste('Histogram of Generated ',chi[2*nu[j]], ' Data')),
        list(j=j)
      )
Run Code Online (Sandbox Code Playgroud)

在您的hist功能中,它会起作用。