2

This is a continuation of this question: Labeling facet strips in ggplot2

By using the following code, I can obtain Year = 2009, Year = 2010 to the top of each sub plot:

set.seed(1)
df=data.frame(year=rep(2009:2013,each=4),
              quarter=rep(c("Q1","Q2","Q3","Q4"),5),
              sales=40:59+rnorm(20,sd=5))
library(ggplot2)
ggplot(df) +
  geom_line(aes(x=quarter,y=sales,group=year))+
  facet_wrap(
    .~year,
    labeller = as_labeller(\(x) paste0("Year=", x)),
    strip.position = "top",
    scales = "free") +
  theme(#panel.spacing = unit(0, "lines"),
    strip.placement = "outside",
    axis.title.x=element_blank(),
    legend.position="none") 

How should I modify the aforementioned code if I need to have a subscript, let's say for example, instead of Year = 2009, I need to display Year ab = 2009

I tried giving

... +
  facet_wrap(
    .~year,
    labeller = as_labeller(\(x) paste0("Year", bquote(a[b]) ,"=", x)),
    strip.position = "top",
    scales = "free"
  ) +
  ...

but it failed.

1 Answers1

3

You could try the unicode value for the subscript, e.g.

library(tidyverse)
set.seed(1)
df=data.frame(year=rep(2009:2013,each=4),
              quarter=rep(c("Q1","Q2","Q3","Q4"),5),
              sales=40:59+rnorm(20,sd=5))

ggplot(df) +
  geom_line(aes(x=quarter,y=sales,group=year))+
  facet_wrap(
    .~year,
    labeller = as_labeller(\(x) paste0("Year a\u2090 = ", x)),
    strip.position = "top",
    scales = "free") +
  theme(#panel.spacing = unit(0, "lines"),
    strip.placement = "outside",
    axis.title.x=element_blank(),
    legend.position="none")

Created on 2022-11-30 with reprex v2.0.2

Whether or not this approach will work will depend on which subscript letters you want to use, i.e. there's a subscript "a" (shown in the plot above) but I don't think there is a subscript "b".

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • 2
    Excellent solution. What I wanted was subscript capital E in my case. And I used the unicode \u1D07 and it perfectly worked. Many thanks! – Dovini Jayasinghe Nov 30 '22 at 05:07
  • It's a perfect solution for customizing "label_both"-like facet strips – Tianjian Qin Dec 06 '22 at 11:07
  • @jared_mamrot could you kindly tell me on how to get the subscript zero within the same code "labeller" ? I tried giving \u2080 but it gave me a square instead. When "\u2080" is compiled separately in R, it correctly gives the symbol. That means R can take it as subscript zero, but why not here? – Dovini Jayasinghe Jan 17 '23 at 10:16
  • @TianjianQin Kindly share if you have any solution for the top comment on subscript zero. – Dovini Jayasinghe Jan 17 '23 at 10:19
  • 1
    The default font for ggplot2 is "Arial". The subscript zero isn't available in this font. If you change the font to something different (e.g. `+ theme(text = element_text(family = "mono"))`) it renders correctly – jared_mamrot Jan 17 '23 at 10:25
  • @jared_mamrot Tried, but it gave me the same "empty square" instead of subscript zero – Dovini Jayasinghe Jan 17 '23 at 11:17