Home > Software engineering >  How to add subscription for the "greater than or equal to " sign in ggplot?
How to add subscription for the "greater than or equal to " sign in ggplot?

Time:04-07

I'm trying to use an expression that has a subscripted greater than or equal sign as the facet_grid title for my plot. The latex version of the expression goes like

enter image description here .

First I used the following code to have the greater than or equal to sign in the expressions, which can be correctly parsed and displayed by the facet_grid function.

data$type <- factor(data$type, levels = c('A holds', "A doesn't hold"),labels = c(expression(S>=T), expression(S<=T))).

But when I try to add the subscription A with code expression(S>=[A]T), R keeps reporting me an error

Unexpected token '[' .

Any suggestions on how to incorporate the subscription as the latex above?

CodePudding user response:

You need an invisible placeholder before [A] to have a legal expression - you can use phantom for this:

library(ggplot2)

data$type <- factor(data$type, levels = c('A holds', "A doesn't hold"),
                    labels = c(expression(S >=phantom(0)[A]~T), 
                               expression(S <= phantom(0)[A]~T)))

ggplot(data, aes(x, y))   
  geom_point()  
  facet_grid(.~type, labeller = label_parsed)  
  theme(strip.text = element_text(size = 20))

Created on 2022-04-06 by the reprex package (v2.0.1)


Data

data <- data.frame(x = rep(1:10, 2), y = rnorm(20),
                   type = rep(c('A holds', "A doesn't hold"), each = 10))
  • Related