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

Changing the number of significant digits shown with stat_cor #114

Closed
raedevan6 opened this issue Sep 23, 2018 · 8 comments
Closed

Changing the number of significant digits shown with stat_cor #114

raedevan6 opened this issue Sep 23, 2018 · 8 comments

Comments

@raedevan6
Copy link

Hello, forgive me if this an easy fix. I'm trying to change the number of digits shown on the plot for both R2 and pvals. Below is my code, I would like to only show 3 sig digits.

ggplot(pxsam, aes(x=cbi, y=value, color=phy)) + geom_point() +
geom_smooth(method = "lm") + theme_pubr() +
ggpubr::stat_cor(
aes(label = paste(..rr.label.., ..p.label.., sep = ",")))

image

@icer182
Copy link

icer182 commented Feb 11, 2020

Hi @raedevan6. I figured this out just by using stat_poly_eq. However, I am not sure how to independently adjust the R^2 values now. I know I can adjust them together, but I'd like for them to event out by declaring rr.digits 2 for one regression line and rr.digits = 3 for the other regression line. @kassambara

@kassambara
Copy link
Owner

Related issue: #186

@kassambara
Copy link
Owner

New arguments digits added.

Applying different digits for each regression line on the same plot is not supported by stat_cor.

I don't know how to achieve this automatically in ggplot context. I would suggest the following (manual) solution:

library(ggpubr)
library(dplyr)

df <- mtcars
paste("italic(R)^2", ..rr.., sep = "~`=`~")
#> Error in paste("italic(R)^2", ..rr.., sep = "~`=`~"): objet '..rr..' introuvable
df$cyl <- as.factor(df$cyl)

# Prepare data for each group
grp1.df <- df %>% filter(cyl == "4")
grp2.df <- df %>% filter(cyl == "6")
grp3.df <- df %>% filter(cyl == "8")

# Plot
ggplot(df, aes(wt, mpg, color = cyl)) +
  geom_point() +
  geom_smooth(method = "lm") +
  stat_cor(label.x = 3, label.y = 34, digits = 3, data = grp1.df)+
  stat_cor(label.x = 3, label.y = 32, digits = 2, data = grp2.df) +
  stat_cor(label.x = 3, label.y = 30, digits = 1, data = grp3.df)

Created on 2020-02-12 by the reprex package (v0.3.0)

@icer182
Copy link

icer182 commented May 8, 2020

Hi @kassambara. Thanks for implementing the digits variable to stat_cor. I was just curious if there was a method to keep trailing 0s? I specified digits = 2, but it deletes my trailing 0. Thank you.

@kassambara
Copy link
Owner

kassambara commented May 9, 2020

Hi,

for advanced p-value formatting, I would suggest the function scales::pvalue() using the option accuracy. Use accuracy = 0.01 to show 2 decimal places of precision.

library(ggpubr)
# Custom formatting fucntion
format_pval <- function(pval){
  pval <- scales::pvalue(pval, accuracy= 0.001, 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
  )
#> `geom_smooth()` using formula 'y ~ x'

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

@icer182
Copy link

icer182 commented May 9, 2020

Hi @kassambara. Thanks for the explanation. However, I am looking to keep a trailing 0 in the R^2 value. Is there a current method of doing this? Sorry for the confusion.

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

@courtney-bryce-hilton
Copy link

courtney-bryce-hilton commented Apr 29, 2022

Hi @kassambara. The above suggestion to including trailing zeros for the correlation coefficient by setting r.accuracy isn't working for me. I have tried both the current CRAN version of ggpubr and the dev version.

For my case, the R2 always shows as 0.6 in the plot rather than 0.60. From what I can gather, ..rr.label.. is correctly set at "0.60" but when it gets passed into the R expression format, that removes the trailing zero. Do you see any way around that?

Here's a reproducible example that manually sets a number in the label to be 0.60 to demonstrate:

sp <- ggscatter(mtcars, x = "wt", y = "mpg", add = "reg.line")
sp + stat_cor(aes(label = paste(0.60, ..p.label.., sep = "~`,`~")), label.x = 3, r.accuracy = 0.01)

Thanks

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

4 participants