AF7
AF7

Reputation: 3242

subset=.() cannot be called in ggplot() directly

It is pretty clear what the following line wants to do:

ggplot(data=mtcars, aes(x=mpg, y=cyl), subset=.(gear=="5")) + 
geom_point(aes(colour=gear))

But it doesn't work (subset is just ignored). What does indeed work is:

ggplot(data=mtcars, aes(x=mpg, y=cyl)) +
geom_point(aes(colour=gear), subset=.(gear=="5"))

or also:

ggplot(data=subset(mtcars, gear=="5"), aes(x=mpg, y=cyl)) +
geom_point(aes(colour=gear))

So it seems subset can be called only from geometry calls, and not from ggplot() directly. Is this a bug or is this the correct behaviour? ggplot doesn't return any kind of warning or error.

Upvotes: 2

Views: 423

Answers (1)

LyzandeR
LyzandeR

Reputation: 37879

I don't think this is a bug. It looks like it is intended if you see the source code of the two functions: ggplot and geom_point:

For 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>

And 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>

If you look at the ellipsis argument ... you will see that it is not used in the ggplot function. So, your use of the argument subset=.() is not transferred or used anywhere. It does not give any errors or warnings however because of the existence of the ellipsis in the ggplot function.

On the other hand the geom_point function uses the ellipsis and transfers it to GeomPoint$new where it is used. In this case your subset=.() argument is transferred to GeomPoint$new where it is used, producing the result you want.

Upvotes: 3

Related Questions