2

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.

4
  • @Pascal: I don't see how. My question is just whether this is the correct behaviour of the subset variable inside ggplot calls, not how to use the (unrelated to ggplot) subset() function. Commented May 25, 2015 at 11:58
  • Did you notice the word "possible"? Commented May 25, 2015 at 12:07
  • 1
    This does not seem a duplicate, however it is probably off-topic, since asking whether something is correct behavior or not doesn't fall into the strict "coding question" category this site is usually about. Commented May 25, 2015 at 14:43
  • 1
    This question has nothing to do with the duplicate question as the OP already knows how to use subset. His question is completely different and it would be wise for people with no experience in r (according to their profiles) to not vote to close such questions. I have voted to reopen. Commented May 25, 2015 at 16:48

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. Apart from looking at the result, which can sometimes be very tricky, is there any way I could have noticed I was not subsetting correctly, since I was initially using the first code in my question?
You are welcome, glad I could be of help :). As far as I know there isn't a way apart from checking either the results or the source code.
thanks again, also for the vote. I wish there was a way to issue a warning if an argument is accepted but not used nor passed over to any other function inside. In this case it would have saved me lots of troubleshooting time!
No problem :). There is a possibility that someone else knows of a package or a way to do it to be honest. If you have the time it might be worth asking a new question (obviously only if you want).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.