 edited description
Hi, I want to add the pvlaue of correlation test by spearman correlation. And your example code is such a case.
#here is your example code and it works well library(ggplot2) # Correlation example x < c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1) y < c( 2.6, 3.1, 2.5, 5.0, 3.6, 4.0, 5.2, 2.8, 3.8) my.df < data.frame(x, y) # estimated correlation is in column "estimate"! broom::glance(cor.test(x, y)) ggplot(my.df, aes(x, y)) + geom_point() + stat_fit_glance(geom = "text", method = "cor.test", method.args = list(x = x, y = y, method = "spearman"), aes(label = sprintf('r[s]~"="~%.2f~~italic(P)~"="~%.2f', stat(estimate), stat(p.value))), parse = TRUE)
However, I find the method.args can not pass the true value from data frame my.df,whereas pass x and y from environment. if I delete x and y.
#rm x and y from environment rm(x,y) #same code as above, but an error ggplot(my.df, aes(x, y)) + geom_point() + stat_fit_glance(geom = "text", method = "cor.test", method.args = list(x = x, y = y, method = "spearman"), aes(label = sprintf('r[s]~"="~%.2f~~italic(P)~"="~%.2f', stat(estimate), stat(p.value))), parse = TRUE) #Error in ggplot2::layer(stat = StatFitGlance, data = data, mapping = mapping, : # object 'x' not found
if we change the method.args into formula input. There is remaining a warning and can not show pvalue correctly.
# there is still no pvalue in the plot ggplot(my.df, aes(x, y)) + geom_point() + stat_fit_glance(geom = "text", method = "cor.test", method.args = list(formula= '~x+y' ), aes(label = sprintf('r[s]~"="~%.2f~~italic(P)~"="~%.2f', ..estimate.., ..p.value..)), parse = TRUE) #Warning message: #Computation failed in `stat_fit_glance()`: #argument "x" is missing, with no default.
However, It works well if we input method="lm" and keep others identically.
# it works well. ggplot(my.df, aes(x, y)) + geom_point() + stat_fit_glance(geom = "text", method = "lm", method.args = list(formula= 'y~x' ), aes(x, y ,label = sprintf('r[s]~"="~%.2f~~italic(P)~"="~%.2f', stat(AIC), stat(p.value))), parse = TRUE, inherit.aes=F)
In summary.
I found for nearly same code, method="lm" works well whereas method="cor.test" can not pass the x and y value from data frame my.dt. It is very strange.
Thank for your suggestions.
Comments (6)

reporter 
repo owner Thanks for reporting this problem. I thought I had fixed it, but seems to persist. Be aware that in the current version only the formula interface to methods is supported as this restriction seemed to take care of some of the name scoping issues. I will try to get this resolved for the next release of 'ggpmisc'.

repo owner 
assigned issue to
 marked as critical

assigned issue to

repo owner I have changed the stat to only support the formula interface as this is the only way to respect the grammar of graphics. Supporting cor.test seems to require this function it to be treated as a special case. I may implement this in version 0.3.2, but will not make it to 0.3.1.

repo owner  marked as major

repo owner Hasn’t made it to 0.3.2 either. Because of bug fixes submission to CRAN cannot be delayed. I need to have a look at handling of onesided model formulas as used by
cor.test()
.  Log in to comment