Successful coding with R is not magic!


Hey folks,

I have long since given up trying to anticipate what types of videos will resonate with people on YouTube. One of my most popular videos shows people how to make stacked bar plots. Throughout it, I tell people that these are a horrible way to visualize data. It’s my third most viewed video. I thought a video on slope plots would be popular. Nope. People panned last week’s episode. But Venn diagrams - holy cats! People are really geeking out about this week’s episodes on Venn diagrams. Who knows. All I can do is keep putting out videos that I think are fun to make and that I think have potential to help you all learn to use {ggplot2} and related packages to make compelling figures.

Honestly, I’m still surprised people watch the videos at all. Thank you.

Can you tell me the last time you pronounced “abracdabra!”, squeezed a good luck charm, hit “Run” at just the right angle and hoped that your R code gave you what you were expecting? I’d love it if you emailed me where you find yourself doing this the most.

A few weeks ago, I was working with some people on my floor doing a mob programming exercise. I really find these activities really give me a lot of energy and enthusiasm for R. Participants let their guards down a bit and are willing to be honest about what they do and don’t know. Because others in the room are in a similar position, it often results in people being willing to make themselves more vulnerable. Everyone has a great sense of humor about their successes and failures.

I was struck by a comment one participant made. It was to the effect that “my strategy is to try a bunch of things and hope that some type of magic happens”. I see this a lot when I’m teaching workshops or when I’m looking at R code from people in my research group. People will try everything and hope for the best. When it works, they aren’t really sure why it worked. When it doesn’t work, they aren’t really sure why it didn’t work.

What is magic? According to Merriam-Webster, magic is “the use of means (such as charms or spells) believed to have supernatural power over natural forces”. In other words, we effectively shout, “Abracadabra!”, click “Run”, and hope for our desired plot. Or at least no error messages.

Trust me on this: there is no magic in R. If something seems “magical” to you about R, I encourage you to dig deep and figure out why something works or doesn’t work. There are a couple of reasons I make this suggestion.

First, when you rely on magic, you aren’t learning anything. If you get an error or warning message, I want you to either modify the code to remove the message or be able to tell your neighbor why you are ok with leaving the warning. I try to demonstrate this in my videos by removing all possible warning messages by cleaning up the logic of my code. This week, you may have noticed I let a warning message go. I was using scale_alpha_discrete(). I got the message, “Using alpha for a discrete variable is not advised.” I let it go because I understood what it was saying. Alpha is a continuous parameter between zero and one. I was using it to apply transparency to the top circle of my Venn diagram. I knew what I was doing.

Second, when you rely on magic, you can’t be confident that what you are doing is actually correct. A lot of code I look at shows signs of someone who has “hacked” their code to bend to their will. It starts looking pretty ugly. A sign of this is when someone uses three techniques to do the same thing in their code. For example, they may use a for loop, mapply(), and map() within lines of each other. Why? Again, talking through your code and explaining why an approach is taken can help to resolve these problems. But if you can’t get map() to work, but you can get mapply() to work, you really owe it to yourself to go back and figure out what you were missing in the syntax or logic. I’ve seen people hack together functions like these only to show them that what they thought was going on wasn’t. They were covering up a problem that gave an error with another problem that didn’t give an error.

Ultimately, accepting magic is like accepting a “black box”. We don’t know how it works, but we know it works and that’s good enough for us. As a personal example, in last week’s videos I used functions from {ggrepel}. This function allows you to place text in a plot so that the text doesn’t overlap other text or geoms. How does it work? Magic? To some degree I’m willing to accept that as an answer. But what if a label isn’t moving where I think it should? What if a label moves every time I run the command? How could I better understand how the arguments worked to better place a label? By moving beyond the magic. At a minimum I could read the documentation and look at other examples. I could look at the package’s “Related Work” page and read more documentation about the algorithms the package implements.

The lesson I want to leave you with is to resist being content with “magic” as an answer. If you peel back the layer of magic each time “abracadabra!” gets you somewhere, you’ll be a far more confident programmer. That’s what I want for myself, the people in my research group, and for you.

Here’s a bit of homework: see if you can explain the difference between setting the x-axis (or y-axis) limits using scale_x|y_continuous(), limits(), or coord_cartesian(). See if you can write example code to highlight what each function does differently or the same.

Workshops

I'm pleased to be able to offer you one of three recent workshops! With each you'll get access to 18 hours of video content, my code, and other materials. Click the buttons below to learn more

In case you missed it…

Here are some videos that I published this week that relate to previous content from these newsletters. Enjoy!

video previewvideo preview

Finally, if you would like to support the Riffomonas project financially, please consider becoming a patron through Patreon! There are multiple tiers and fun gifts for each. By no means do I expect people to become patrons, but if you need to be asked, there you go :)

I’ll talk to you more next week!

Pat

Riffomonas Professional Development

Read more from Riffomonas Professional Development

In case you missed it, I have nine kids ranging in age from 23 to 7 that my wife homeschools. They’re a riot. Each of them has to find a way to be different from all of the others. This makes for some real characters. Let me introduce you to Peter. This week, Peter, who is 11, has been working on a times table. You may remember these from when you were a kid. Say you want to know what 7 times 8 is (this was always my hardest “times” to remember). You take your finger down the rows to the...

Hey folks, I’m really enjoying sharing with you my 30,000 foot view of how I would go about making figures that I find in the “wild”. Following up on these emails with a couple of related YouTube videos has been a lot of fun for me. Of course if you find any figures you like, send them my way - I love seeing what interests you all. I was reminded recently though that not everyone feels enough confidence with their R and tidyverse skills to keep up. Sorry! Towards the bottom of this email I...

Hey folks, We’re still slogging our way through Thanksgiving leftovers. As time passes from last Thursday, there’s a fine line between setting a good example about not wasting food and setting a bad example by getting every food poisoning! Speaking of eating, our teeth are pretty important, don’t you think? In the US, Trump’s expected head for the Department of Health and Human Services has a number of interesting views about health. One example is that its a bad idea to spike our drinking...