Writing R functions that rely on tidyverse functionality can be tricky. In this thread, I will share screenshots of a handout I put together on writing such functions using a common use case: plotting histograms. #rstats
For any R function that uses tidyverse functionality inside its belly, the first crucial question is:

Will the function use the entire dataset AND one or more variable names as arguments?
The second crucial question is:

When we apply the function to a particular dataset and to particular variable names pulled from that dataset, will we want to use quotes around those variable names or not? (The dataset will NOT need quotes around its name.)
How we answer these two crucial questions will dictate how we write our histogram function. The attached screenshot illustrates the difference in approach based on (1) whether we will use quotes around a variable name or (2) we will NOT use quotes around a variable name.
If we use quotes around a variable name when we apply/call the function, we have to reference that variable as .data[[var]] inside the belly of the function when we define the function.
If we DO NOT use quotes around a variable name when we apply/call the function, we have to reference that variable as {{var}} inside the belly of the function when we define the function.
Once we understand these basic rules, it becomes easy to add additional variable names to our function as input arguments. The screenshot below illustrates how we can add a grouping variable that would allow you to plot a histogram separately for each of several groups.
Grouping will overlay the histograms for different groups on top of each other and display the result in a single graphical window. That works well for a small number of groups but for larger groups facetting will be more useful as each group will get its own graphical panel.
The attached screenshot shows how facetting can be incorporated into our function definition and application/call. Throughout, I used the penguins dataset from the palmerpenguins package to illustrate the code.
Facetting requires a bit more care than grouping when specifying what input variable from the input dataset we need to group by.
I hope this is useful - to understand more about why we need to write R functions this way when they include tidyverse functionality, please refer to this excellent YouTube video on Programming with Tidyverse functions by @TiffanyTimbers:
This thread was inspired by @TiffanyTimbers's video and by an Intermediate R Course I just finished teaching for the Columbia Mountains Institute of Applied Ecology (cmiae.org), where we covered writing R functions. Let me know if you find the thread helpful.
I should clarify that, in this thread, I used the Freedman-Diaconis rule for choosing the number of histogram binwidths. According to @robjhyndman, this rule is "robust and works well in practice": stats.stackexchange.com/questions/798/….

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Isabella R. Ghement

Isabella R. Ghement Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @IsabellaGhement

25 Oct
Comfort food - shrimp, veggies and polenta.
This is the polenta!
The dessert: 👇
Read 4 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(