如何在 Ggplot2 标签中使用 "Symbol = Numeric" 和 "Character = Numeric"

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

我希望此图的列 header 和行 header 分别为 phi = 0.8, phi = 0.9, phi = 0.95sd = 1, sd = 3, sd = 5, sd = 10phi 应显示为希腊字母符号,而 sd 仍为英文字母。

## 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 调用而不是您现在正在使用的调用。

  facet_grid(sd ~ phi,  scales = "free_y", labeller = . %>% label_both(sep = " = "))

使用bquote

  facet_grid(sd ~ phi,  scales = "free_y", 
             labeller = 
               label_bquote(rows = sigma == .(sd), cols = phi == .(phi))
  )

这将产生正确的标签。

答案灵感来自

[编辑以说明第三行代码中的重构]

一个快速解决方法是使用以 UTF8 编码表示的 Phi 创建或重命名分面键,以及创建您想要的字符串(即“Phi|SD = N”) 在这种情况下,我创建了一个新变量:

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))

这是 plotmath 仅使用标准字符,没有特殊转义序列的解决方案。

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()