CODEX

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

David Vermillion
Feb 17 · 3 min read
A pink heart outline created through a sinusoidal function
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!

Originally published at https://davidmvermillion.com on February 17, 2021.

CodeX

Everything connected with Tech & Code

David Vermillion

Written by

Telling stories with data to accomplish mission objectives, improve outcomes, increase efficiencies, and boost profitability

CodeX

Everything connected with Tech & Code

David Vermillion

Written by

Telling stories with data to accomplish mission objectives, improve outcomes, increase efficiencies, and boost profitability

CodeX

Everything connected with Tech & Code

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store