[R] ggplot: combining geom's in function
sbihorel
Sebastien.Bihorel at cognigencorp.com
Fri Oct 23 21:06:08 CEST 2015
Hi
Thanks for the suggestion. Unfortunately, the qplot function would work
nicely if only I did not need to combine its output with other geom
called before...
A simple example using the data previously described:
ggplot(data, aes(x,y,group=g)) + geom_blank() +
qplot(x=x,y=y,data=data,group=g,colour=factor(g),geom = c('point','line'))
> Error in p + o : non-numeric argument to binary operator
In addition: Warning message:
Incompatible methods ("+.gg", "Ops.data.frame") for "+"
I know I could combine the geom's by adapting the order of the calls to
my data and needs for this one example. But this is not my goal. I am
trying to build a generic function, not an ad-hoc script.
Sebastien
On 10/23/2015 12:27 PM, Jeff Newmiller wrote:
> Have you looked at the qplot function in the ggplot2 package?
> ---------------------------------------------------------------------------
> Jeff Newmiller The ..... ..... Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
> Live: OO#.. Dead: OO#.. Playing
> Research Engineer (Solar/Batteries O.O#. #.O#. with
> /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
> ---------------------------------------------------------------------------
> Sent from my phone. Please excuse my brevity.
>
> On October 23, 2015 3:12:41 PM GMT+02:00, sbihorel <Sebastien.Bihorel at cognigencorp.com> wrote:
>> Hi,
>>
>> Next adventure into my journey from lattice to ggplot: I would like to
>> create a custom generic function that combines multiple existing geom's
>>
>> in order to reproduce what the lattice panel.xyplot function does based
>>
>> on the type argument (ie, plotting points only for type='p', plotting
>> lines for type 'l', etc).
>>
>> My current naive attempt is:
>>
>> library(lattice)
>> library(ggplot2)
>>
>> geom_xyplot <- function (mapping = NULL, data = NULL, stat =
>> "identity",
>> position = "identity", na.rm = FALSE, type =
>> 'p', ...) {
>>
>> if (any(type=='p')){
>> geom_point(mapping = mapping, data = data, stat = stat,
>> position = position, na.rm = na.rm, ...)
>> }
>> if (any(type=='l')){
>> geom_path(mapping = mapping, data = data, stat = stat,
>> position = position, na.rm = na.rm, ...)
>> }
>> if (any(type%in%c('b','o'))){
>> geom_point(mapping = mapping, data = data, stat = stat,
>> position = position, na.rm = na.rm, ...) +
>> geom_path(mapping = mapping, data = data, stat = stat,
>> position = position, na.rm = na.rm, ...)
>> }
>> }
>>
>> data <- data.frame(x = rep(1:4, each = 25),
>> y = rep(1:25, times = 4),
>> g = rep(1:4, each = 25))
>> data$x <- data$x + 0.005*(data$y)^2-0.1*data$y+1
>>
>> ggplot(data2, aes(x, y, group = g, colour = factor(g))) +
>> geom_xyplot(type = 'l')
>>
>> I get:
>>> Error: No layers in plot
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list