【问题标题】:Non-numeric argument error to binary operation in R, needs explanationR中二进制运算的非数字参数错误,需要解释
【发布时间】:2021-04-10 18:13:35
【问题描述】:

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

如果有任何帮助,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)是数字。然而,在第一个我没有得到任何错误,而在第二个中,我做到了。有什么想法吗?

【问题讨论】:

  • head 可以提供一些数据来复制您的错误吗?我假设您检查了每一列的class 而没有假设它?

标签: r ggplot2 syntax-error numeric


【解决方案1】:

根据我的经验,如果我弄错了一个括号并尝试在对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 周围多余的()

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

【讨论】:

  • 这并没有进入我的脑海中的可能性列表,因为 R studio 的左侧没有红色 x 或错误。感谢您的帮助。
  • 是的,这里的错误并不是特别有用。我经常犯这个错误,以至于它成为我首先要寻找的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-15
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-02
  • 2016-11-15
相关资源
最近更新 更多