MYaseen208
MYaseen208

Reputation: 23898

Label with mathematical expressions in two line with facet_grid for two grids in ggplot2

I want to add label with mathematical expressions in two line with facet_grid for two grids (See MWE). I can get that in one line, wonder to get in two lines (Beta in one line and Gamma in second line in second graph).

library(ggplot2)

p1 <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()

p1 + facet_grid(
    facets = gear ~ vs + am
  , labeller = label_both
  )

enter image description here

p1 + facet_grid(
     facets = gear ~ vs + am
    , labeller = 
       label_bquote(
         rows = alpha:.(gear)
      , cols = list(beta:.(vs), gamma:.(am))
      )
    )

enter image description here

Upvotes: 3

Views: 331

Answers (1)

pogibas
pogibas

Reputation: 28309

You can use atop() instead of list():

library(ggplot2)
ggplot(mtcars, aes(mpg, wt)) + 
    geom_point() +
    facet_grid(gear ~ vs + am, 
        labeller = label_bquote(
            rows = alpha:.(gear), 
            cols = atop(beta:.(vs), gamma:.(am))))

enter image description here

atop() is a brute force approach as it puts "x over y (no horizontal bar)"

Upvotes: 1

Related Questions