- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用一个新类来扩展 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/
include.sh #!/bin/bash - export gg f() { for i in "${gg[@]}" do echo $i done } r
我的 ggplot 有问题,我想为某些值设置不同的形状。但是在图例中,所有东西都堆叠在一起,因此图例不再清晰。此外,我想从图例中删除日本或找到另一种方法将其设置为红色且形状=17。也许添加到过滤器 J
我正在使用 footable library用于显示表。每行都有一个日期列,我想按日期列(在列标题上单击)对该表进行排序。 Data Scadenza Titolo
当我尝试构建我的项目时,它返回以下错误: 错误 1 意外错误创建调试信息文件 'D:\Documents\Lance\Documents\School\Capstone\GG\GG\obj\Deb
我试图做到这一点: var curr = new Date; var first = curr.getDate() - curr.getDay(); var last = f
例如,假设我正在 Vim 中愉快地编辑文档,这时我不小心按下了 gg。然后我最终到达文档的第一行,当然我不知道我上次编辑的确切行是哪一行。有什么方法可以神奇地回到 gg 跳转之前最后编辑的行吗? 最佳
当我尝试使用 gg=G 修复 HTML 文件的缩进时,每一行都失去了缩进并变为左对齐。有人知道这里会发生什么吗? 测试.html Indent test
我正试图弄清楚如何解决这个问题。它没有打印任何内容,也没有发送 DM。 代码: async def on_dbl_vote(self, ctx, data): print("Recei
我正在尝试使用一个新类来扩展 ggplot2,我们将在本例中将其称为 foo。目标是编写一个 +.foo 方法来代替 +.gg。但是我遇到了“不兼容方法”的问题 设置 目前我可以编写 ggplot_a
全面警告:react-dom.development.js:2592 指定的值“”不符合要求的格式。格式为“#rrggbb”,其中 rr、gg、bb 是两位十六进制数。 代码运行良好。它正在做它应该做
我正在尝试使用一个新类来扩展 ggplot2,我们将在本例中将其称为 foo。目标是编写一个 +.foo 方法来代替 +.gg。但是我遇到了“不兼容方法”的问题 设置 目前我可以编写 ggplot_a
全面警告:react-dom.development.js:2592 指定的值“”不符合要求的格式。格式为“#rrggbb”,其中 rr、gg、bb 是两位十六进制数。 代码运行良好。它正在做它应该做
我正在尝试用 HTML5 制作一个多人游戏。我找到了 lance.gg 并四处游玩。我像这样修改了 Pong 游戏。 移除 Paddle(只剩下 Ball) 将重力设置为 (0, 0.1) 设置Bal
我正在尝试为 Oracle Golden Gate (12c) 配置 BigQuery 适配器。但不幸的是我无法理解这个gg.classpath。在文档中,他们提到从 GitHub 下载客户端库 但是
我的最终目标是使用 gg=G 自动缩进符合 eslintrc.js 文件的所有 JS 代码。 所以,目前我有 syntastic和 vim-javascript在我的 .vimrc 中查看我的 JS
我使用存储过程来检查表中是否存在名称,方法是使用以下代码片段.. BEGIN IF ids = 0 THEN SELECT * FROM table_name WHERE `table_id` = a
我想用 css 选择 child 的最后一个 该文本出现gg我只想选择 Last gg 请帮忙 ul { margin: 0; padding: 0; } .container ul li +
我试图运行一段非常简单的代码,以便为作业创建ggplot。我是R的新手,所以我怀疑这是一个简单的问题,但现在我只是在努力。我的教授实际上写了这段代码,并且已经为其他与我交谈过的学生工作过。但是,我遇到
通常,人们在 vim 中使用 gg=G 来自动切换 C/C++/Java 代码或实际上任何类型的代码。 但是,我在 vim 的默认设置中将 tabstop 变量更改为 tabstop=2,这样每当我按
我使用这段代码将数据导出到一个 csv 文件并且它有效: project_id = 'project_id' client = bigquery.Client() dataset_id = 'data
我是一名优秀的程序员,十分优秀!