error: Aesthetics must either be length one, or the same length as the dataProblems:personCategoryz when omitting NA
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)))
Admin
Updated on July 09, 2022Comments
-
Admin almost 2 years
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!