From fc99e2d6335dc5e03e1364c2abd3637d74518d31 Mon Sep 17 00:00:00 2001 From: hadley Date: Wed, 20 Jul 2016 08:47:17 -0500 Subject: [PATCH] Discuss ggplot2 call transition --- EDA.Rmd | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/EDA.Rmd b/EDA.Rmd index 3dd884d..f750414 100644 --- a/EDA.Rmd +++ b/EDA.Rmd @@ -554,3 +554,28 @@ ggplot(data = diamonds2, mapping = aes(x = cut, y = resid)) + ``` Modelling is important because once you have recognised a pattern, a model allows you to make that pattern quantitative and precise, and partition it out from what remains. That supports a powerful interative approach where you indentify a pattern with visualisation, then subtract with a model, allowing you to see the subtler trends that remain. I deliberately chose not to teach modelling yet, because understanding what models are and how they work are easiest once you have some other tools in hand: data wrangling, and programming. + +## ggplot2 calls + +As we move on from these introductory chapters, we'll transition to a more concise expression of ggplot2 code. So far we've been very explicit, which is helpful when you are learning: + +```{r, eval = FALSE} +ggplot(data = faithful, mapping = aes(x = eruptions)) + + geom_histogram(binwidth = 0.25) +``` + +But the first couple of arguments to a function are typically so important that you should know them by heart. The first two arguments to `ggplot()` are `data` and `mapping`, and the first two arguments to `aes()` are `x` and `y`. In the remainder of the book, we won't supply those names. That saves typing and by reducing the amount of boilerplate makes it easier to see what's different between plots (that's a rely important programming concern that we'll come back in [functions]). + +```{r, eval = FALSE} +ggplot(faithful, aes(eruptions)) + + geom_histogram(binwidth = 0.25) +``` + +Sometimes we'll turn the end of pipeline of data transformation into a plot. Watch for the transition from `%>%` to `+`. I wish this transition wasn't necessary but unfortunately ggplot2 was created before the pipe was discovered. + +```{r, eval = FALSE} +diamonds %>% + count(cut, clarity) %>% + ggplot(aes(clarity, cut, fill = n)) + + geom_tile() +```