Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report p-values as <0.001 (or similar) #186

Closed
garthtarr opened this issue Jun 11, 2019 · 8 comments
Closed

Report p-values as <0.001 (or similar) #186

garthtarr opened this issue Jun 11, 2019 · 8 comments

Comments

@garthtarr
Copy link

garthtarr commented Jun 11, 2019

It would be great to have an option to report small p-values at some arbitrary threshold, e.g. "p < 0.001" or "p < 0.0001" . Looks like some efforts along this path exist in stat_cor() but it is hardcoded and at machine level precision.

I'm thinking that instead of this:

ggscatter(mtcars, x = "wt", y = "mpg", add = "reg.line") +
  stat_cor(
    aes(label = paste(..rr.label.., ..p.label.., sep = "~`,`~")),
    label.x = 3
  )

You might be able to implement something like this:

ggscatter(mtcars, x = "wt", y = "mpg", add = "reg.line") +
  stat_cor(
    aes(label = paste(..rr.label.., ..p.rounded.., sep = "~`,`~")),
    label.x = 3, accuracy = 0.001
  )

where accuracy defines how far the p-value rounding went, much like the pvalue_format() function from the scales package does. I.e.

p.vals = c(0.123, 0.015, 0.0015, 0.00015, 1e-10)
accuracy = 0.01
scales::pvalue(p.vals, accuracy = accuracy, add_p = TRUE)
# [1] "p=0.12" "p=0.02" "p<0.01" "p<0.01" "p<0.01"
accuracy = 0.001
scales::pvalue(p.vals, accuracy = accuracy, add_p = TRUE)
# [1] "p=0.123" "p=0.015" "p=0.002" "p<0.001" "p<0.001"

I note that you've used spaces around the = and < in the ggpubr package, so that could be added back in:

p.vals = c(0.123, 0.015, 0.0015, 0.00015, 1e-10)
accuracy = 0.01
pfmt = scales::pvalue(p.vals, accuracy = accuracy, add_p = TRUE)
gsub(pattern = "(=|<)", replacement = " \\1 ", x = pfmt)
# [1] "p = 0.123" "p = 0.015" "p = 0.002" "p < 0.001" "p < 0.001"
@kassambara
Copy link
Owner

good idea and thank you for the well documented examples. I will implement this as soon as possible

@kassambara
Copy link
Owner

Hi,

With the current version, you can pass any pvalue formatting function you want to stat_cor().

For example:

# Custom formatting fucntion
format_pval <- function(pval){
  pval <- scales::pvalue(pval, accuracy= 0.0001, add_p = TRUE)
  gsub(pattern = "(=|<)", replacement = " \\1 ", x = pval)
}
# Display formatted p-value
ggscatter(mtcars, x = "wt", y = "mpg", add = "reg.line") +
  stat_cor(
    aes(label = paste(..rr.label.., format_pval(..p..), sep = "~`,`~")),
    label.x = 3
  )

Rplot13

@garthtarr
Copy link
Author

Thanks!

@cigan77
Copy link

cigan77 commented Dec 28, 2019

Is it possible to avoid the space between the R2 value and comma?

@garthtarr
Copy link
Author

garthtarr commented Dec 31, 2019

You can use * in place of ~ to prevent a space being generated:

library(ggpubr)
# Custom formatting fucntion
format_pval <- function(pval){
  pval <- scales::pvalue(pval, accuracy= 0.0001, add_p = TRUE)
  gsub(pattern = "(=|<)", replacement = " \\1 ", x = pval)
}
# Display formatted p-value
ggscatter(mtcars, x = "wt", y = "mpg", add = "reg.line") +
  stat_cor(
    aes(label = paste(..rr.label.., format_pval(..p..), sep = "*`,`~")),
    label.x = 3
  )

@kassambara
Copy link
Owner

With the latest dev version, you can now specify the argument p.accuracy and r.accuracy:

suppressPackageStartupMessages(library(ggpubr))
# Default plot
sp <- ggscatter(mtcars, x = "wt", y = "mpg", add = "reg.line")
sp + stat_cor(label.x = 3)
#> `geom_smooth()` using formula 'y ~ x'

# Specify the number of decimal places of precision for 
# the p-value and the correlation coefficient r
sp + stat_cor(label.x = 3, p.accuracy = 0.001, r.accuracy = 0.01)
#> `geom_smooth()` using formula 'y ~ x'

Created on 2020-05-10 by the reprex package (v0.3.0.9001)

@johannesjuliusm
Copy link

This is fantastic, but is it true that this functionality is not available for stat_compare_means()? It would be fantastic to be able to do the same p-value formatting throughout statistical tests and procedures in ggpubr.

@dchiu911
Copy link

Any updates on adding p.accuracy to stat_compare_means()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants