I'm trying to add mathematical expressions to my plot using text(x,y,expression(...))
. However, I'm getting a mysterious error message:
Warning: Error in text.default: invalid 'vfont' value [typeface -2147483648]
In the code samples below, the uncommented lines work as expected but the commented lines generate the error:
This part just sets up default values for the random numbers used later.
inputPanel(
selectInput("M", label = "Number of Repetitions:",
choices = c(50, 100, 200), selected = 100),
numericInput("seed", label = "Random number Seed (integer)",
min = 0, max = .Machine$integer.max,
value = floor(runif(1)*.Machine$integer.max),
step = 157)
)
renderText({
M <<- as.numeric(input$M)
set.seed(input$seed)
Z <<- rnorm(M)
paste("Number of Repetitions =",M,"Random Seed = ",input$seed,"\n")
})
Here is where the problem occurs
inputPanel(
selectInput("N", label = "Sample Size:",
choices = c(1,5,10,25,50,100), selected = 1),
sliderInput("my", label = "Mean of Y:",
min=0, max=100, value=50, step=1),
sliderInput("sy", label = "Standard Deviation of Y:",
min = 0.2, max = 25, value = 10, step = 0.1)
)
renderPlot({
my <<- input$my
sy <<- input$sy
sem <<- input$sy/sqrt(as.numeric(input$N))
X <<- Z*sem+my
pch <<- floor(abs(Z))+1
pch <<- ifelse(pch>2,5,pch)
curve(dnorm(x,my,sem),xlim=c(my-3.5*sy,my+3.5*sy),ylab="density",
xlab="Sample Mean")
abline(v=my)
abline(h=0)
text(my+.25*sy,.02,expression(mu[Y]))
abline(v=my-2*sem)
text(my-2*sem+.25*sy,0.0025,expression(-2*sigma[bar(Y)]))
abline(v=my-sem)
#text(v=my-sem+.25*sy,0.0025,expression(-sigma[bar(Y)]))
abline(v=my+sem)
#text(v=my+sem+.25*sy,0.0025,expression(+1*sigma[bar(Y)]))
abline(v=my+2*sem)
#text(v=my+2*sem+.25*sy,0.0025,expression(2*sigma[bar(Y)]))
points(X,rep(0,M),pch=pch)
})
renderText(
paste("Standard Error = ",round(sem,3),":\n",
sum(pch==1), "Estimates less than 1 SE from mean;\n",
sum(pch==2), "Estimates between 1 and 2 SE from mean;\n",
sum(pch==5), "Estimates more than 2 SE from mean.\n"))
I can't figure out why some of the expressions work and some do not. The differences are pretty small.