Combining geoms & scales

59 views
Skip to first unread message

Ryan Hope

unread,
May 18, 2013, 8:56:20 AM5/18/13
to ggp...@googlegroups.com
I am working on a R package for eyetracking reasearch and in it I provide some functions to visual data with ggplot2. Some of the visualizations are stand alone plots but others work well as layers that could be combined with other plots. I've created some "custom" geoms by simply wrapping pre-existing geoms in a function so that I can transform in input into ggplot friendly arguments. This works fine for most cases but I have run into a few situations where I would like to combine 2 geoms into one. For example, I would like to do something like this:

geom_grid <- function() {
 geom_hline() + geom_vline()
}

which obviously throws a "non-numeric argument to binary operator" error. The same thing goes for creating a function to set both x and y scales with one function. Is there an easy way to do this?

--
Ryan Hope, M.S.
CogWorks Lab
Cognitive Science Department
Rensselaer Polytechnic Institute

Brian Diggs

unread,
May 20, 2013, 5:03:31 PM5/20/13
to Ryan Hope, ggplot2
On 5/18/2013 5:56 AM, Ryan Hope wrote:
> I am working on a R package for eyetracking reasearch and in it I provide
> some functions to visual data with ggplot2. Some of the visualizations are
> stand alone plots but others work well as layers that could be combined
> with other plots. I've created some "custom" geoms by simply wrapping
> pre-existing geoms in a function so that I can transform in input into
> ggplot friendly arguments. This works fine for most cases but I have run
> into a few situations where I would like to combine 2 geoms into one. For
> example, I would like to do something like this:
>
> geom_grid <- function() {
> geom_hline() + geom_vline()
> }


geom_grid <- function() {
c(geom_hline(), geom_vline())
}

Or in a complete example:

geom_grid <- function(v = 0) {
c(geom_hline(yintercept=v), geom_vline(xintercept=v))
}

ggplot(data.frame(x=rnorm(20), y=rnorm(20)), aes(x,y)) +
geom_point() +
geom_grid() +
geom_grid(1)


> which obviously throws a "non-numeric argument to binary operator" error.
> The same thing goes for creating a function to set both x and y scales with
> one function. Is there an easy way to do this?
>


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

Ryan Hope

unread,
May 20, 2013, 6:55:41 PM5/20/13
to Brian Diggs, Ryan Hope, ggplot2
Awesome, this seems to work great for geoms but not for scales :/




--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2

--- You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


thomas kern

unread,
May 20, 2013, 8:22:09 PM5/20/13
to ggp...@googlegroups.com, Brian Diggs, Ryan Hope
try

sc = function(){
  list(scale_x_continuous(breaks=1:10), scale_y_continuous(breaks=100:101))

Ryan Hope

unread,
May 20, 2013, 9:52:38 PM5/20/13
to thomas kern, ggp...@googlegroups.com, Brian Diggs, Ryan Hope
Using list() works for both geoms and scales, thanks Brian!
Reply all
Reply to author
Forward
0 new messages