gpt4 book ai didi

r - S3 方法 : Extending ggplot2 `+.gg` function

转载 作者:行者123 更新时间:2023-12-02 18:59:09 26 4
gpt4 key购买 nike

我正在尝试使用一个新类来扩展 ggplot2,我们将在本例中将其称为 foo。目标是编写一个 +.foo 方法来代替 +.gg。但是我遇到了“不兼容方法”的问题

设置

目前我可以编写 ggplot_add.foo_layer ,它会将 plot 放入我的 foo 类中,然后正常添加相应的图层。

这个想法是,一旦 plot 对象继承了 foo,它将在添加下一层时分派(dispatch)到 +.foo

我想这样做的原因是因为我想检查 foo 对象的结构是否仍然有效/与传入层兼容。这将使我不必为 ggplot_build 编写方法。

代码定义

library(ggplot2)

`+.foo` <- function(e1, e2){
cat("Using foo ggplot +") # for Debugging
NextMethod() #ideally just dispatches to `+.gg`
}

ggplot_add.foo_layer <- function(object, plot, object_name) {
plot <- as_foo(plot)
ggplot2:::add_ggplot(plot, object$layer, object_name)
}

as_foo <- function(x){
if(!is_foo(x)){
class(x) <- c("foo", class(x))
}
x
}

is_foo <- function(x) inherits(x, "foo")

foo_layer <- function(x) structure(list(layer = x), class = "foo_layer")

错误

p1 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species)) +
geom_point()
class(p1)
#[1] "gg" "ggplot"
p1 + geom_density(aes(y = after_stat(density)))


p2 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species)) +
foo_layer(geom_point())

class(p2)
#[1] "foo" "gg" "ggplot"
p2 + geom_density(aes(y = after_stat(density)))
#Error in p2 + geom_density(aes(y = after_stat(density))) :
# non-numeric argument to binary operator
#In addition: Warning message:
#Incompatible methods ("+.foo", "+.gg") for "+"

从上面的代码 p1 + geom_* 执行得很好。但是 p2 + geom_* 由于上述关于 Incompatible methods 的错误而无法进行。根据我对 S3 方法调度的了解,我不明白为什么这行不通。谁能解释这是为什么,或者我该如何补救。

理想情况下,我不必编写方法 ggplot_build.foo,因为我希望其他包的 ggplot_build 在存在时被使用(例如 gganimate).

最佳答案

您可以做的一件事是覆盖 ggplot2:::+gg 方法以支持 S3 中的双重分派(dispatch)。如果您正在编写一个程序包,这并不是很好的行为,但它可以完成工作。请注意,这种顽皮的行为并没有阻止其他包覆盖 ggplot 的函数(看着你,ggtern)。

library(ggplot2)

`+.gg` <- function(e1, e2) {
UseMethod("+.gg")
}

`+.gg.default` <- ggplot2:::`+.gg`

`+.gg.foo` <- function(e1, e2) {
cat("Using foo ggplot +")
NextMethod()
}

ggplot_add.foo_layer <- function(object, plot, object_name) {
plot <- as_foo(plot)
ggplot2:::add_ggplot(plot, object$layer, object_name)
}

as_foo <- function(x){
if(!is_foo(x)){
class(x) <- c("foo", class(x))
}
x
}

is_foo <- function(x) inherits(x, "foo")

foo_layer <- function(x) structure(list(layer = x), class = "foo_layer")

p1 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species)) +
geom_point()

p2 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species)) +
foo_layer(geom_point())


p2 + geom_density(aes(y = after_stat(density)))
#> Using foo ggplot +

reprex package 创建于 2021-01-20 (v0.3.0)

关于r - S3 方法 : Extending ggplot2 `+.gg` function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65817557/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com