[R] ggplot: combining geom's in function

sbihorel Sebastien.Bihorel at cognigencorp.com
Fri Oct 23 21:46:49 CEST 2015


Following up on my previous reply, this following would work but would 
not behave like a geom function:

geom_xyplot <- function (gplot, mapping = NULL, data = NULL, stat = 
"identity",
                          position = "identity", na.rm = FALSE, type = 
'p', ...) {

   if (any(type=='p')){
     gplot + geom_point(mapping = mapping, data = data, stat = stat,
                        position = position, na.rm = na.rm, ...)
   }
   if (any(type=='l')){
     gplot + geom_path(mapping = mapping, data = data, stat = stat,
                       position = position, na.rm = na.rm, ...)
   }
   if (any(type%in%c('b','o'))){
     gplot + 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, ...)
   }
}

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