R,mapply,ggplot:EXPR必须是长度为1的向量 [英] R, mapply , ggplot : EXPR must be a length 1 vector
问题描述
我正在尝试使用ggplot和gridExtra绘制表的子集. 但是我深陷以下错误,EXPR必须是长度为1的向量.
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
推荐答案
更改
mplot <- mapply(p,dt2[,zone1],dt2[,zone2])
到
mplot <- mapply(p,dt2[,zone1],dt2[,zone2], SIMPLIFY=FALSE)
或
mplot <- Map(p,dt2[,zone1],dt2[,zone2])
如果返回的对象的尺寸匹配,
mapply()
将尝试将其结果强制转换为矩阵,但是,在这种情况下,您将始终需要一个列表.您可以将SIMPLIFY=
参数设置为false,或使用始终返回列表的Map()
.
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.
这篇关于R,mapply,ggplot:EXPR必须是长度为1的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!