stat_fit_glance does not apply the weight aesthetic

Create issue
Issue #28 resolved
Former user created an issue

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\np-value = %.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\np-value = %.3g", stat(x_estimate), stat(x_p.value))))

summary(lm(data=mtcars, mpg ~disp)) summary(lm(data=mtcars, mpg ~disp, weights =cyl))

Comments (10)

  1. Samer MOUKSASSI

    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\np-value = %.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\np-value = %.3g", 
                                                  stat(x_estimate), stat(x_p.value))))
    

  2. Pedro J. Aphalo 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!

  3. Pedro J. Aphalo repo owner

    Hi! I checked the current definition of the different stats, The only one that supports the weight aesthetic is stat_poly_eq() . But this stat at the moment returns ready-made text labels out of which it is not easy to extract the slope.

  4. Pedro J. Aphalo 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 enhance stat_fit_tidy() to support weights.

    To install this updated version from Bitbucket you can use:

    devtools::install_bitbucket("aphalo/ggpmisc")
    

  5. Pedro J. Aphalo repo owner

    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”.

  6. Pedro J. Aphalo 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\np-value = %.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\np-value = %.3g", 
                                                  stat(x_estimate), stat(x_p.value))))
    

    One needs to add weights = quote(weight) to the list passed as argument to methos.args. One can use an aesthetic that is known to ggplot2 and expects a numeric variable to be mapped to it. In this case both weight and z can be used, but not an arbitrarily named aesthetic.

  7. Pedro J. Aphalo repo owner

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

  8. Pedro J. Aphalo repo owner

    Added examples of how to achieve this to documentation. No change to code seems necessary, although easy to implement, hard-coding the weight aesthetic to the weights parameter of the fit method could potentially interfere with the use of this aesthetic with some fit methods.

  9. Log in to comment