How do I Use "Symbol = Numeric" and "Character = Numeric" in Ggplot2 Label

I want the column header and row header of this plot to be phi = 0.8, phi = 0.9, phi = 0.95 and sd = 1, sd = 3, sd = 5, sd = 10 respectively. The phi should appear as the Greek letter symbol while the sd remains the English letter.

## simulate ARIMA(1, 0, 0)
set.seed(289805)
x1 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 1)
set.seed(671086)
x2 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 1)
set.seed(799837)
x3 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 1)
set.seed(289805)
x4 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 3)
set.seed(671086)
x5 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 3)
set.seed(799837)
x6 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 3)
set.seed(289805)
x7 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 5)
set.seed(671086)
x8 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 5)
set.seed(799837)
x9 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 5)
set.seed(289805)
x10 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 10)
set.seed(671086)
x11 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 10)
set.seed(799837)
x12 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 10)
xx <- 1:10

df <- data.frame(xx, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12)
reshapp <- reshape2::melt(df, id = "xx")

NEWDAT <- data.frame(y = reshapp$value, x = reshapp$xx, sd = rep(rep(c(sd=1, sd=3, sd=5, sd=10), each = 10), each = 3),phi = rep(rep(c(.8, .9, .95), each = 10), 4))

ggplot(NEWDAT, aes(x = x, y = y)) +  geom_line() +  geom_point() + labs(x = 'lb', y = 'RMSE') + facet_grid(sd ~ phi,  scales = "free_y") +
  theme_bw() + ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

Facet Grid

>Solution :

A quick fix is to either create or rename the faceted key with Phi expressed in UTF8 encoding, plus the creation of the string you want (ie. "Phi|SD = N")
In this case I create a new variable:

NEWDAT %>% 
mutate(phi_label = paste0("\U03D5 = ", phi), #\U03D5 represents ϕ character.
       sd_label = paste0("sd =", sd)) %>% 
ggplot(aes(x = x, y = y)) +  geom_line() +  geom_point() + labs(x = 'lb', y = 'RMSE') + 
facet_grid(sd_label ~ phi_label,  scales = "free_y") +
theme_bw() + ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

enter image description here

Leave a Reply