rggplot2facet-wrapsubscript

How can I convert the panel title to subscript when using facet_wrap()?


I want to change the panel titles of the graph to subscripts when using facet_wrap(). For example, when I create a graph, I want to change the title 'B/R1.1' or 'B.R2.1' to appear as the right figure, showing only the numbers as subscripts. Could you tell me how to do that?

df = data.frame(
  Genotype = c("A", "B", "A", "B"),
  Treatment = c("B/R1.1", "B/R1.1", "B/R2.1", "B/R2.1"),
  yield = c(100, 150, 88, 120)
)

ggplot(df, aes(x=Genotype, y=yield)) +
  geom_bar(stat="identity", position="dodge", width=0.9, size=1) +
  facet_wrap(~ Treatment, scales="free") +
  theme_classic(base_size=22, base_family="serif")+
  theme(axis.line=element_line(linewidth=0.5, colour="black"),
        strip.background=element_rect(color="white",
                                      linewidth=0.5,linetype="solid"))

enter image description here

Thanks,


Solution

  • Try the following

    df = data.frame(
      Genotype = c("A", "B", "A", "B"),
      Treatment = c("B/R[1.1]", "B/R[1.1]", "B/R[2.1]", "B/R[2.1]"),
      yield = c(100, 150, 88, 120)
    )
    
    ggplot(df, aes(x=Genotype, y=yield)) +
      geom_bar(stat="identity", position="dodge", width=0.9, size=1) +
      facet_wrap(~ Treatment, scales="free", labeller = label_parsed) + # note new argument here
      theme_classic(base_size=22, base_family="serif")+
      theme(axis.line=element_line(linewidth=0.5, colour="black"),
            strip.background=element_rect(color="white",
                                          linewidth=0.5,linetype="solid"))
    

    Format the Treatment column for subscript using []. Then in facet_wrap tell ggplot to process the labels as follows labeller = label_parsed.

    enter image description here