1

I have some data for which I want to get density plots. For each facet, I want to use personalized maths expression, but unfortunately, this doesn't display correctly using Tex function:

ggplot(data = x, aes(x=.data[[clinical_factor]], y=LBORRES,color=.data[[clinical_factor]]))+
      geom_violin()+theme_bw()+
      facet_wrap (~POPULATION, ncol=2, scales = "free_y", 
                  labeller = labeller(POPULATION = as_labeller(latex2exp::TeX)))
head(x)
   DISEASE LBORRES              POPULATION  OMIC_ID
1:     SjS     273               Monocytes 32140214
2:     SjS       0                    LDGs 32140214
3:     SjS       3  CD4$^+$CD8$^+$ T cells 32140214
4:     SjS       8 Non classical monocytes 32140214
5:     SjS    2490             Neutrophils 32140214
6:     SjS     146                 B Cells 32140214

And here's the output:

Density plots with bad Latex display

Do you have any idea how to deal with Math latex symbols with facet wrap?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • do you want to use exclusively math symbols for labelling the facets? or a combination of both. I ask because I see plain characters in your POPULATION variable – Mau Mar 17 '21 at 15:07

1 Answers1

1

I did not see the mathematical formulas in your POPULATION variable, but well, this may help you:

library(ggplot2)
library(latex2exp)

df <- data.frame(DISEASE=rep(c('SjS','Control'),each=1000),
                 LBORRES=matrix(rnorm(2000),ncol=1),
                 POPULATION=sample(LETTERS[1:6], 2000,replace=T,prob=c(0.15,0.15,0.15,0.15,0.15,0.25)))

#assign labels to the variable you want to use latex2exp:

df$POPULATION <- factor(df$POPULATION,labels=c('A'=parse(text=TeX('Monocyte $\\beta^2$')),
                                               'B'=parse(text=TeX('CD4+CD8+T cells$\\frac{a^2}{b}$')),
                                               'C'=parse(text=TeX('LDGs $\\alpha^2$')),
                                               'D'=parse(text=TeX('Non classical monocytes $\\mu^2$')),
                                               'E'=parse(text=TeX('Neutrophils $\\gamma^2$')),
                                               'F'=parse(text=TeX('B cells $\\alpha^3$'))))
                        
ggplot(data = df, aes(x=DISEASE, y=LBORRES,color=DISEASE))+
  geom_violin()+theme_bw()+
  facet_wrap (~POPULATION, ncol=2, scales = "free_y", 
              labeller=label_parsed)

This will give you something like

enter image description here

Mau
  • 131
  • 10