Ken Reid
Ken Reid

Reputation: 609

Concatenating Greek Symbol & Double & String in R

I'm making a boxplot which has the variance shown on the plot, as follows:

boxplot(avgs, xlab="Technique",ylab="Accuracy of Prediction", cex = 1.5)

num <- round(var(data$gblup),3)
label <- expression(paste(sigma,"² = ",num))
text(x= 1, y= 0.4, labels= label)

However, I'm instead seeing this:

enter image description here

I'm trying to place the contents of the double num into the label, without much luck. Thanks in advance for any help in doing this.

Upvotes: 0

Views: 155

Answers (2)

akrun
akrun

Reputation: 887148

We can construct the expression with bquote

num <- round(var(iris$Sepal.Length),3)
label <- bquote(sigma^2 == .(num))
boxplot(iris$Sepal.Length)
text(1, 6, labels = label)

-output

enter image description here

Upvotes: 1

r2evans
r2evans

Reputation: 160447

Here's one way, I'll use sprintf to ensure three digits, and bquote to handle the expression and exponent.

boxplot(mtcars$disp)
num <- sprintf("%0.03f", var(mtcars$disp))
text(1, 220, bquote(sigma^2 * " = " * .(num)))

sample plot

(I like akrun's use of ==, which produces a slightly longer equals sign and looks more intuitive in the code.)

Upvotes: 1

Related Questions