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
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.
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>
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.
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.