Coder Social home page Coder Social logo

Comments (4)

alesssia avatar alesssia commented on July 3, 2024 1

Hi @csdaw,
this is brilliant, thank you so much!

from ggprism.

sbaross avatar sbaross commented on July 3, 2024 1

Just to mention, if you replace *x* in the example expression with %*% you'll get a proper multiplication symbol × instead of a lowercase x.

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

from ggprism.

csdaw avatar csdaw commented on July 3, 2024

Hi @alesssia ,

Thanks for the detailed issue, it really helps!

As far as I know add_pvalue() can't do this, but it should be simple to add something similar which will achieve the result you're looking for without relying on latex2exp. I can give add_pvalue() a parse argument like in ggplot2::geom_text() which will be able to parse a plotmath expression.

See this simple example below using geom_text():

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()

## add a character column to stat.test with the plotmath expression 
## you want to use
stat.test$p.exprs <- paste0("P==1*x*10^", round(log10(stat.test$p.adj), 0))

## or use this rather ugly expression if you want everything italicised
# stat.test$p.exprs <- lapply(
#   paste(round(log10(stat.test$p.adj), 0)), 
#   function(x) bquote(italic("P = 1x10"^.(x)))
# )

p + geom_text(aes(label = p.exprs, x = xmax, y = y.position), 
              data = stat.test, parse = TRUE)

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

from ggprism.

csdaw avatar csdaw commented on July 3, 2024

Hi @alesssia,

I've updated add_pvalue() to have a parse argument like geom_text(). You can install the Github version of ggprism to try it out.

Sorry I'm unfamiliar with latex2exp but from what I can tell it generates plotmath expression which I am a bit more familiar with. Now you can add a column to the p-value table with plotmath expressions to use to make prettier labels.

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)

from ggprism.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.