csdaw / ggprism

ggplot2 extension inspired by GraphPad Prism
https://csdaw.github.io/ggprism/
170 stars 21 forks source link

Parse labels as expressions #11

Closed csdaw closed 3 years ago

csdaw commented 3 years ago

This PR addresses the label text formatting feature request raised in #10.

Now the text in the label column can be parsed as a plotmath expression, as in geom_text().

See an example below:

library(magrittr)
library(ggplot2)
library(ggprism)
# also use rstatix

p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
  geom_boxplot(aes(fill = dose), colour = "black") + 
  theme_prism() + 
  theme(legend.position = "none")

stat.test <- rstatix::t_test(ToothGrowth, len ~ dose, ref.group = "0.5") %>% 
  rstatix::add_xy_position()

stat.test$p.exprs <- paste0("P==1*x*10^", round(log10(stat.test$p.adj), 0))

p + add_pvalue(stat.test, label = "p.exprs", parse = TRUE)

Created on 2021-06-17 by the reprex package (v1.0.0)