Inserimento del simbolo leq in sostituto, Annotazione matematica in R

Aug 22 2020

Ho il seguente grafico:

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, args = list(mean = mm,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'

Creato il 22-08-2020 dal pacchetto reprex (v0.3.0)

Vorrei sostituire "<" con "minore o uguale a" accedi

substitute(paste("P(X < ",v," ) = ",s),list(v=format(xrange, nsmall = 1),s=round(dnorm(xrange,mm,sdm),4)))

Risposte

1 user20650 Aug 22 2020 at 22:15

Se hai

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

Quindi uno di questi funzionerà.

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)

Notare da annotare una formula (con bqoute o substitute) in R (ggplot) restituisce un errore che annotatenon accetta espressioni., Da qui il deparse/parsemateriale per sbarazzarsi degli avvertimenti.

1 chemdork123 Aug 22 2020 at 21:48

Uno dei modi più semplici per inserire un carattere è utilizzare la \usequenza di escape. Viene utilizzato proprio come qualsiasi altra sequenza di escape, dove segue il codice dei caratteri Unicode \u. Puoi vedere l'esempio qui:

library(ggplot2)

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

Come puoi vedere, minore o uguale a è Unicode 2264, quindi usalo \u2264direttamente nella tua etichetta.