I want to plot some functions with their gradients using the ggplot2 package in r.
p = 3
n0 = 100
z0 = seq(0.01, 0.99, length = n0)
AB0 = matrix(rbeta(600,4,1), nrow = n0)

library(ggplot2)
ab.names=c(paste("g",1:p,sep=""),paste("g' ",1:p,sep=""))
pl0=ggplot(data.frame(ab = c(AB0), ID = rep(ab.names, each = n0), Z = z0),
           aes(x = Z, y = ab)) +
  geom_point() +
  facet_wrap(~ID, scales = "free",nrow = 2, ncol = p) + theme_bw() +
  ggtitle("Unpenalized VCM",expression(nabla~"g  and  "~"g")) + ylab("")

I want to switch the rows and add the nabla symbol in the title of the plots in the 1st row before switching them to the second row. To be clear, the 1st row is for the functions, and the 2nd row is for the gradients where the nabla symbol should appear in the title.
Here is a screenshot of the outcome


in ggplot2, print an expression in the facet_wrap
the labeller argument to facet_wrap might come in handy, if you set it to "label_parsed". Example:
d <- data.frame(x = rnorm(2),
                y = rnorm(2),
                ID = c(paste0('g~', 1:2),
                       paste0('g~nabla~', 1:2)
                       )
                )


d |>
    ggplot(aes(x, y)) +
    geom_point() +
    facet_wrap(. ~ ID, 
               labeller = label_parsed,
               nrow = 2
               )


