[R] ggplot: combining geom's in function

Erich Neuwirth erich.neuwirth at univie.ac.at
Fri Oct 23 18:44:07 CEST 2015


I often look for examples in
http://www.cookbook-r.com/Graphs/ <http://www.cookbook-r.com/Graphs/>

> On 23 Oct 2015, at 18:27, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> 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.
> 
> ______________________________________________
> 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.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 670 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20151023/438b3e8d/attachment.bin>


More information about the R-help mailing list