Home > Mobile >  Remove the equal sign and set significant figures with stat_poly_eq?
Remove the equal sign and set significant figures with stat_poly_eq?

Time:12-30

I am using the function stat_poly_eq to display the R-squared and p-value of a linear regression within a ggplot. I have two questions to optimize my output:

  1. How do I remove the equal sign (=) from the p-value, such that only these less than sign (<) remains?
  2. How can I set a desired number of significant digits to display? For example, I would like to see 3 significant digits for both the R-squared and p-value.

Here's some reproducible code to show the issue:

data(mtcars)

ggplot(data=mtcars, aes(x=mpg,y=hp))  
  geom_point()  
  geom_smooth(method = "lm",formula = y ~ x,se=TRUE, color="black")  
  stat_poly_eq(formula = y ~ x, 
               aes(label = paste(..rr.label.., ..p.value.label.., sep = "*`,`~")), 
               parse = TRUE,label.x.npc = "right",size=8)

enter image description here

CodePudding user response:

The number of digits is easy to control with the argument rr.digits. I can't replicate your problem with the equals sign, but if you update ggplot and ggpmisc and use the modern after_stat syntax rather than the depricated .. syntax, you should get the same result as demonstrated in this reprex:

library(ggplot2)
library(ggpmisc)
#> Loading required package: ggpp
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate

ggplot(mtcars, aes(mpg, hp))  
  geom_point()  
  geom_smooth(method = "lm", formula = y ~ x, color = "black")  
  stat_poly_eq(formula = y ~ x, 
               aes(label = paste(after_stat(rr.label),  "*`,`~", 
                                 after_stat(p.value.label))), 
               parse = TRUE, label.x.npc = "right", size = 8, rr.digits = 3)

Created on 2022-12-23 with reprex v2.0.2

  • Related