error: Aesthetics must either be length one, or the same length as the dataProblems:personCategoryz when omitting NA

流过昼夜 提交于 2019-12-10 10:38:37

问题


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] <-1 
titanic$personCategoryx[titanic$Age > 13 & titanic$Sex=="female"] <-2
titanic$personCategoryx[titanic$Age > 13 & titanic$Sex=="male"] <-3
titanic$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] <-1 
titanic$personCategoryx[titanic$Age > 13 & titanic$Sex=="female"] <-2
titanic$personCategoryx[titanic$Age > 13 & titanic$Sex=="male"] <-3
titanic$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!


回答1:


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


来源:https://stackoverflow.com/questions/26511317/error-aesthetics-must-either-be-length-one-or-the-same-length-as-the-dataprobl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!