When you’ve written the same code 3 times, write a function When you’ve given the same in-person advice 3 times, write a blog post — David Robinson (@drob) November 9, 2017 Fun fact: This tweet is actually what inspired me to start this website. Secondly, he’s right, and we should definitely all be writing functions for bits and pieces of code that we are using over and over again.
I recently figured out how to do something that I’ve been trying to do for a while: Plot geom_sf objects (spatial points, lines, polygons, etc) onto ggmaps (i.e. basically a google map background), in such a way that everything easily lines up properly. I won’t get into the nitty gritty and ins and outs of all of the components here, or else we’ll be here all day. Instead, I’ll just quickly go through an example and show what worked for me.
Have you ever wanted to work on two (or even more) different, separate things in R, at the same time? Do you crave some order and organization in your R life? Yes? Then you need to start using RStudio projects! (Side note: I assume that this is all pretty much the same on Windows as it is on Mac OS, but because using Windows makes me want to remove my own brain with a butter knife, I’m not sure… proceed with caution, Windows users.
Loops are control flow statemtents that repeat the same code for a certain number of iterations. So, if you want to do the same thing over and over again, a loop might be exactly what you need. Loops often have a bad reputation among coders because they can be relatively slow and can also be confusing, especially compared to functionals (ex. apply() functions).1 But I can’t deny it, I really love loops.
I recently gave a talk in which I wanted to show how the overlap between mother and daughter orangutans’ home ranges changed over time. I’d been itching to play around with the gganimate package (created by Thomas Lin Pedersen; his twitter, his github), and it seemed like the perfect opportunity. If you’re already familiar with ggplot2, then gganimate is a breeze. I was really happy with my final result, and figured it’s probably worth a post.
- OLDER POSTS
- page 1 of 3