在替换中插入leq符号,R中的数学注释

问题描述

我有以下图形:

library(tidyverse)

mm<-70
sdm<-12
weight_lim<-c(30,110)
xrange<-55
ggplot(data = data.frame(weight = weight_lim),aes(weight)) +
  stat_function(fun = dnorm,n = 101,args = list(mean = mm,sd = sdm),color=1) +
  ylab("f(weight)") + scale_x_continuous(breaks=seq(weight_lim[1],weight_lim[2],by=5)) + 
  stat_function(fun = dnorm,sd=sdm),xlim = c(weight_lim[1],xrange[1]),geom = "area",fill="red",alpha=0.5)+
  annotate("text",x = 40,y = .02,label = substitute(paste("P(X < ",v," ) = ",s),list(v=format(xrange,nsmall = 1),s=round(pnorm(xrange,mm,sdm),4))),size=3,fontface="bold")+
  theme_bw()
#> Warning in is.na(x): is.na() applied to non-(list or vector) of type 'language'

reprex package(v0.3.0)于2020-08-22创建

我想用“小于或等于”登录名替换“

substitute(paste("P(X < ",s=round(dnorm(xrange,4)))

解决方法

插入字符最简单的方法之一是使用\u转义序列。它与其他任何转义序列一样使用,其中Unicode字符代码紧跟\u。您可以在此处查看示例:

library(ggplot2)

ggplot(mtcars,aes(disp,mpg)) + geom_point() + theme_classic() +
  annotate('text',x=300,y=29,label='Unicode 2264: \u2264') +
  annotate('text',y=26,label='Unicode 2265: \u2265')

enter image description here

如您所见,小于或等于Unicode 2264,因此只需在标签中直接使用\u2264

,

如果有

vv = format(xrange,nsmall = 1)
ss = round(dnorm(xrange,mm,sdm),4)

那么这两个都可以。

lab = bquote(P(X <= .(vv)) == .(ss)) 
# or
lab = substitute(P(X <= v) == s,list(v=vv,s=ss))

p + annotate("text",x = 40,y = .02,label = deparse(lab),parse=TRUE)

annotate a formula (with bqoute or substitute) in R (ggplot) gives Error注意到annotate不使用表达式。因此deparse/parse的东西摆脱了警告。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...