简体   繁体   English

关于`rstatix`包中的`get_test_label`和`get_pwc_label`函数的问题

[英]Question about the functions `get_test_label` and `get_pwc_label` from the `rstatix` package

I have recently become familiar with the rstatix package.我最近熟悉了rstatix包。 Below is an example code using functions from this package.以下是使用此包中函数的示例代码。

library(tidyverse)
library(rstatix)
library(ggpubr)

set.seed(1111)
n=100
df = tibble(
  x1 = rnorm(n, 0, 1.1),
  x2 = rnorm(n, 0.2, .1),
  x3 = rnorm(n, -.2, .2),
  x4 = rnorm(n, 0, 2),
) %>% pivot_longer(x1:x4)
df

pwc = df %>%
  pairwise_t_test(value~name, paired = TRUE,
                  p.adjust.method = "bonferroni") %>%
  add_xy_position(x = "name") %>%
  mutate(name=group1,
         lab = paste(p, " - ", p.adj.signif))
res.test = df %>% anova_test(value~name)

df %>% ggplot(aes(name, value))+
  geom_boxplot(alpha=0.6)+
  stat_pvalue_manual(pwc, step.increase=0.05, label = "lab")+
  labs(title = get_test_label(res.test, detailed = TRUE),
       subtitle = get_pwc_label(pwc))

However, I noticed that functions like get_test_label or get_pwc_label do not return text, but commands to prepare the text.但是,我注意到像get_test_labelget_pwc_label这样的函数不返回文本,而是准备文本的命令。

For example, calling get_test_label(res.test, detailed = TRUE) gives this:例如,调用get_test_label(res.test, detailed = TRUE)给出了这个:

paste("Anova, ", italic("F"), "(3,396)", " = ", 
    "5.26, ", italic("p"), " = ", "0.001", 
    paste(", ", eta["g"]^2, " = ", 0.04), "")

In turn, calling get_pwc_label(pwc) will result in:反过来,调用get_pwc_label(pwc)将导致:

paste("pwc: ", bold(c(t_test = "T test")), "; p.adjust: ", 
    bold("Bonferroni"))

Now my question, basically two questions.现在我的问题,基本上是两个问题。

  1. What could be the reason these functions do not return text but commands?这些函数不返回文本而是返回命令的原因是什么?
  2. How to make your own function that returns similar commands.如何制作自己的返回类似命令的函数。

R's graphics devices need a syntax for distinguishing between literal and formatted text. R 的图形设备需要一种语法来区分文字和格式化文本。 The syntax that they use is defined in ?plotmath .他们使用的语法在?plotmath定义。 A basic feature of this syntax is that strings define literal text while functions define operations on text, such as juxtaposition and formatting (changing fonts, adding diacritics, getting whitespace right in mathematical formulae, etc.).这种语法的一个基本特征是字符串定义文字文本,而函数定义文本操作,例如并列和格式化(更改字体、添加变音符号、在数学公式中正确使用空格等)。 What you are seeing, then, are unevaluated compositions of functions and strings defining formatted text.那么,您所看到的是定义格式化文本的函数和字符串的未评估组合。 These are usually called plotmath expressions.这些通常称为plotmath表达式。

I could provide you with a basic example showing how different expressions translate to graphical output, but there is an excellent demo built into R: just run demo("plotmath") in an interactive R session and follow the prompts.我可以为您提供一个基本示例,展示不同的表达式如何转换为图形输出,但是 R 内置了一个出色的演示:只需在交互式 R 会话中运行demo("plotmath")并按照提示进行操作。

Functions that you can use to create singular expressions like the ones you are seeing include quote , substitute , bquote , and str2lang .可用于创建单数表达式(如您所见)的函数包括quotesubstitutebquotestr2lang You would probably use one of these to format, eg, a plot title.您可能会使用其中之一来格式化,例如,情节标题。 Here is how I would use each to generate the plotmath expression paste("Michaelis constant: ", italic("K")["M"], " = ", 0.015) .这是我将如何使用每个来生成plotmath表达式paste("Michaelis constant: ", italic("K")["M"], " = ", 0.015) Note the subtle differences:注意细微的差别:

quote(
  paste("Michaelis constant: ", italic("K")["M"], " = ", 0.015)
)

substitute(
  paste("Michaelis constant: ", italic(sym)[sub], " = ", val),
  list(sym = "K", sub = "M", val = 0.015)
)

sym <- "K"
sub <- "M"
val <- 0.015
bquote(
  paste("Michaelis constant: ", italic(.(sym))[.(sub)], " = ", .(val))
)

str2lang(
  'paste("Michaelis constant: ", italic("K")["M"], " = ", 0.015)'
)

Functions that you can use to create expression vectors include expression and str2expression .可用于创建表达式向量的函数包括expressionstr2expression You would use one of these to format one or more labels at once, eg, when defining axis tick labels.您可以使用其中之一同时设置一个或多个标签的格式,例如,在定义轴刻度标签时。 Here is how I would use them to format labels for ticks at increasing powers of 10:以下是我将如何使用它们以 10 的递增幂格式化刻度标签:

expression(10^0, 10^1, 10^2, 10^3, 10^4, 10^5)
str2expression(paste0(10, "^", 0:5))

Here is a fun plot putting stuff together:这是一个有趣的情节,把东西放在一起:

plot.new()
plot.window(c(0, 1), c(1, 100000), log = "y")
box()
title(main = quote(paste("Michaelis constant: ", italic("K")["M"], " = ", 0.015)))
axis(side = 2, at = 10^(0:5), labels = str2expression(paste0(10, "^", 0:5)), las = 1)
text(x = seq(0, 1, by = 0.2), y = 10^(0:5), labels = str2expression(sprintf('%s("%s")', c("plain", "bold", "italic", "bolditalic", "symbol", "underline"), month.abb[1:6])))

For more examples, take a look at ?plotmath .有关更多示例,请查看?plotmath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM