Home > Net >  Labelling issues with Greek letters, superscript and expression in facet_wrap
Labelling issues with Greek letters, superscript and expression in facet_wrap

Time:08-25

I have 4 plots which I have merged/wrapped into 2x2 plots using facet_wrap(). But I am unable to fix the labels of the plots. Here are my codings.

labelp = c('0.6' = expression(paste(phi, ""[0], " = 0.60")),
           '0.7' = expression(paste(phi, ""[0], " = 0.70")),
           '0.8' = expression(paste(phi, ""[0], " = 0.80")),
           '0.9' = expression(paste(phi, ""[0], " = 0.90")))

ggplot(data,aes("x" = N, "y" = alpha)) 
  geom_ribbon(aes(ymin = low, ymax = upp), fill = "grey") 
  geom_point() 
  facet_wrap(~p, nrow = 2, labeller = as_labeller(labelp)) 
  labs("y" = "Type I error rates")

My figure is given below: Figure 1

I would like to change label for each plots. Meaning, instead of $0.6$, I want $\phi_0 = 0.60$ and so on. How should I edit my labeller for facet_wrap()?

I read about label_parsed so I have tried using that. Here is my code.

labelp = as_labeller(c('0.6' = "Phi[0] = 0.60", '0.7' = "Phi[0] = 0.70",
                       '0.8' = "Phi[0] = 0.80",'0.9' = "Phi[0] = 0.90"),
                          default = label_parsed)

ggplot(data,aes("x" = N, "y" = alpha)) 
  geom_ribbon(aes(ymin = low, ymax = upp), fill = "lightblue") 
  geom_point() 
  geom_hline(yintercept = 0.05, linetype = "dashed") 
  facet_wrap(~p, nrow = 2, labeller = labelp) 
  labs("y" = "Type I error rates")

This codes resulted in this figure: Figure 2

Both are not what I want. Any clues how to fix this?

CodePudding user response:

First I created some fake data. You could use the function glue to create these labels. With {} you assign the greek symbols to each value of your specific column in this case "p". You can use the following code:

data <- data.frame(N = runif(20, 200, 600),
                   alpha = runif(20, 0.03, 0.06),
                   low = runif(20, 0.03, 0.04),
                   upp = runif(20, 0.04, 0.05),
                   p = rep(c(0.6, 0.7, 0.8, 0.9), 5))

library(ggplot2)
library(glue)

ggplot(data,aes("x" = N, "y" = alpha)) 
  geom_ribbon(aes(ymin = low, ymax = upp), fill = "lightblue") 
  geom_point() 
  geom_hline(yintercept = 0.05, linetype = "dashed") 
  facet_wrap(~glue('Phi[0]*" = {p}"'), nrow = 2, labeller = label_parsed) 
  labs("y" = "Type I error rates")

Created on 2022-08-25 with reprex v2.0.2

  • Related