aphalo / ggpmisc

R package ggpmisc is an extension to ggplot2 and the Grammar of Graphics
https://docs.r4photobiology.info/ggpmisc
94 stars 6 forks source link

stat_poly_eq() with weights fails for a specific example #53

Closed markbneal closed 4 months ago

markbneal commented 5 months ago

Hi Pedro,

I think this is a bug. I use examples largely copied from your help file for stat_poly_eq(). My problem is that referencing the weight in stat_poly_eq() in the final plot leads to an error. I need to reference it there because i want to compare with and without weights on the same plot. I haven't though of an easy workaround either.

# Test weights with ggpmisc

library(ggplot2)
library(ggpmisc)

# generate artificial data
set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
y <- y / max(y)
my.data <- data.frame(x = x, y = y,
                      group = c("A", "B"),
                      y2 = y * c(1, 2) + c(0, 0.1),
                      w = sqrt(x))

# give a name to a formula
formula <- y ~ poly(x, 1, raw = TRUE)

# no weights
ggplot(my.data, aes(x, y)) +
  geom_point() +
  stat_poly_line(formula = formula) +
  stat_poly_eq(formula = formula, use_label(c("eq", "R2", "P" )))

# using weights
ggplot(my.data, aes(x, y, weight = w)) +
  geom_point() +
  stat_poly_line(formula = formula) +
  stat_poly_eq(formula = formula, use_label(c("eq", "R2", "P" )))

# combined
ggplot(my.data, aes(x, y)) +
  geom_point() +
  stat_poly_line(formula = formula, colour = "darkgreen") +
  stat_poly_eq(formula = formula, use_label(c("eq", "R2", "P" )), colour = "darkgreen")+
  stat_poly_line(formula = formula, aes(weight = w), colour = "darkred") +
  stat_poly_eq(formula = formula, mapping = aes(weight = w), 
               use_label(c("eq", "R2", "P" )), colour = "darkred", label.y = 0.8)

Error: Error in fortify(): ! data must be a , or an object coercible by fortify(), or a valid -like object coercible by as.data.frame(), not a object. ℹ Did you accidentally pass aes() to the data argument? Run rlang::last_trace() to see where the error occurred.

aphalo commented 5 months ago

@markbneal

Hi Mark, use_label() is a wrapper on aes(), so it must be passed as argument to mapping, when another argument is passed to mapping the call to use_label() is interpreted as argument passed to data. To make the code work you need to pass the additional mappings to use_label() through parameter other.mapping. I need to improve the documenttaion about this.

# Test weights with ggpmisc

library(ggplot2)
library(ggpmisc)
#> Loading required package: ggpp
#> Registered S3 methods overwritten by 'ggpp':
#>   method                  from   
#>   heightDetails.titleGrob ggplot2
#>   widthDetails.titleGrob  ggplot2
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate
#> Registered S3 method overwritten by 'ggpmisc':
#>   method                  from   
#>   as.character.polynomial polynom

# generate artificial data
set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
y <- y / max(y)
my.data <- data.frame(x = x, y = y,
                      group = c("A", "B"),
                      y2 = y * c(1, 2) + c(0, 0.1),
                      w = sqrt(x))

# give a name to a formula
formula <- y ~ poly(x, 1, raw = TRUE)

# combined
ggplot(my.data, aes(x, y)) +
  geom_point() +
  stat_poly_line(formula = formula, colour = "darkgreen") +
  stat_poly_eq(formula = formula, use_label(c("eq", "R2", "P" )), 
               colour = "darkgreen" ) +
  stat_poly_line(formula = formula, mapping = aes(weight = w), 
                 colour = "darkred") +
  stat_poly_eq(formula = formula, 
               mapping = use_label(c("eq", "R2", "P" ),
                                   other.mapping = aes(weight = w)),
               colour = "darkred", label.y = 0.8)

Created on 2024-04-09 with reprex v2.1.0

markbneal commented 5 months ago

Thanks Pedro, that works nicely.

I would remove the "bug" tag since it only a documentation issue, but not obvious to me how to do that!

aphalo commented 4 months ago

Documentation clarified and comment added to the example of combined mappings.