0

I want to plot a data frame similar to the following example:

rho_values <- 1:3
rho0_values <- 4:6
combinations <- expand.grid(rho_values, rho0_values)
mydf <- data.frame(rho = rep(combinations[,1], each = 10), rho0 = rep(combinations[,2], each = 10), val = rnorm(90), x = rnorm(90))

p <- ggplot(mydf, aes(x, val)) + geom_point()
p <- p + facet_grid(rows = vars(rho), cols = vars(rho0), labeller = "label_both")
p

This results in a facet grid plot, where the labels are "rho: 1", "rho: 2", ... in the rows, and "rho0: 4", "rho0: 5", ... in the columns. Instead, I would like to have labels of the form $\rho = 1$, $\rho = 2$, ... in the rows, and $\rho_0 = 4$, $\rho_0 = 5$, ... in the columns (sorry, I am not sure how to use LaTeX here...).

How can I achieve this?

user3825755
  • 883
  • 2
  • 10
  • 29
  • 2
    Have a look [here](https://stackoverflow.com/questions/5293715/how-to-use-greek-symbols-in-ggplot2). Here is an extensive explanation on how to do that. – ricoderks Dec 12 '19 at 13:47
  • @ricoderks I have read this, yet, it is not clear to me how can can get the "_0" index from "rho0". Renaming th columns to `c("rho", "rho[0]")` and using `labeller = "label_parsed"` does not work – user3825755 Dec 12 '19 at 13:59

1 Answers1

2
p <- p + facet_grid(rows = vars(rho), cols = vars(rho0), 
                    labeller = label_bquote(rows = rho==.(rho), 
                                            cols = rho[0]==.(rho0)))

enter image description here

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225