如何在 bquote 中打印加减号和 beta 号,并正确导出为 pdf
How to print plus-minus and beta signs in bquote, and correctly export to pdf
¿如何在 R 中的 bquote() 表达式中打印 ± 符号?
我试过以下方法:
pm
%pm%
±
这些都没用。
更新 #1 这是一些示例代码
plot(NULL,xlim=c(0,10),ylim=c(0,10),xlab=NA,ylab=NA,xaxs="i",yaxs="i")
c <- "name"
p <- .004
n <- 969
b <- 1.23
s <- 0.45
tmp.txt <- paste(c(c," (n=",n,")\nslope = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")
text(9.5,9.5,labels=tmp.txt,adj=c(1,1),cex=.75)
我想要做的是让第二行有 beta(符号)而不是斜率,并出现 ± 符号。如果我使用表达式,我可以获得 beta,但不是 ±;如果我只是粘贴 ß(或类似的东西),它不会 运行.
更新#2:看来我必须使用 bquote()...否则当通过 pdf() 输出时不会打印 beta 字符。
对 this question 的回答建议使用 paste
和 bquote
。然后,您可以使用 ±:
的 Unicode 字符
x <- 232323
plot(1:10, main = bquote(paste(ARL[1], " curve for ", S^2, "; x=\U00B1",.(x))))
请注意,此示例(不包含 \U00B1)来自 fabian 对先前链接问题的回答。
我很感激所提供的建议,但它并没有完全实现我的目标。这是我想出的解决方法(我个人认为它只是缺少 asinine……但我不知所措)。
c <- "name"
p <- .004
n <- 969
b <- 1.23
s <- 0.45
## draw empty plot
plot(NULL,xlim=c(0,10),ylim=c(0,10),xlab=NA,ylab=NA,xaxs="i",yaxs="i")
## place the "poor man's substitute"
tmp.txt <- paste(c(c," (n=",n,")\nslope = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")
text(9.5,9.5,labels=tmp.txt,adj=c(1,1),cex=.75)
## place the next best option
tmp.txt <- paste(c(c," (n=",n,")\n\U03B2 = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")
text(9.5,7.5,labels=tmp.txt,adj=c(1,1),cex=.75)
## place the two boxes to superimpose the bquote() version
tmp.txt2 <- paste(c(c," (n=",n,")\n\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")
text(9.5,5.5,labels=tmp.txt2,adj=c(1,0.5),cex=.75)
text(9.5,5.5,labels=bquote(beta == .(b)%+-%.(s)),adj=c(1,0.5,cex=.75))
## same as above, but piped to a *.pdf
pdf("tmp_output.pdf")
plot(NULL,xlim=c(0,10),ylim=c(0,10),xlab=NA,ylab=NA,xaxs="i",yaxs="i")
tmp.txt <- paste(c(c," (n=",n,")\nslope = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")
text(9.5,9.5,labels=tmp.txt,adj=c(1,1),cex=.75)
tmp.txt <- paste(c(c," (n=",n,")\n\U03B2 = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")
text(9.5,7.5,labels=tmp.txt,adj=c(1,1),cex=.75)
tmp.txt2 <- paste(c(c," (n=",n,")\n\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")
text(9.5,5.5,labels=tmp.txt2,adj=c(1,0.5),cex=.75)
text(9.5,5.5,labels=bquote(beta == .(b)%+-%.(s)),adj=c(1,0.5,cex=.75))
dev.off()
如果你运行这个,它似乎在 R 内部和生成的 *.pdf 文件中都有效。
一如既往,我们将不胜感激更优雅(和明智)的解决方案。
¿如何在 R 中的 bquote() 表达式中打印 ± 符号?
我试过以下方法:
pm
%pm%
±
这些都没用。
更新 #1 这是一些示例代码
plot(NULL,xlim=c(0,10),ylim=c(0,10),xlab=NA,ylab=NA,xaxs="i",yaxs="i") c <- "name" p <- .004 n <- 969 b <- 1.23 s <- 0.45 tmp.txt <- paste(c(c," (n=",n,")\nslope = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="") text(9.5,9.5,labels=tmp.txt,adj=c(1,1),cex=.75)
我想要做的是让第二行有 beta(符号)而不是斜率,并出现 ± 符号。如果我使用表达式,我可以获得 beta,但不是 ±;如果我只是粘贴 ß(或类似的东西),它不会 运行.
更新#2:看来我必须使用 bquote()...否则当通过 pdf() 输出时不会打印 beta 字符。
对 this question 的回答建议使用 paste
和 bquote
。然后,您可以使用 ±:
x <- 232323
plot(1:10, main = bquote(paste(ARL[1], " curve for ", S^2, "; x=\U00B1",.(x))))
请注意,此示例(不包含 \U00B1)来自 fabian 对先前链接问题的回答。
我很感激所提供的建议,但它并没有完全实现我的目标。这是我想出的解决方法(我个人认为它只是缺少 asinine……但我不知所措)。
c <- "name" p <- .004 n <- 969 b <- 1.23 s <- 0.45 ## draw empty plot plot(NULL,xlim=c(0,10),ylim=c(0,10),xlab=NA,ylab=NA,xaxs="i",yaxs="i") ## place the "poor man's substitute" tmp.txt <- paste(c(c," (n=",n,")\nslope = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="") text(9.5,9.5,labels=tmp.txt,adj=c(1,1),cex=.75) ## place the next best option tmp.txt <- paste(c(c," (n=",n,")\n\U03B2 = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="") text(9.5,7.5,labels=tmp.txt,adj=c(1,1),cex=.75) ## place the two boxes to superimpose the bquote() version tmp.txt2 <- paste(c(c," (n=",n,")\n\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="") text(9.5,5.5,labels=tmp.txt2,adj=c(1,0.5),cex=.75) text(9.5,5.5,labels=bquote(beta == .(b)%+-%.(s)),adj=c(1,0.5,cex=.75)) ## same as above, but piped to a *.pdf pdf("tmp_output.pdf") plot(NULL,xlim=c(0,10),ylim=c(0,10),xlab=NA,ylab=NA,xaxs="i",yaxs="i") tmp.txt <- paste(c(c," (n=",n,")\nslope = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="") text(9.5,9.5,labels=tmp.txt,adj=c(1,1),cex=.75) tmp.txt <- paste(c(c," (n=",n,")\n\U03B2 = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="") text(9.5,7.5,labels=tmp.txt,adj=c(1,1),cex=.75) tmp.txt2 <- paste(c(c," (n=",n,")\n\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="") text(9.5,5.5,labels=tmp.txt2,adj=c(1,0.5),cex=.75) text(9.5,5.5,labels=bquote(beta == .(b)%+-%.(s)),adj=c(1,0.5,cex=.75)) dev.off()
如果你运行这个,它似乎在 R 内部和生成的 *.pdf 文件中都有效。
一如既往,我们将不胜感激更优雅(和明智)的解决方案。