代码之家  ›  专栏  ›  技术社区  ›  Tim Wilcox

R中二进制运算的非数字参数错误,需要解释

  •  0
  • Tim Wilcox  · 技术社区  · 4 年前

    how to modify axis labels ggplot in R ). 提供的代码工作得很好。数据是手动创建的。所以我转移了一个更大的数据集,现在得到以下错误。

       Error in aes(y = AnnualDifference, x = (reorder(Seriesttls, AnnualDifference))) +  :
       non-numeric argument to binary operator
    

    这是正在使用的代码

       jobgrowthbyindustry<-ggplot(data=nvces2, aes(y=AnnualDifference,x= 
       (reorder(Seriesttls,AnnualDifference)))+geom_col(color="blue")
       +coord_flip()+labs(x=NULL)+ggtitle("Nevada Nonfarm Job Growth by Industry"))+
       theme(plot.title.position = "plot",
       plot.title = element_text(hjust =0.5))
    

      nvces<- nvces%>%
      group_by(adjusted,areaname,seriescode)%>%
      mutate(PreviousYear=lag(ravg,12), 
         PreviousMonth=lag(ravg),
         AnnualDifference=ravg-PreviousYear,
         MonthlyDifference=ravg-PreviousMonth,
         MonthlyGrowthRate=MonthlyDifference/PreviousMonth,
         PercentGrowthRate=AnnualDifference/PreviousYear)%>%
      ungroup()
    

    1 回复  |  直到 4 年前
        1
  •  2
  •   coffeinjunky    4 年前

    根据我的经验,如果我把其中一个圆括号弄错了,并且试图在调用中添加一些内容,就会出现这个错误 ggplot . 您的格式设置使其难以查看,因此让我们看一下格式更精确的:

       jobgrowthbyindustry <- 
          ggplot(data=nvces2, 
                 aes(y = AnnualDifference, 
                     x = (reorder(Seriesttls,AnnualDifference))   
                     )
                 + geom_col(color="blue")
                 + coord_flip()
                 + labs(x=NULL)
                 + ggtitle("Nevada Nonfarm Job Growth by Industry")
                 ) + theme(plot.title.position = "plot",
                           plot.title = element_text(hjust =0.5)
                 )
    

    应该是:

       jobgrowthbyindustry <- 
          ggplot(data=nvces2, 
                 aes(y = AnnualDifference, 
                     x = (reorder(Seriesttls,AnnualDifference))   
                     )
                 ) +
         geom_col(color="blue") +
         coord_flip() +
         labs(x=NULL) +
         ggtitle("Nevada Nonfarm Job Growth by Industry") +
         theme(plot.title.position = "plot",
               plot.title = element_text(hjust =0.5)
               )
    

    () 围绕着呼叫 reorder

    如果这不能解决您的问题,请提供一些数据,以便我们重现错误。