I’ve searched the entire internet for a solution, but none of the posts seemed to cover my exact problem. Sorry if I missed the answer somewhere.
I’m trying to create to add a bracket with a p-value and an asterisk indicating significance to a plot. Since the p-value is coming from a model, it’s stored in a variable which I want to use. The p should be in italics, and I would like to have an asterisk at the end. So the result should be: p < .02*
The part with the p-value in italics is easy:
library(ggplot2)
library(ggpubr)
df <- iris %>% mutate(Species = factor(Species))
pval <- .02
ggboxplot(df, x = "Species", y = "Sepal.Length", color = "Species") +
geom_bracket(
xmin = "setosa", xmax = "versicolor", y.position = 7.2,
label = paste("italic(p) <", pval), type = "expression",
tip.length = c(0.35, 0.02)
)
However, I can’t figure out how to add an asterisk. Something like this will fail:
ggboxplot(df, x = "Species", y = "Sepal.Length", color = "Species") +
geom_bracket(
xmin = "setosa", xmax = "versicolor", y.position = 7.2,
label = paste("italic(p) <", pval, "*"), type = "expression",
tip.length = c(0.35, 0.02)
)
Error in `geom_bracket()`:
! Problem while converting geom to grob.
ℹ Error occurred in the 2nd layer.
Caused by error in `parse()`:
! <text>:1:19: unexpected '*'
1: italic(p) < 0.02 ^*
For the label argument, I’ve things along the lines of:
paste("italic(p) <", pval, "^*")
bquote("~italic(p)"<.(pval)^"*")
But no luck. How can I add an asterisk at the end?
Thanks in advance!
>Solution :
As the * is a special character which used to separate tokens in plotmath you have to quote it to display the asterisk, i.e. use '*'. Additionally, to make your expression a valid plotmath expression you have to separate the asterisk via the mentioned unquoted *:
library(ggplot2)
library(ggpubr)
df <- iris %>% mutate(Species = factor(Species))
pval <- .02
ggboxplot(df, x = "Species", y = "Sepal.Length", color = "Species") +
geom_bracket(
xmin = "setosa", xmax = "versicolor", y.position = 7.2,
label = paste("italic(p) <", pval, "*'*'"), type = "expression",
tip.length = c(0.35, 0.02)
)

