很明显,下面这一行想要做什么:
ggplot(data=mtcars, aes(x=mpg, y=cyl), subset=.(gear=="5")) +
geom_point(aes(colour=gear))但是它不起作用(子集只是被忽略了)。真正起作用的是:
ggplot(data=mtcars, aes(x=mpg, y=cyl)) +
geom_point(aes(colour=gear), subset=.(gear=="5"))也可以:
ggplot(data=subset(mtcars, gear=="5"), aes(x=mpg, y=cyl)) +
geom_point(aes(colour=gear))因此,子集似乎只能从几何学调用中调用,而不能直接从ggplot()调用。这是一个错误,还是这是正确的行为?ggplot不返回任何类型的警告或错误。
发布于 2015-05-25 12:47:13
我不认为这是个窃听器。看起来,如果您看到两个函数的源代码:ggplot和geom_point,那么它的意图是
对于ggplot
> getAnywhere(ggplot.data.frame)
A single object matching ‘ggplot.data.frame’ was found
It was found in the following places
registered S3 method for ggplot from namespace ggplot2
namespace:ggplot2
with value
function (data, mapping = aes(), ..., environment = globalenv())
{
if (!missing(mapping) && !inherits(mapping, "uneval"))
stop("Mapping should be created with aes or aes_string")
p <- structure(list(data = data, layers = list(), scales = Scales$new(),
mapping = mapping, theme = list(), coordinates = coord_cartesian(),
facet = facet_null(), plot_env = environment), class = c("gg",
"ggplot"))
p$labels <- make_labels(mapping)
set_last_plot(p)
p
}
<environment: namespace:ggplot2>和geom_point
> geom_point
function (mapping = NULL, data = NULL, stat = "identity", position = "identity",
na.rm = FALSE, ...)
{
GeomPoint$new(mapping = mapping, data = data, stat = stat,
position = position, na.rm = na.rm, ...)
}
<environment: namespace:ggplot2>如果您查看省略号参数...,您将看到它没有在ggplot函数中使用。因此,您对参数subset=.()的使用不会被转移或在任何地方使用。但是,由于ggplot函数中存在省略号,所以它不会给出任何错误或警告。
另一方面,geom_point函数使用省略号并将其传递给使用省略号的GeomPoint$new。在这种情况下,subset=.()参数被转移到使用它的GeomPoint$new中,从而产生所需的结果。
https://stackoverflow.com/questions/30435345
复制相似问题