Closed markbneal closed 4 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
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!
Documentation clarified and comment added to the example of combined mappings.
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.
Error: Error in, or an object coercible by -like object coercible by
object.
ℹ Did you accidentally pass
fortify()
: !data
must be afortify()
, or a validas.data.frame()
, not aaes()
to thedata
argument? Runrlang::last_trace()
to see where the error occurred.