-2

1.

 ggplot(data.combined[1:891,], aes(title[1:891], fill=as.factor(Survived[1:891])))+
   geom_bar(width = .5) +
   facet_wrap(~Pclass+Sex)

2.

    ggplot(data.combined[1:891,], aes(dataframe$title[1:891], fill=as.factor(dataframe$Survived[1:891]))) +
      geom_bar(width = .5) +
      facet_wrap(~Pclass+Sex)

Why do I get different results? How is the $ used?

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
Vidit
  • 1
  • 1
    Please include example data and see [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to make a good question – Steve Bronder Aug 11 '16 at 17:12
  • You rarely need to use $ in ggplot. In the first code, you should omit the [1:891] in the `aes` – Richard Telford Aug 11 '16 at 17:13
  • with `$` you specify a column in your data set. For example `mydata$col1` selects column `col1` from the `mydata` data set. – mask Aug 11 '16 at 17:15

1 Answers1

1

Before answering your question, when using ggplot you don't need to subset the data after calling it in ggplot()

For number one:

ggplot(data.combined[1:891,], aes(title, fill=as.factor(Survived)))+ 
  geom_bar(width = .5) + facet_wrap(~Pclass+Sex)

This is because you are referencing the names of the columns in your data frame from the object in ggplot(data.combined[1:891,]).

If you don't do that, ggplot will take the columns you are referencing from specific data frame (the same one in this case) like you do in aes(dataframe$title[1:891], fill=as.factor(dataframe$Survived[1:891])))

In the second example you are referencing data.combined in ggplot() and dataframe in aes().

cylondude
  • 1,816
  • 1
  • 22
  • 55