ggpmisc::stat_poly_eq() does not consider weighting by a variable

Create issue
Issue #16 resolved
Pedro J. Aphalo repo owner created an issue

How can I get ggpmisc::stat_poly_eq() to take into account weighting by a variable? I don't see in the documentation a parameter to include weight as it exists in geom_smooth and if I try to force it within aes() it results in 'Warning: Ignoring unknown parameters: weight.' I am aware of other workarounds to print the correct equation as shown here but it would be so much cleaner to have it in stat_poly_eq(). Has anybody figured this out or am I missing something? Thanks everyone.

stuff<-structure(list(var = 2:31, mean = c(17026.5, 11028.6842105263, 
                                13113.1111111111, 11087.3679395386, 9863.8664212548, 10060.676012167, 
                                9378.01091924399, 9790.67922990444, 8569.95788246269, 8839.68511390887, 
                                7656.50625471556, 7370.78564257028, 7939.13425925926, 7541.83192090395, 
                                8845.67474747475, 8023.03099415205, 6373.05976190476, 6337.93259803922, 
                                6824.79901960784, 7450.80769230769, 6651.81884057971, 5548.59722222222, 
                                7802.78205128205, 3627.07407407407, 2471, 2248.33333333333, 1368.7, 
                                2104.25, 742, 2097.5), n = c(2L, 19L, 150L, 419L, 1562L, 3178L, 
                                                             3880L, 2965L, 4288L, 2780L, 2209L, 664L, 54L, 59L, 66L, 57L, 
                                                             50L, 34L, 34L, 26L, 23L, 18L, 13L, 9L, 7L, 7L, 5L, 2L, 4L, 2L
                                )), .Names = c("var", "mean", "n"), row.names = c(NA, -30L), class = "data.frame")

ggplot(data=stuff, aes(x=var, y=mean))+
geom_smooth(aes(weight=n), method='lm', formula = y~x)+
stat_poly_eq(aes(label=paste(..eq.label.., ..rr.label.., sep="~~~")),
           formula=y~x, label.x.npc=0.8, label.y.npc=0.8,
           coef.digits=3, parse=TRUE)

figure from stuff data

#currently, the equation printed in the plot does not correspond to the coefficients resulting with weigth=n

truecoeff<-lm(data=stuff, mean~var, weight=n)


Comments (8)

  1. Pedro J. Aphalo reporter

    I have implemented support for lm's weights through the weight aesthetic following the same approach as in stat_smooth and geom_smooth in 'ggplot2'. Needs testing, but will be included in the next CRAN release, in a few days' time.

  2. Jan Stanstrup

    This appears to only work if weight is in the ggplot aes. If I try to add it in the in the aes of stat_poly_eq then I get: "Ignoring unknown aesthetics: weight"

  3. Pedro J. Aphalo reporter
    • changed status to open

    Thanks for reporting this problem. I will look into it for the next release. I need to check how stat_smooth() is coded in the current version of 'ggplot2' and whether weight works as it should for stat_smooth() which I used as a model for supporting weight in 'ggpmisc'. I have reopened the issue.

  4. Pedro J. Aphalo reporter

    In fact, the mapping seems to work in spite of the warning. I haven't been able to remove the spurious warning, but as the example below shows, the code works as it should. The warning is very confusing, and needs fixing, but meanwhile figures can be still created. Here is an example based on the same example data as in the message at the top of this issue.

    ggplot(data=stuff, aes(x=var, y=mean))+
      geom_smooth(method='lm', formula = y~x, colour = "blue")+
      geom_smooth(aes(weight = n), method='lm', formula = y~x, colour = "red")+
      stat_poly_eq(aes(label=paste(..eq.label.., ..rr.label.., sep="~~~")),
                   formula=y~x, label.x.npc=0.8, label.y.npc=0.8,
                   coef.digits=3, parse=TRUE, vjust = 1.5, colour = "blue") +
      stat_poly_eq(aes(weight = n, label=paste(..eq.label.., ..rr.label.., sep="~~~")),
                   formula=y~x, label.x.npc=0.8, label.y.npc=0.8,
                   coef.digits=3, parse=TRUE, vjust = -0.5, colour = "red")


  5. Pedro J. Aphalo reporter

    Downgrade to major, as the code returns the correct plot even though it issues a spurious warning.

  6. Log in to comment