Axis label specifications in ggplot

Viewed 135

I'm attempting to have a two-line y-axis label that contains a superscript in ggplot and I am struggling.

I want the y axis label to say “[3H]ASEM binding (pmol/g)” with the 3 superscripted and (pmol/g) on a separate line.

This is what I have tried so far:

labs(x="", y=expression(paste("[" ^3 "H] ASEM Binding \n (pmol/g)"))) 

And it's given me the error "unexpected string constant"

Any suggestions?

3 Answers

You need an empty ''(2 single quotes) prior to the ^3.

  ggplot(sample_data, aes(x, y)) +
  geom_point() +
  labs(
    x = "",
    y = expression(atop(paste("[", ''^3, "H] ASEM Binding"), "(pmol/g)"))
  )

enter image description here

Another alternative is:

y = expression(atop("["^3*"H] ASEM Binding", "(pmol/g)"))

I'm not quite sure what you're doing with your for loop, but this code chunk should get you the two-lined axis label with a superscript for which you are looking.

library(tidyverse)

sample_data <- tibble(x = rnorm(1000),
       y = x^2)

sample_data %>%
  ggplot(aes(x, y)) +
  geom_point() +
  labs(
    x = "X",
    y = expression(atop("Variable", X^2))
  )

Is this what you are after?

library(ggplot2)

ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point()+
  labs(x = "",
       y = expression(atop("[ "^3*H~"] ASEM Binding", "(pmol/g)")))+
  theme(plot.margin = unit(c(10, 10, 20, 10), "mm"))

Created on 2020-05-19 by the reprex package (v0.3.0)

Related