英文:
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("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
)
答案1
得分: 3
你可以使用ggplot2::labs()
函数来添加y轴标签,而不是使用ylab
参数:
library(ggpubr)
ggqqplot(wdata,
x = "weight",
title = NULL,
xlab = NULL
) +
labs(y = bquote("Sample"~delta^{13}* "C Values"))
英文:
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"))
答案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)
#> 加载需要的包: ggplot2
g <- ggqqplot(wdata,
x = "weight",
title = NULL,
xlab = NULL)
g + labs(y = expression(Sample~delta^{13}*C~Values))
<!-- -->
使用 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("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))
<!-- -->
<sup>Created on 2023-08-08 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论