subscripts and superscripts facet_wrap (facet labels)

Hello,
I want to change the facet_wrap labels, because the parameters have subscripts and superscripts. I tried to create the labels and use label_parsed, but it didn't work.
Can anyone help?

g1 <- ggplot(data1, aes(x = valor, fill = categoria)) +
geom_histogram()+
scale_fill_manual(values = c("#FF3333", "#3399FF", "#33FF33", "#9933FF",
"#FF33FF", "#A0A0A0"))+
theme_bw() +
scale_x_continuous(name="Valor dos parâmetros")+
scale_y_continuous(name="Parametrizações")+
theme(panel.border=element_blank(),
legend.position = "bottom",
axis.text.y = element_text(size = 30, color = "black"),
axis.text.x = element_text(size=30, color = "black"),
axis.title.x = element_text(size = 45),
axis.title.y = element_text(size = 45),
legend.text = element_text(size=35),
axis.line = element_line(),
legend.title=element_blank(),
strip.text.x = element_text(size = 40))+
facet_wrap(~parametro, scales = "free_x",ncol=4);g1

facet_wrap() has an option to rewrite the facet labels. It is a bit unintuitive as it requires a special function called a labeller. But it's very easy to create using as_labeller(). You just need to provide a named vector that gets used as lookup table. For example:

my_labeller <- as_labeller(c(setosa="A", versicolor="B", virginica="C"))
ggplot(iris) +
  geom_bar(aes(x=Sepal.Length)) +
  facet_wrap(~Species, labeller = my_labeller)

But you have an additional difficulty: you want your labels to contain subscripts and superscripts. So you have to tell as_labeller() that it will need to parse the labels you provide, using its second argument (the default labeller):

my_labeller <- as_labeller(c(setosa="A[0]", versicolor="B^1", virginica="Gamma"),
                           default = label_parsed)

ggplot(iris) +
  geom_bar(aes(x=Sepal.Length)) +
  facet_wrap(~Species, labeller = my_labeller)

1 Like

Thank you Alexis!! It works!
But only for those who have no symbol, how can I add them? Including the degree symbol for temperature.

I don't understand your question exactly. Do you mean how to use those who already have a symbol such as °?

If that's the case, I don't have a good solution (perhaps label_bquote(), but I can't figure out how to use it here). I would go with replacing these characters beforehand:

my_labels <- c(setosa="A[0] (°C)", versicolor="B^1", virginica="eta")

my_labels_processed <- str_replace_all(my_labels, "°", "degree*") %>%
  set_names(names(my_labels))

my_labeller <- as_labeller(my_labels_processed,
                           default = label_parsed)

ggplot(iris) +
  geom_bar(aes(x=Sepal.Length)) +
  facet_wrap(~Species, labeller = my_labeller)

Or equivalently:

my_names_processed <- str_replace_all(my_names, "°", "symbol('\\\\260')*") %>%
  set_names(names(my_names))

You can find a description of the "plotmath" annotation here with the characters available.

1 Like

I'm still receiving the follow error:
Error in parse(text = as.character(values)) :
:1:10: unexpected symbol
1: g[Cx] (m s
^

Hello,
The variables in the database were like symbols, so I replaced them with letters and ran the first command suggested by Alexis again, using the name of the symbols (gamma, eta ...)
I also removed the spaces, as they were generating the above error.
In some cases I had to add the symbol "*" between the symbol name and the letter. Now it worked perfectly. = D
Thank you for your help!!

my_labeller <- as_labeller(c("p2"="p[2]","p20" ="p[20]","as"= "a[s]","ns" = "n[s]","nRx" = "eta[Rx]","nRN"="eta[RN]","YF0 (mês-1)" = "gamma[F0] (mês^-1)","YF1 (mês-1)" = "gamma[F1] (mês^-1)","tYF (meses)" = "t[gamma*F] (meses)",,.......),default = label_parsed)

Great!

Yeah I should have insisted on that strange quirk of plotmath where you need * to keep symbols together and ~ to add a space. I'm not sure where that decision comes from.

1 Like

Degree sign can be added with:
"Temperature~(degree*C)"

1 Like