0

I am trying plot the subsets of a table using ggplot and gridExtra. But I have bumbed in the following error EXPR must be a length 1 vector.

I could come up with any side step. Any help would be useful.

Here is a little example of what I'm trying to perform:

# the table
dt1 <- data.table(parkName=rep(c("Zone A","Zone B", "Zone C" ,
"Zone D"),5), boundary=rep(0:1,10),v=1:20, w=rnorm(20))[]

# criteria for subsetting the table
dt2 <- data.table(zone1 = c("Zone A","Zone B"), zone2 =c("Zone B","Zone C"))

# function for subsetting the table and plotting
p <- function(sd1,sd2){
dlist <- dt1[parkName==sd1 | parkName==sd2]    

b <- dt1[parkName %in% dlist]
a <- ggplot(
       b,
       aes(v,w)) + geom_line()
return(a)
}

mplot <- mapply(p,dt2[,zone1],dt2[,zone2])
cairo_pdf("myplot1.pdf")
do.call(marrangeGrob, c(mplot, list(nrow=2, ncol=2)))
dev.off()

# results
Error in switch(ct, ggplot = ggplotGrob(grobs[[ii.table]]), trellis =
 latticeGrob(grobs[[ii.table]]),  : EXPR must be a length 1 vector
4

1 回答 1

2

Change

mplot <- mapply(p,dt2[,zone1],dt2[,zone2])

to

mplot <- mapply(p,dt2[,zone1],dt2[,zone2], SIMPLIFY=FALSE)

or

mplot <- Map(p,dt2[,zone1],dt2[,zone2])

mapply() will attempt to coerce it's result to a matrix if the dimensions of the returned objects match up, however, in this case you will always want a list. You can either set the SIMPLIFY= parameter to false, or use Map() which always returns a list.

于 2015-03-09T18:40:34.760 回答