2

I am making qq plots of my data and found ggqqplot, which adds the line and the confidence intervals. I have stable isotope data and ultimately want my y-axis label to show: Sample δ13C Values.

since just working with the label, dataset does not matter - got this off the web

# Create some data
set.seed(1234)
wdata = data.frame(
  sex = factor(rep(c("F", "M"), each=200)),
  weight = c(rnorm(200, 55), rnorm(200, 58)))

head(wdata, 4)

# no italics no superscript
ggqqplot(wdata, 
         x = "weight",
         title = NULL,
         xlab = NULL,
         ylab = "Sample \u03B413C Values"
)

#tried bquote with ^ for superscript -unicode not recognized, tried delta
#https://stackoverflow.com/questions/63010394/superscript-in-axis-labels-in-ggplot2-for-ions
#produces Error in `map()`: ℹ In index: 5. ℹ With name: ylab. Caused by error in `.f()`: ! argument "user_env" is missing, with no default
ggqqplot(wdata, 
         x = "weight",
         title = NULL,
         xlab = NULL,
         ylab = bquote("Sample"~delta^{13}*"C Values"))


# tried expression with ^ for superscript -> produces error unexpected '^'
#(https://stackoverflow.com/questions/69212413/super-script-within-axis-label-using-ggplot2-or-ggpubr)
ggqqplot(wdata, 
         x = "weight",
         title = NULL,
         xlab = NULL,
         ylab = expression("Sample \u03B4"*^13*"C Values")
)

#tried just getting plain and italic text
#https://stackoverflow.com/questions/72538196/r-ggpubr-ggplot2-use-italics-for-one-factor-level
#produces Error in `map()`: ℹ In index: 5. ℹ With name: ylab. Caused by error in `.f()`: ! argument "user_env" is missing, with no default

ggqqplot(wdata, 
         x = "weight",
         title = NULL,
         xlab = NULL,
         ylab = expression(plain("Sample")~italic("\u03B4")~plain("13C Values"))
)

#tried using bquote and ylab as vector
#https://stackoverflow.com/questions/4973898/combining-paste-and-expression-functions-in-plot-labels
#produces Error in `map()`: ℹ In index: 5. ℹ With name: ylab. Caused by error in `.f()`: ! argument "user_env" is missing, with no default

ylab <- bquote("Sample"~delta^13*"C Values")
ggqqplot(wdata, 
         x = "weight",
         title = NULL,
         xlab = NULL,
         ylab = ylab
)
stefan
  • 90,330
  • 6
  • 25
  • 51
Adrienne
  • 23
  • 4

3 Answers3

3

Instead of using the ylab argument you could add your y axis label with ggplot2::labs():

library(ggpubr)

ggqqplot(wdata,
  x = "weight",
  title = NULL,
  xlab = NULL
) +
  labs(y = bquote("Sample"~delta^{13}*"C Values"))

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • 1
    Such a simple solution for something I struggled with for an unreasonable amount of time. I greatly appreciate it! – Adrienne Aug 08 '23 at 21:46
3

stefan beat me by 1 minute.
I post my answer because it uses expression, not bquote. But the main trick is the same, create the plot without ylab, then add the y-axis label to it.

# Create some data
set.seed(1234)
wdata = data.frame(
  sex = factor(rep(c("F", "M"), each=200)),
  weight = c(rnorm(200, 55), rnorm(200, 58)))

library(ggpubr)
#> Loading required package: ggplot2

g <- ggqqplot(wdata, 
              x = "weight",
              title = NULL,
              xlab = NULL)

g + labs(y = expression(Sample~delta^{13}*C~Values))

Created on 2023-08-08 with reprex v2.0.2

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
0

I just had the same problem, thank you so much, I had the same problem after an update to R and ggpubr. But I don't understand why the +labs code works, but not directly including it? It used to work..

JacquieS
  • 43
  • 3