[R] ggplot: combining geom's in function
sbihorel
Sebastien.Bihorel at cognigencorp.com
Fri Oct 23 15:12:41 CEST 2015
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
More information about the R-help
mailing list