在ggqqplot(ggpubr/ggpar)轴上添加斜体delta和上标。

huangapple go评论115阅读模式
英文:

Adding italic delta and superscript in ggqqplot (ggpubr/ggpar) axis

问题

我正在制作我的数据的 qq 图,并找到了 ggqqplot 这个函数,它可以添加线条和置信区间。我的数据是稳定同位素数据,我希望 y 轴标签显示为:样本 δ<sup>13</sup>C 值。

由于只涉及标签,数据集并不重要 - 从网上找到的

# 创建一些数据
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)

# 没有斜体和上标
ggqqplot(wdata, 
         x = "weight",
         title = NULL,
         xlab = NULL,
         ylab = "样本 δ13C 值"
)

# 尝试使用 bquote 和 ^ 添加上标 - unicode 未被识别,尝试使用 delta
# https://stackoverflow.com/questions/63010394/superscript-in-axis-labels-in-ggplot2-for-ions
# 产生错误: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("样本"~delta^{13}*"C 值")
)


# 尝试使用 expression 和 ^ 添加上标 -> 产生错误: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("样本 δ"*^13*"C 值")
)

# 尝试获取普通和斜体文本
# https://stackoverflow.com/questions/72538196/r-ggpubr-ggplot2-use-italics-for-one-factor-level
# 产生错误: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("样本")~italic("δ")~plain("13C 值"))
)

# 尝试使用 bquote 和 ylab 作为向量
# https://stackoverflow.com/questions/4973898/combining-paste-and-expression-functions-in-plot-labels
# 产生错误:Error in `map()`: ℹ In index: 5. ℹ With name: ylab. Caused by error in `.f()`: ! argument "user_env" is missing, with no default
ylab <- bquote("样本"~delta^13*"C 值")
ggqqplot(wdata, 
         x = "weight",
         title = NULL,
         xlab = NULL,
         ylab = ylab
)

希望这些信息对你有帮助!

英文:

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 δ<sup>13</sup>C 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(&quot;F&quot;, &quot;M&quot;), each=200)),
weight = c(rnorm(200, 55), rnorm(200, 58)))
head(wdata, 4)
# no italics no superscript
ggqqplot(wdata, 
x = &quot;weight&quot;,
title = NULL,
xlab = NULL,
ylab = &quot;Sample \u03B413C Values&quot;
)
#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 &quot;user_env&quot; is missing, with no default
ggqqplot(wdata, 
x = &quot;weight&quot;,
title = NULL,
xlab = NULL,
ylab = bquote(&quot;Sample&quot;~delta^{13}*&quot;C Values&quot;))
# tried expression with ^ for superscript -&gt; produces error unexpected &#39;^&#39;
#(https://stackoverflow.com/questions/69212413/super-script-within-axis-label-using-ggplot2-or-ggpubr)
ggqqplot(wdata, 
x = &quot;weight&quot;,
title = NULL,
xlab = NULL,
ylab = expression(&quot;Sample \u03B4&quot;*^13*&quot;C Values&quot;)
)
#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 &quot;user_env&quot; is missing, with no default
ggqqplot(wdata, 
x = &quot;weight&quot;,
title = NULL,
xlab = NULL,
ylab = expression(plain(&quot;Sample&quot;)~italic(&quot;\u03B4&quot;)~plain(&quot;13C Values&quot;))
)
#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 &quot;user_env&quot; is missing, with no default
ylab &lt;- bquote(&quot;Sample&quot;~delta^13*&quot;C Values&quot;)
ggqqplot(wdata, 
x = &quot;weight&quot;,
title = NULL,
xlab = NULL,
ylab = ylab
)

答案1

得分: 3

你可以使用ggplot2::labs()函数来添加y轴标签,而不是使用ylab参数:

library(ggpubr)

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

在ggqqplot(ggpubr/ggpar)轴上添加斜体delta和上标。

英文:

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

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

在ggqqplot(ggpubr/ggpar)轴上添加斜体delta和上标。

答案2

得分: 3

stefan比我快了1分钟。
我发布我的答案,因为它使用了expression而不是bquote。但是主要的技巧是相同的,先创建没有ylab的图,然后添加y轴标签。

# 创建一些数据
set.seed(1234)
wdata = data.frame(
  sex = factor(rep(c("F", "M"), each=200)),
  weight = c(rnorm(200, 55), rnorm(200, 58)))

library(ggpubr)
#&gt; 加载需要的包: ggplot2

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

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

在ggqqplot(ggpubr/ggpar)轴上添加斜体delta和上标。<!-- -->

使用 reprex v2.0.2 在2023-08-08创建

英文:

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(&quot;F&quot;, &quot;M&quot;), each=200)),
  weight = c(rnorm(200, 55), rnorm(200, 58)))

library(ggpubr)
#&gt; Loading required package: ggplot2

g &lt;- ggqqplot(wdata, 
              x = &quot;weight&quot;,
              title = NULL,
              xlab = NULL)

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

在ggqqplot(ggpubr/ggpar)轴上添加斜体delta和上标。<!-- -->

<sup>Created on 2023-08-08 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年8月9日 04:24:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定