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))
CodePudding user response:
[Edited to account for the refactor in the third line of code]
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 the character "ϕ"
sd_label = fct_reorder(.f = paste0("sd =", sd), .x = 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))
CodePudding user response:
Here is a solution with plotmath
using only standard characters, no special escape sequences.
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))
NEWDAT$sd <- factor(NEWDAT$sd, levels = NEWDAT$sd, labels = paste("sd ==", NEWDAT$sd))
NEWDAT$phi <- with(NEWDAT, factor(phi, levels = phi, labels = paste("phi ==", phi)))
ggplot(NEWDAT, aes(x = x, y = y))
geom_line()
geom_point()
scale_y_continuous(expand = c(0.0, 0.00))
labs(x = 'lb', y = 'RMSE')
facet_grid(
sd ~ phi,
scales = "free_y",
labeller = label_parsed
)
theme_bw()
CodePudding user response:
A partial answer is this: Use this facet_grid
call instead of the one you're using right now.
facet_grid(sd ~ phi, scales = "free_y", labeller = . %>% label_both(sep = " = "))
Using bquote
facet_grid(sd ~ phi, scales = "free_y",
labeller =
label_bquote(rows = sigma == .(sd), cols = phi == .(phi))
)
This will yield the right labels.
Answer inspired from this post