【问题标题】:Error when plotting in a function with ggplot2使用 ggplot2 在函数中绘图时出错
【发布时间】: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,...):参数必须具有相同的长度

我哪里出错了,我该如何改正?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    是的,另一种解决方案是创建单独的函数

    makeData <- function() {
      makingData <- data.frame(factorname=rep(c("first","second"),10), Topic=sort(c(1:10, 1:10)), Distribution=sample(1:200,20))
      makingData <- makingData[order(makingData$factorname,makingData$Topic),]
      topicavg <- c()
      for (row in 1:10) {topicavg[row] <- makingData[row,3]-makingData[row+10,3]}
      topicavg <- c(topicavg,topicavg)
      makingData <- cbind(makingData,topicavg)
      return(makingData)
    }
    
    make.pyramid <- function(){
      myData <- makeData()
      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)
    }
    

    【讨论】:

    • 这样更干净。谢谢!
    【解决方案2】:

    我想通了!我认为我在 ggplot2 上遇到了问题,这让我偏离了轨道。我实际上错误地使用了该功能。在这个例子中,我需要在全局范围内定义我的对象,即使只是为了在函数中本地使用它:

    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)
      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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-09
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多