How can I have a Greek symbol within one of my facet labels?

前端 未结 1 1874
你的背包
你的背包 2021-01-20 02:22

I\'m trying to create a plot using ggplot2 (v. 2_2.2.1) facet_wrap, and I need to have a Greek symbol in only one facet label (out of five). I have tried to use

相关标签:
1条回答
  • 2021-01-20 03:12

    Here is a trick that uses labeller=label_parsed.

    First, define labels for df$genes that uses the expression to be parsed:

    df$genes <- factor(df$genes, levels = c("BA","MLL","pos","neg","PMLalpha+"),
     ordered = TRUE, labels=c("BA","MLL","pos","neg",expression(paste("PML", alpha,"+"))))
    

    Then use labeller=label_parsed in facet_wrap:

    ggplot(df,aes(x=genes, y=value)) + geom_boxplot()+ 
      facet_wrap(~genes, ncol = 5, scales = "free_x", labeller = label_parsed)
    

    As you can see this messes with the x-axis labels, but you can fix it in scale_x_discrete as follows:

    ggplot(df,aes(x=genes, y=value)) + geom_boxplot()+ 
      facet_wrap(~genes, ncol = 5, scales = "free_x", labeller = label_parsed) +
      scale_x_discrete(name="",
                   breaks = c("BA","MLL","pos","neg","paste(\"PML\", alpha, \"+\")"),
                   labels = c("BA","MLL","pos","neg", expression(paste("PML", alpha,"+"))))
    

    0 讨论(0)
提交回复
热议问题