- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 ggplot2 构建一个自定义 stat 函数,其中我想访问一个离散变量来计算每组的统计数据。但是,ggplot 层的默认行为是自动将隐式组分配给任何离散变量(主要是)。这意味着我的数据会通过自动分组进行拆分,这是我不想要的。
我可以显示如下;我有一个非常标准的构造函数:
library(ggplot2)
stat_example <- function(
mapping = NULL,
data = NULL,
geom = "point",
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
) {
layer(data = data,
mapping = mapping,
stat = StatExample,
geom = geom,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(na.rm = na.rm))
}
value
这里。
StatExample <- ggproto(
"StatExample",
Stat,
required_aes = c("x", "y", "value"),
default_aes = aes(x = after_stat(x), y = after_stat(y)),
compute_group = function(data, scales) {
print(head(data, 2))
data
}
)
compute_group()
的内容。功能为
data
.
g <- ggplot(iris) +
stat_example(aes(Sepal.Width, Sepal.Length, value = Species))
# To get only the print side-effect, not the plot (which looks normal)
g <- ggplotGrob(g)
#> x y value PANEL group
#> 1 3.5 5.1 setosa 1 1
#> 2 3.0 4.9 setosa 1 1
#> x y value PANEL group
#> 51 3.2 7.0 versicolor 1 2
#> 52 3.2 6.4 versicolor 1 2
#> x y value PANEL group
#> 101 3.3 6.3 virginica 1 3
#> 102 2.7 5.8 virginica 1 3
group
打印了 3 个 data.frames变量,这意味着数据已被分成 3 组。我认为到达那里需要的是
value
变量逃逸自动组检测。
-1
,这是标准的“无组”组。但是,当我这样做时,数据不会自动分组,例如 aes(colour = some_variable)
.这是我绝对希望发生的。 ggplot2:::add_group()
功能,似乎我可以通过命名我的 value
来逃避自动分组变量 label
,但是这会使统计数据与 geom_text()
不兼容并且它没有描述value
的含义自然。 layer()
使用此函数的变体调用,这将创建一个不同的层 ggproto 对象,其中 compute_aesthetics()
不同的工作组。然而,这是我宁愿避免承担的大量工作。 vctrs::new_vctr(..., class = "not_discrete")
的方式来耍花招。 ,但是在哪里包装我的value
合适的地方该类中的变量? label
”的新看法' 也有争论。
最佳答案
如果这是一个偶然的用例,一个简单的(虽然是手动的)hack 可能正在运行 trace(ggplot2:::add_group, edit = TRUE)
并添加 "value"
旁边 "label", "PANEL"
作为要从自动组检测中排除的变量名称。
实现相同效果的较少手动(但可能更脆弱)的方法将涉及以下步骤:
add_group
的修改版本具有上述修改的功能; Layer
的修改版本使用修改后的 ggproto 对象 add_group
在其 compute_aesthetics
功能; # define modified add_group function
add_group2 <- function (data) {
if (ggplot2:::empty(data))
return(data)
if (is.null(data$group)) {
disc <- vapply(data, ggplot2:::is.discrete, logical(1))
disc[names(disc) %in% c("label", "PANEL", "value")] <- FALSE # change here
if (any(disc)) {
data$group <- vctrs::vec_group_id(data[disc])
}
else {
data$group <- ggplot2:::NO_GROUP
}
} else {
data$group <- vctrs::vec_group_id(data["group"])
}
data
}
# define modified compute_aesthetics function that uses modified add_group in second last line
compute_aesthetics_alt <- .subset2(ggplot2:::Layer, "compute_aesthetics")
body(compute_aesthetics_alt)[[length(body(compute_aesthetics_alt)) - 1]] <-
quote(evaled <- add_group2(evaled))
# define modified Layer ggproto object that uses alternative compute_aesthetics
Layer2 <- ggproto("Layer2",
ggplot2:::Layer,
compute_aesthetics = compute_aesthetics_alt)
# define modified stat with Layer2 specified as its layer_class
stat_example <- function(
mapping = NULL,
data = NULL,
geom = "point",
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
) {
layer(data = data,
mapping = mapping,
stat = StatExample,
geom = geom,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(na.rm = na.rm),
layer_class = Layer2) # change here
}
用法:
# add new column to simulate different colour
iris$gg <- sample(c("a", "b"), size = nrow(iris), replace = TRUE)
ggplot(iris) +
stat_example(aes(Sepal.Width, Sepal.Length,
value = Species))
# prints one data frame, because there's only one group by default
ggplot(iris) +
stat_example(aes(Sepal.Width, Sepal.Length,
value = Species, colour = gg))
# prints two data frames, because grouping is based on the colour aesthetic,
# which has two possible values
关于r - 在自定义统计中转义 'discrete aesthetic implies group',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62075586/
我有一个 javascript 从用户输入中读取的 URL。这是 JavaScript 代码的一部分: document.getElementById("Snd_Cont_AddrLnk_BG").v
我将如何在 javascript 中转义斜杠// var j = /^(ht|f)tp(s?)://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$;/ 最佳答案 使用 \ 进行转
在解析到这样的对象之前,我要转义 & 和 =: var obb = parseJSON('{"' + text.replace(/&/g, "\",\"").replace(/=/g,"\":\"")
我正在使用 freemarker 生成一个 freemarker 模板。但我需要一些方法来转义 freemarker 标签。 我将如何逃脱 标签或 ${expression} ? 最佳答案 您也可以使
我正在尝试匹配方括号,即 excel 中正则表达式 VBA 中的 []。我正在尝试使用以下代码,但它不起作用。 Public Function IsSpecial(s As String) As L
我通过设置将 PowerShell 添加到我的上下文菜单中: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\she
我需要转义 $,因此我需要将所有出现的 $ 替换为 \$ 所以我写了这个方法: // String#replaceAll(String regex, String replacement) publi
我正在格式化我的问题。非常遗憾。这是我的问题的摘要 在 JSP 中我有一个字段 我输入的值类似于“cQN==ujyRMdr+Qi8dO9Xm*eRun+ner==aLTyt?aKmGI” 实际行动
我有一个文本文件,其内容是C:\temp 我想要值 C:\temp替换为从变量定义的不同值 此外,将从批处理文件(windows .cmd)中调用 perl oneliner set CMDDIR=C
有没有办法使用 jTemplates 来转义 {$,这样我就可以在 onBlur 中使用内联 javascript,例如 telegraaf 在 processTemplate 之后得到这个: 谢谢
我正在尝试将 wget 与包含“#”符号的 url 一起使用。无论我做什么来逃避这个角色,它都不起作用。我用过\、' 和 "。但它们都不起作用。有人有什么建议吗? 谢谢! 最佳答案 如果您真的想让它有
我想知道如何从数据库中回显带有 $ 符号的字符串。此时,数据库中的值“Buy one for $5.00”将转换为“Buy one for .00”。 假设该字段的名称为 title,值为 Buy o
我在 mySQL 中有一个查询,旨在返回我们网站上使用的搜索词。是的,这是一个标签云,是的,我知道它是一条鲻鱼 :) 我们有一个管理页面,管理员可以在其中查看搜索词并选择将它们排除在云端之外。这些词进
我有一个文本区域。在其点击事件上。我将其插入数据库中,然后将其显示为元素列表中的第一个元素。问题是。如果我输入""在textarea中,jquery无法正确显示。它显示为空。代码是 var note
我想知道是否有某种字符串前缀,这样 cstring 就可以按原样使用,而不需要我转义所有字符。我不是 100% 确定。我记得一些关于在字符串前加上 @ 符号( char str[] = @"some\
这个问题在这里已经有了答案: How do I escape curly-brace ({}) characters in a string while using .format (or an f
C/C++编译器如何操作源代码中的转义字符["\"]?如何编写用于处理该字符的编译器语法?遇到那个字符后,编译器会做什么? 最佳答案 大多数编译器分为几个部分:编译器前端称为 lexical anal
我计划接受用户输入,并将其插入到一个 div 中 user_content 一个用户提供内容,另一个用户接收内容。 我认为我会遵循的建议来自 https://www.owasp.org/index.p
我有一个这种形式的 url - http:\\/\\/en.wikipedia.org\\/wiki\\/The_Truman_Show。我怎样才能使它成为正常的网址。我试过使用 urllib.unq
我有一个带有转义数据的字符串 escaped_data = '\\x50\\x51' print escaped_data # gives '\x50\x51' 什么 Python 函数会对其进行反转
我是一名优秀的程序员,十分优秀!