STAT545-UBC / Discussion

Public discussion
38 stars 20 forks source link

Use of ~ in facet_wrap but not elsewhere? #351

Open nivretta opened 8 years ago

nivretta commented 8 years ago

Two related questions:

1- Why do we have to use the '~' symbol when using facet_wrap, as in class from 9/27, but it's not necessary in other expressions like geom_point? (I excluded the symbol and kept getting an 'object not found' error.)

2- How would I find the answer to this question on Google? This wasn't helpful, since I think this is a basic programming language question.

oganm commented 8 years ago

Unlike other ggplot functions facet_wrap takes in a formula object as its input. That is what you define by adding the ~. While in facet_wrap, the fact that it is a formula doesn't change much, if you use facet_grid the structure of the formula is used to choose which variable separates the rows and which one separates the columns. Try

d <- ggplot(diamonds, aes(carat, price)) +
  xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1)
d + facet_grid(color~cut)