I would like to use the expression functionality within ggpubr to have both the ability to italicise my APA test statistics within text of a between groups comparison using geom_bracket
within ggpubr.
My problem is that I need to remove the leading 0 that is produced, but this appears to be rendered not within the string (hence the second example where I show gsub()
can't just pull it out.
library(ggpubr)
library(ggplot2)
# Example close to what I currently have in code with leading 0 in-front of p value
ggboxplot(df, x = "dose", y = "len")
geom_bracket(
xmin = "0.5", xmax = "1", y.position = 30,
label = "list(~italic(t)(666)==6.66,~italic(p) < .05)",
type = "expression",
tip.length = c(0.2, 0.02)
)
# what i thought would work to use `gsub()` to remove what I don't want
ggboxplot(df, x = "dose", y = "len")
geom_bracket(
xmin = "0.5", xmax = "1", y.position = 30,
label = gsub(pattern = "p","q", "list(~italic(t)(666)==6.66,~italic(p) < .05)"),
type = "expression",
tip.length = c(0.2, 0.02)
)
# but if you remove the zeros, they only come out of the text, not the leading 0 that is added
ggboxplot(df, x = "dose", y = "len")
geom_bracket(
xmin = "0.5", xmax = "1", y.position = 30,
label = gsub(pattern = "0", "", "list(~italic(t)(666)==6.66,~italic(p) < .05)"),
type = "expression",
tip.length = c(0.2, 0.02)
)
Is there a way to either:
- Have the ability to write both numbers, letters, and italics within an expression? I have tried this with using the "text" argument then using
italics()
but have not been able to get this to work. Could just be doing something silly after getting very frustrated with it. - Remove the leading 0 that is inserted using plotmath?
Either way, would be interested to know why the 0 is inserted anyway.
CodePudding user response:
In plotmath expressions, the R parser converts any numbers found into actual numbers and will print them as actual numbers, leading zeros and all. If you want to avoid the leading 0, put .05
in single quotes to identify it as a character string:
ggboxplot(df, x = "dose", y = "len")
geom_bracket(
xmin = "0.5", xmax = "1", y.position = 30,
label = "list(~italic(t)(666)==6.66,~italic(p) < '.05')",
type = "expression",
tip.length = c(0.2, 0.02)
)
Data used
In the absence of reproducible data, I used the following sample data set, which has the relevant features that could be inferred from the question:
set.seed(2)
df <- data.frame(dose = rep(c("0.5", "1"), each = 20),
len = rnorm(40, rep(c(17.5, 20.5), each = 20), 3.5))