首页 > 解决方案 > R中二进制运算的非数字参数错误,需要解释

问题描述

这是(如何在 R 中修改轴标签 ggplot)的延续。提供的代码效果很好。数据是手动创建的。所以我转移了一个更大的数据集,现在出现以下错误。

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

我的困惑是图表中涉及的项目的数据类型是相同的。Seriesttls 是字符,AnnualDifference(或上一个问题中的 A)是数字。然而,在第一个我没有得到任何错误,而在第二个中,我做到了。关于为什么会这样的任何想法?

标签: rggplot2syntax-errornumeric

解决方案


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

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


推荐阅读