每个“facet_grid”面板上带有希腊字母的单独文本框

Individual text box with Greek letter on each `facet_grid` panel

我需要在 6 个 facet_grid 面板中的每个面板上填写 $\lambda$ = x(x 的 6 个值)

ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  facet_grid(am ~ cyl) +
  theme(panel.spacing = unit(1, "lines")) +
  annotate("text", label = c("\u03BB = 1", "\u03BB = 2", "\u03BB = 0.1", "\u03BB = 2.2", "\u03BB = 1.5", "\u03BB = 5") , size = 4, x = 30, y = 5)

我修改了这里的解决方案 Annotating text on individual facet in ggplot2 但出现错误。

Error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (36): label

Backtrace:
  1. base `<fn>`(x)
  2. ggplot2:::print.ggplot(x)
  4. ggplot2:::ggplot_build.ggplot(x)
  5. ggplot2 by_layer(function(l, d) l$compute_geom_2(d))
  6. ggplot2 f(l = layers[[i]], d = data[[i]])
  7. l$compute_geom_2(d)
  8. ggplot2 f(..., self = self)
  9. self$geom$use_defaults(data, self$aes_params, modifiers)
 10. ggplot2 f(..., self = self)
 11. ggplot2:::check_aesthetics(params[aes_params], nrow(data))

预期的图表应该有 lambda 的值跟随 = 符号如下

实现所需结果的一种选择是通过 geom_text 和 data.frame 标签添加标签,如下所示:

library(ggplot2)

df_label <- data.frame(
  label = c("\u03BB = 1", "\u03BB = 2", "\u03BB = 0.1", "\u03BB = 2.2", "\u03BB = 1.5", "\u03BB = 5"),
  am = rep(0:1, each = 3),
  cyl = rep(c(4, 6, 8), 2)
)

ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  geom_text(data = df_label, aes(label = label), size = 4, x = 30, y = 5) +
  facet_grid(am ~ cyl) +
  theme(panel.spacing = unit(1, "lines"))