我有以下任务:
我想创建一个方面图,其中方面标题具有数学表达式:接下来是一些代码:
rm(list=ls())
library(nlme)
library(ggplot2)
mupre<-10
sdpre<-25
n<-1000
xpre<-rnorm(n,mupre,sdpre)
############
### a function to define a variance
############
A<-matrix(c(rep(1,3),c(.2,.5,.8),c(.2,.5,.8)^2),ncol=3)
B<-c(.09,.2,.32)
A%*%solve(A,B)
X=solve(A,B)
dltf<-function(bta){c(1,bta,bta^2)%*%X}
###############
###each plot is a diffirent simulation with different beta and delta
###############
resdf<-NULL
for(i in seq(.2,1,length.out = 10)){
bta<-i
dlta<-dltf(bta)
Ec_xpost<- xpre*bta
sdc_xpost<- xpre*dlta
xpost<-rnorm(n,Ec_xpost,sdc_xpost)
xpost[xpost<0]<-0
xprepost<-data.frame(pre=xpre,post=xpost,beta=bta,delta=dlta)
resdf<-rbind(resdf,xprepost)
plt<-ggplot(xprepost,aes(x=xpre,y=xpost))+geom_point()+
ggtitle(substitute(paste(beta,"=",b1,", ", delta,"=",d2),list(b1=round(bta,2),d2=round(dlta,2))))+
geom_hline(yintercept = 0,col=2)+ geom_abline(intercept = 0, slope = 1,col=2)+
scale_x_continuous(limits = c(-5,100))+scale_y_continuous(limits = c(-5,100))+
labs(x="pre",y="post")
print(plt)
}
此代码块仅用于说明我希望每个图形的标题在下面的方面显示为标题,使用resdf中存储的信息。
resdf$lab<-factor(resdf$beta,labels=c(expression(beta[1]),expression(beta[2]),expression(beta[3]),expression(beta[4]),
expression(beta[5]),expression(beta[6]),expression(beta[7]),expression(beta[8]),
expression(beta[9]),expression(beta[10])))
ggplot(resdf,aes(x=pre,y=post))+geom_point()+
scale_x_continuous(limits = c(-5,100))+scale_y_continuous(limits = c(-5,100))+labs(x="pre",y="post")+
geom_hline(yintercept = 0,col=2)+ geom_abline(intercept = 0, slope = 1,col=2)+
facet_wrap( ~ lab, ncol=5,labeller = label_parsed)
我能做的最好的就是把那些&#34; beta_i&#34;标签。我感谢任何建议
答案 0 :(得分:0)
正如您在第一个图中所看到的,您实际上并不需要用ShapeRenderer
包裹每个beta[i]
; expression()
应直接从字符中解释ggplot
语言。
相反,请尝试使用以下格式将标签创建为字符向量:
plotmath