0

I want to plot data using ggplot(), that is split into multiple plots by using facet_wrap() on three different columns of the data. So let's say p is my plot created by ggplot2, then I have

p +
  facet_wrap(~ column1 + column2 + column3)

Each column is a factor variable with 2 - 3 levels. Per default, the labels of the individual plots only show the value of the corresponding level, but I also want to show which column the value is coming from. So if column1 represents beta_2, instead of a label just showing "150", I want it to show "beta_2 = 150", and I want "beta_2" to be displayed as a Greek letter subsetted by 2. The other columns have different names, but they all involve Greek letters and subsetting.

I tried multiple approaches involving renaming the levels with the TeX() function with the latex2exp package, or different labeller functions that switch the level value to an expression involving the column name as a Greek letter (tried to produce that with both expression() as well as bquote()), but nothing worked so far.

I would be very thankful for your help.

stefan
  • 90,330
  • 6
  • 25
  • 51

1 Answers1

0

One option would be to use some ?plotmath and ggplot2::label_parsed.

Using a fake example based on mtcars:

library(ggplot2)

mtcars2 <- mtcars

mtcars2$cyl <- paste0("alpha[1]==", mtcars2$cyl)
mtcars2$am <- paste0("beta[2]==", mtcars2$am)
mtcars2$gear <- paste0("gamma[3]==", mtcars2$gear)

ggplot(mtcars2, aes(hp, mpg)) +
  geom_point() +
  facet_wrap(~ cyl + am + gear, labeller = label_parsed)

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thanks a lot for your answer. If I apply this code to my data, I get the following error message Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : invalid use of -963 < 0 in 'X11_MetricInfo' and no resulting plot. As I am working on a server rather than on my local computer, I assume it has something to do with the server system. Does anyone know how this can be resolved? – Anouk Petitpierre Mar 07 '23 at 13:55
  • Hi Anouk. To help you any further we need a [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a "working" example of your code which others could run and a snippet of your data or some fake data. As is we can't even guess what might be the issue as we neither have your code nor your data. – stefan Mar 07 '23 at 17:01