Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I need to fill $\lambda$ = x (6 values of x) on each of the 6 facet_grid panels

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)

I’ve modified the solutions here
Annotating text on individual facet in ggplot2 but got an error.

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

The expected graph should have the values of lambda follow the = sign as follow

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

One option to achieve your desired result would be to add the labels via a geom_text and a data.frame of labels like so:

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading