2

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!

Izzy
  • 6,740
  • 7
  • 40
  • 84
antsy
  • 21
  • 1
  • 1
  • 3
  • Could you provide the data? I have a couple of `titanic`datasets in different `R` packages, but I am not sure to which you are refering? – thothal Oct 22 '14 at 16:02
  • You remove the `NA` from `personCategory` but not from `Survived`. You likely want to make a new dataset without the missing category values instead. Something like `titanic2 = subset(titanic, !is.na(personCategory))`. – aosmith Oct 22 '14 at 16:08
  • 2
    possible duplicate of [Aesthetics must either be length one, or the same length as the dataProblems](http://stackoverflow.com/questions/20057452/aesthetics-must-either-be-length-one-or-the-same-length-as-the-dataproblems) – zx8754 Sep 25 '15 at 10:26

1 Answers1

4

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)))
thothal
  • 16,690
  • 3
  • 36
  • 71