【发布时间】:2016-10-28 17:34:15
【问题描述】:
我绘制堆叠金字塔的代码在终端窗口中运行良好,但在函数内部时会引发错误。
这是我的代码:
make.pyramid <- function(){
mydata <- data.frame(factorname=rep(c("first","second"),10), Topic=sort(c(1:10, 1:10)), Distribution=sample(1:200,20))
mydata <- mydata[order(mydata$factorname,mydata$Topic),]
topicavg <- c()
for (row in 1:10) {topicavg[row] <- mydata[row,3]-mydata[row+10,3]}
topicavg <- c(topicavg,topicavg)
mydata <- cbind(mydata,topicavg)
library(ggplot2)
dist <- ggplot(data=mydata, aes_q(x=substitute(reorder(Topic, topicavg)), y=quote(Distribution), fill=as.name("factorname")))
dist <- dist + geom_bar(data=subset(mydata,mydata[,1]=="first"), stat="identity")
dist <- dist + geom_bar(data=subset(mydata,mydata[,1]=="second"), stat="identity", position="identity", mapping=aes(y=-Distribution))
dist <- dist + scale_y_continuous(labels=abs)
dist <- dist + xlab("Topics")
dist <- dist + coord_flip()
dist <- dist + geom_point(data=subset(mydata,mydata[,1]=="second"), mapping=aes(y=topicavg), shape=4, show.legend = F)
print(dist)
}
但是在函数中使用时,出现如下错误:
tapply 中的错误(X = X,INDEX = x,FUN = FUN,...):参数必须具有相同的长度
我哪里出错了,我该如何改正?
【问题讨论】: