本文介绍了错误:美学必须是长度为1,或者与dataProblems:personCategoryz的长度相同时省略NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图用泰坦尼克数据集创建一个图表,看看女性,儿童和男性以及他们的生存率。我已经创建了新的类别来读取数据,但当我尝试做更多的事情时,不断发出错误消息。 当我运行一个图表来显示它时,它表现良好,除了它有一个单独的NA数据类别,所以我试图省略数据和错误消息美学必须是长度为1或与dataProblems:personCategoryz相同的长度。 以下是我所做的,大部分情况都很好,除了显示NA数据外: titanic $ personCategoryx [titanic $ Age< 14] titanic $ personCategoryx [titanic $ Age> 13& titanic $ Sex ==female] titanic $ personCategoryx [titanic $ Age> 13& (titanic $ personCategoryx,labels = c(children,women,men)) ggplot(titanic)+ geom_bar(aes(x = personCategory,fill = factor(Survived))) 这里是我给的错误信息: titanic $ personCategoryx [titanic $ Age< 14] titanic $ personCategoryx [titanic $ Age> 13& titanic $ Sex ==female] titanic $ personCategoryx [titanic $ Age> 13& (titanic $ personCategoryx,labels = c(children,women,men)) personCategoryz< - na.omit(titanic $ personCategory) summary(personCategoryz) ggplot(titanic)+ geom_bar(aes(x = personCategoryz,fill = factor(Survived ))) 我会如何解决这个问题?感谢!解决方案发生这个问题是因为 personCategoryz 的长度不一样如 titanic 。在任何情况下,使用 ggplot 不是数据框中列的向量通常不是一个好主意。你可以做的是缩短整个 data.frame ,这样你只能看完整的情况: titanic2 ggplot(titanic2)+ geom_bar(aes(x = personCategory,fill = factor(Survived))) I'm trying to create a graph using titanic dataset that looks at women, children, and men and their survival rates. I've created new categories to read the data from, but keep coming up with error messages when i try to do more beyond that point.When I run a graph to show this, it comes up fine except it has a separate category for NA data, so I tried to omit the data and the error message "Aesthetics must either be length one, or the same length as the dataProblems:personCategoryz" comes up.Here is what I do that is fine for the most part, with the exception of showing NA data: titanic$personCategoryx[titanic$Age < 14] <-1titanic$personCategoryx[titanic$Age > 13 & titanic$Sex=="female"] <-2titanic$personCategoryx[titanic$Age > 13 & titanic$Sex=="male"] <-3titanic$personCategory <- factor(titanic$personCategoryx, labels=c("children", "women", "men"))ggplot(titanic) + geom_bar(aes(x=personCategory, fill=factor(Survived)))here is what I put in that gives me the error message:titanic$personCategoryx[titanic$Age < 14] <-1titanic$personCategoryx[titanic$Age > 13 & titanic$Sex=="female"] <-2titanic$personCategoryx[titanic$Age > 13 & titanic$Sex=="male"] <-3titanic$personCategory <- factor(titanic$personCategoryx, labels=c("children", "women", "men"))personCategoryz <- na.omit(titanic$personCategory)summary(personCategoryz)ggplot(titanic) + geom_bar(aes(x=personCategoryz, fill=factor(Survived)))How would I go about fixing this? Thanks! 解决方案 The problem occurs because personCategoryz is not the same length as titanic. In any case it is generally not a good idea to use vectors with ggplot which are not columns in the data frame. What you can do, is to shorten the whole data.frame such that you look only at complete cases:titanic2 <- na.omit(titanic)ggplot(titanic2) + geom_bar(aes(x=personCategory, fill=factor(Survived))) 这篇关于错误:美学必须是长度为1,或者与dataProblems:personCategoryz的长度相同时省略NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-05 21:01