How to Create a Dazzlingly Nerdy Valentine’s Day Card Using RMarkdown and ggplot2 in RStudio

Creating a Heart Using a geom_line() Argument

I made a Valentine’s Day Card! Before you click/tap away, there’s more to it.

Background

It’s easy enough to make a card by hand or with a word processor and some images. For that matter, you can even buy a pre-made card with ease. But what happens when you’re a nerd and know that your significant other would appreciate a fabulously nerdy card you made yourself?

I found myself in that position and went on a wild chase to create a card in HTML and PDF form using ggplot2 to generate images and RMarkdown to develop the cards themselves. I also learned about the here package. Here’s the journey!

About a month ago, my wife asked if I would be doing something nerdy for her Valentine’s Day gift. Being the dutiful husband that I am, I got to work two weeks beforehand. The whole project took between 6 and 15 hours spread across that time (imprecise tracking) because of life obligations.

Sometime while I was an undergrad studying physics at North Carolina A&T State University, the math department put on a math-themed event that included love equations. That served as my inspiration. I decided I wanted to graph functions to create hearts and spell a message. I chose R in RStudio because that is where I am currently specializing my visualization skills.

Fourier Transforms will be something I’ll try in the future. I also need to figure out how to get multiple y values to show for a given x value in ggplot2. I’ll get there eventually, but it was more work than I wanted for this round.

Process

I found a few heart-shaped equations (yes, I know they aren’t anatomically correct) here. Unlike Mathematica, I can’t plot mathematical functions in R. The workaround is to plug the function in and calculate it incrementally to plot each point:

# Theta values
t <- seq(-pi,pi,0.01)


# X Values
x <- 16*0.25*(3*sin(t) - sin(3*t))


# Y Values
y <- 13*cos(t) - 5*cos(2*t) - cos(4*t)

I used Polar Coordinates for the heart shapes because Cartesian Coordinates didn’t work correctly. I wanted to center the result around the origin but ran into time constraint issues, leaving that for a future iteration. I made the letter I using error bars and the letter O using a circle geom from the ggforce package. You can find the code I used for each letter here.

After I made the images, it was time to use RMarkdown! I first heard about RMarkdown several months ago, but this was my first time using it. There was a learning curve, but it wasn’t too terrible. HTML output is the easiest, but PDF output is doable — with a lot of LaTeX tweaking. I still didn’t get the exact result I wanted, but applying the Pareto Principle, I decided it was close enough. I found and used the here package to reference my file structure since RMarkdown doesn’t see the global environment the same way as the rest of RStudio (or at least a script file opened in RStudio).

Conclusion

This exciting project allowed me to make a nerdy Valentine’s Day card for my wife and learn a lot in the process! You can find my sources listed here and my entire GitHub Project here. If you have any suggestions for improvements, I’d love to hear them in the comment section below!