r - 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)是数字。然而,在第一个我没有得到任何错误,而在第二个中,我做到了。关于为什么会这样的任何想法?
解决方案
根据我的经验,如果我弄错了一个括号并尝试在对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
.
如果这不能解决您的问题,请务必提供一些数据,以便我们重现错误。
推荐阅读
- pyspark - 在 PySpark 中使用 pandas_udf 均匀分配组任务
- sql-server - SQL Server:面临通过 XQuery 解析嵌套 xml 元素的问题
- ms-word - 如何在word中在线添加和显示书签?
- apache-spark - 尝试为 TransformedDStream 中的每个项目(列表)应用函数时出错
- python - 从函数创建年份列表
- mysql - mysql 如何将行结果转换为具有新列名的列?
- .net - 使用 ReactiveUI 与 PasswordBox 进行两种方式绑定
- matlab - 如何将单元格数组的元素从 double 转换为 char?
- rust - 如果在 Rust 中使用异步,你总是需要一个异步 fn main() 吗?
- python - 分叉/拆分数字以降低粒度熊猫 | 整数四舍五入