Home > other >  How to add Greek letters, an equal sign and , a value in ggplot facet_wrap strip text?
How to add Greek letters, an equal sign and , a value in ggplot facet_wrap strip text?

Time:12-08

Labeller functions in facet wraps help, for example, to illustrate Greek letters in plots. But how can you include, e.g. an equal sign / element sign (epsilon) plus a value together with a Greek letter in facet wraps?!

If I'm plotting the following data, the labels are wrongly presented as =(\sigma,"value")


library(ggplot2)
dtf <- data.frame(MSE = cumsum(rnorm(20)),
                  Train = rep(seq(1,10,1),2),
                  Variance = rep(c("sigma = 0.3","sigma = 0.5"),each=10))

ggplot(data=dtf) 
  geom_line(aes(x=Train, y=MSE),size=1)  
  theme_bw()   facet_wrap(~Variance, labeller = label_parsed)



enter image description here

CodePudding user response:

With label_bquote:

See help documentation for explanation of function: ?label_bquote and ?plotmath for build up of the mathematical expression.

set.seed(123)


dtf <- data.frame(MSE = cumsum(rnorm(20)),
                  Train = rep(seq(1,10,1),2),
                  Variance = rep(c( 0.3, 0.5), each = 10))
library(ggplot2)

ggplot(data = dtf) 
  geom_line(aes(x = Train, y = MSE), linewidth = 1)  
  theme_bw()   
  facet_wrap(~Variance, labeller = label_bquote(sigma == .(Variance)))

Created on 2022-12-07 with reprex v2.0.2

  • Related