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

Non-numeric argument error to binary operation in R, needs explanation

这是 () 的延续。提供的代码效果很好。数据是手动创建的。所以我转移了一个更大的数据集,现在收到以下错误。

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

如果这有任何帮助,则使用以下代码创建 AnnualDifference 项目

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

让我感到困惑的是,图表中涉及的项目的数据类型是相同的。 Seriesttls 是字符,AnnualDifference(或上一个问题中的 A)是数字。然而,在第一个中我没有出错,而在第二个中,我有。想知道这是为什么吗?

根据我的经验,如果我弄错了一个括号并试图在对 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.

调用周围多余的 ()

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