You can use the ggplot2
package for this nicely:
library(ggplot2)
trajectories <- structure(list(X = c(2L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 7L), Y = c(4L, 6L, 4L, 8L, 7L, 5L, 4L, 6L, 4L), ID = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L)), .Names = c("X", "Y", "ID"), class = "data.frame", row.names = c(NA, -9L))
ggplot(trajectories, aes(x=log(abs(X) + 0.01), y=log(Y))) +
geom_point() +
facet_wrap( ~ ID)
For what its worth, the reason your cod is failing is exactly what the error says. the second argument to sapply
needs to be a function. If you define your plot code as a function:
myfun <- function(DF) {
plot(log(abs(DF$X) + 0.01), log(DF$Y))
}
But this will not split your data on ID
. You could also use the plyr
or data.table
package to do this splitting and plotting but you will need to write the plots to a file or they will close as each new plot is created.