
assigned issue to
ggplot(mtcars,aes(disp,mpg,size=cyl))+ geom_point() + stat_smooth(aes(col="no weights"), geom="line", method='lm',formula=y~x,se=F) + stat_smooth(aes(weight=!!as.symbol("cyl"),col="weights"), geom="line",method='lm',formula=y~x,se=F) + stat_fit_tidy(method = "lm", method.args = list(formula = y ~ x), mapping = aes(label = sprintf("Slope = %.3g\npvalue = %.3g", stat(x_estimate), stat(x_p.value)))) + stat_fit_tidy(method = "lm",label.y ="bottom", method.args = list(formula = y ~ x), mapping = aes(weight=!!as.symbol("cyl"),label = sprintf("Slope = %.3g\npvalue = %.3g", stat(x_estimate), stat(x_p.value))))
summary(lm(data=mtcars, mpg ~disp)) summary(lm(data=mtcars, mpg ~disp, weights =cyl))
Comments (10)

repo owner 
sorry I am new to Bitbucket here is another attempt to paste the code: and the figure
I show that the lines are not the same with weights applied yet the stat_fit_tidy ignore the weights and give the same paramters
library(ggplot2) library(ggpmisc) ggplot(mtcars,aes(disp,mpg,size=cyl))+ geom_point() + stat_smooth(aes(col="no weights"), geom="line", method='lm',formula=y~x,se=F,size = 2)+ stat_smooth(aes(weight=cyl,col="weights"), geom="line",method='lm',formula=y~x,se=F,size = 2) + # lines are different stat_fit_tidy(method = "lm", method.args = list(formula = y ~ x), size=6, mapping = aes(col="no weights", label = sprintf("Slope = %.3g\npvalue = %.3g", stat(x_estimate), stat(x_p.value)))) + stat_fit_tidy(method = "lm",label.y ="bottom", method.args = list(formula = y ~ x), size=6, mapping = aes(col="weights",weight=cyl, label = sprintf("Slope = %.3g\npvalue = %.3g", stat(x_estimate), stat(x_p.value))))

repo owner Thanks! No problem. This looks very much like a bug in my package. It may take me some days to fix this problem as I am quite busy at the moment. Sorry!

repo owner Hi! I checked the current definition of the different stats, The only one that supports the
weight
aesthetic isstat_poly_eq()
. But this stat at the moment returns readymade text labels out of which it is not easy to extract the slope. 
repo owner The most recent commit is an update to
stat_poly_eq()
that supports optionally the return of numeric values from the fit. I added also an example (the last one) to the help page of this stat with an example that should help as a basis to add the labels you wish. In a future version I will try to enhancestat_fit_tidy()
to support weights.To install this updated version from Bitbucket you can use:
devtools::install_bitbucket("aphalo/ggpmisc")

repo owner  marked as enhancement
Changed to enhancement as the updated
stat_poly_eq()
can be used to create the desired plot. The enhancement could be designed to work with other fit function parameters or even designed to pass additional variables to the fit function (e.g. to allow use of covariates that are not mapped to aesthetics). I think what would be the most general approach is to allow passing other “dummy” aesthetics in addition to “weight”. 
repo owner Found a solution that works with the current version of ‘ggspectra’.
library(ggplot2) library(ggpmisc) ggplot(mtcars,aes(disp,mpg,size=cyl))+ geom_point() + stat_smooth(aes(col="no weights"), geom="line", method='lm',formula=y~x,se=F,size = 2)+ stat_smooth(aes(weight=cyl,col="weights"), geom="line",method='lm',formula=y~x,se=F,size = 2) + # lines are different stat_fit_tidy(method = "lm", method.args = list(formula = y ~ x), size=6, mapping = aes(col="no weights", label = sprintf("Slope = %.3g\npvalue = %.3g", stat(x_estimate), stat(x_p.value)))) + stat_fit_tidy(method = "lm",label.y ="bottom", method.args = list(formula = y ~ x, weights = quote(weight)), size=6, mapping = aes(col="weights",weight=cyl, label = sprintf("Slope = %.3g\npvalue = %.3g", stat(x_estimate), stat(x_p.value))))
One needs to add
weights = quote(weight)
to the list passed as argument tomethos.args
. One can use an aesthetic that is known to ggplot2 and expects a numeric variable to be mapped to it. In this case bothweight
andz
can be used, but not an arbitrarily named aesthetic.

repo owner @Samer MOUKSASSI Thanks for raising this point! I will add an example to the documentation.

repo owner  changed status to resolved
Added examples of how to achieve this to documentation. No change to code seems necessary, although easy to implement, hardcoding the weight aesthetic to the weights parameter of the fit method could potentially interfere with the use of this aesthetic with some fit methods.

Thank you this is awesome. I tried to use it the same use I used stat smooth i.e mapping a weight aesthetic in
weight=cyl
( no need to pass to method.args) it is automatic:
https://github.com/tidyverse/ggplot2/blob/master/R/statsmooth.r#L146
good to know that user cannot expect stat_fit_tidy to work the same way and that we need the additional way you showed.Thanks again for the great package and swift fixes.
 Log in to comment
The code example is scrambled with HTML scapes. Needs cleaning before further investigation.