please handle 'ggplot() + c()' more elegantly

9 views
Skip to first unread message

Matthew O'Meara

unread,
Jul 13, 2010, 12:32:33 PM7/13/10
to ggplot2
Hi,

Would it be possible to have ggplot2 handle this

ggplot() + c()

by doing nothing rather than giving a cryptic error? It think it
would only take adding an extra case to "+.ggplot" <- function(...)
before the last 'else' block.


6813a6814,6816
> } else if(class(object) == "NULL"){
> set_last_plot(p)
> p


This naturally comes up for example,

add_stuff_to_plot <- function (p, layers=NULL, scales=NULL,
facets=NULL, coord=NULL, options=NULL ){
# do stuff with each of the layers, scales, facets, coord and options...
...
p <- p + layers
...
p <- p + scales
...
}

There may be other ways of doing this (ie only add it if it actually
contains an object), but I don't think this is totally unreasonable as
it is. Depending on the philosophy of the project, I could see either
giving a warning/error that what was passed in was not allowed or
simply assuming the user knew what was going on and do something
sensible (ie just return the cloned ggplot object), but giving the
error

Error in switch(object$class(), layer = { : attempt to apply non-function

is frustrating because it doesn't give much indication about what went
wrong without digging through the ggplot2 code.

Best,
Matt


PS maybe "+.ggplot" could also handle when class(object)== "logical"
more elegantly as well. Thanks!

Hadley Wickham

unread,
Jul 16, 2010, 6:16:50 AM7/16/10
to Matthew O'Meara, ggplot2
Hi Matt,

Both features there now in the development version.

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Matthew O'Meara

unread,
Jul 16, 2010, 8:49:09 AM7/16/10
to Hadley Wickham, ggplot2
Thanks Hadley!

Mike Lawrence

unread,
Jul 16, 2010, 10:34:41 AM7/16/10
to Matthew O'Meara, ggplot2
In the meantime, why not just check whether those objects are null in
the first place?

add_stuff_to_plot <- function (p, layers=NULL, scales=NULL,
facets=NULL, coord=NULL, options=NULL ){
# do stuff with each of the layers, scales, facets, coord and options...
...

if(!is.null(layers)){


p <- p + layers
}
...

if(!is.null(scales)){


p <- p + scales
}
...
}

--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tr.im/mikes_public_calendar

~ Certainty is folly... I think. ~

Reply all
Reply to author
Forward
0 new messages