[R] How to understand the mentality behind tidyverse and ggplot2?
Duncan Murdoch
murdoch@dunc@n @end|ng |rom gm@||@com
Wed Nov 18 21:38:53 CET 2020
On 17/11/2020 12:43 p.m., C W wrote:
> Dear R list,
>
> I am an old-school R user. I use apply(), with(), and which() in base
> package instead of filter(), select(), separate() in Tidyverse. The idea of
> pipeline (i.e. %>%) my code was foreign to me for a while. It makes the
> code shorter, but sometimes less readable?
Think of the pipe as pure syntactic sugar. It doesn't really do
anything, it just lets you write "f(g(x))" as "x %>% g() %>% f()" (where
the parens "()" are optional). Read it as "Take x and pass it to g();
take the result and pass it to f()", which is exactly how you'd read
"f(g(x))". The pipe presents it in the same order as in English, which
sometimes makes it a bit easier to read than the mathematical notation.
There's a lot more to tidyverse ideas besides the pipe. The overview is
in the "Tidyverse Manifesto" (a vignette in the tidyverse package), and
details are in Grolemund and Wickham's book "R for Data Science".
>
> With ggplot2, I just don't understand how it is organized. Take this code:
ggplot2 is much harder to understand, but Wickham's book "ggplot2:
Elegant Graphics for Data Analysis" gives a really readable yet thorough
description.
>
>> ggplot(diamonds, aes(x=carat, y=price)) + geom_point(aes(color=cut)) +
> geom_smooth()
>
> There are three plus signs. How do you know when to "add" and what to
> "add"? I've seen more plus signs.
>
> To me, aes() stands for aesthetic, meaning looks. So, anything related to
> looks like points and smooth should be in aes(). Apparently, it's not the
> case.
Yes "aesthetic" was a really bad choice of word.
> So, how does ggplot2 work? Could someone explain this for an old-school R
> user?
Not in one email, but hopefully the references (which are both available
online for free, or in a bookstore at some cost) can help.
Duncan Murdoch
More information about the R-help
mailing list