Little silly fun with R

[This article was first published on R –, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Yes, I know. I’m a hypocrite to be creating ‘interesting’ stuff using R. If you’ll excuse me just this one time, you’ll see some silly fun using R. It is an experiment to see how much we can extend R as well as learning new packages.

Add a background image to a plot

I don’t know when you would need to use this, but if you ever find a need to add a background image in a ggplot, you can use this following function called sighr.


#ggthemes Excel theme colors

Executing this function we get a random background image and bonus: Excel 95 color scheme and turned axis labels.


If you pass the additional argument sighmore = TRUE, you get a table attached with the plot.

sighr(sighmore = TRUE)

Create a gif

GIFs actually could be useful depending on your use. Here, I wanted to combine a chart with carbon emissions data and global temperature change with a “I-don’t-believe-it” gif.

Here we go.

First, let’s load all the good libraries.


Donwload all the data

##download CO2 emissions data
global_temp_l % gather(key = month, value = temp, -Year)

Plot the data

You will see I’m not using ggplot here. I had a difficult time getting two y-axis going. base-R graphics to rescue.

png(filename = "co2_temp_plot.png", width = 5, height = 3.5, units = "in", res = 200)
par(mar = rep(3, 4), las = 1)
with(temp_co2ppm, plot(x = date, y = temp, type = 'l', col = "grey80", axes = FALSE, xlab = NA, ylab = NA, ylim = c(-.4, 1.4)))
axis(side = 4, at = seq(from = -.40, to = 1.40, length.out = 6), tick = FALSE, hadj = 1, line = 1, col.axis = "grey80")
par(new = TRUE)
with(temp_co2ppm, plot(x = date, y = interpolated, type = 'l', col = "red", axes = FALSE, ylim = c(300, 400)))
abline(h = seq(from = 300, to = 400, length.out = 6), col = "grey95")
axis(side = 2, at = seq(from = 300, to = 400, length.out = 6), tick = FALSE, line = 1, hadj = 0, col.axis = "red")
axis.Date(side = 1, at = seq.Date(from = min(temp_co2ppm$date), to = max(temp_co2ppm$date), length.out = 6), 
          lwd = 0, line = -1, format = "%Y", lwd.ticks = 0.5, col.ticks = "grey95")
mtext(expression(""*CO[2]*" ppm"), side = 2, col = "red", at = 410, adj = 0, line = 2)
mtext(expression("Global temp deviation from mean"~degree~C), side = 4, col = "grey80", at = 410, adj = 1, line = 2)

Read the plot, gif, and combine

Using the magick library, we can read the saved plot as well as the gif and write a new combined gif.


climate change deniers be like

Generate cat memes

This was truly fun and I was surprised by the results. Some were really on point. To make this code work, you will need to get an API key from [. This function downloads a random quote based on your select: Chuck Norris, Ron Swanson, or a motivational quote.

Load all goodies


Setup your mashape key


Create a function to wrap the API call


Create the meme

This function will change the font type to Impact and adds the quote to a random cat photo from the site By default, the function will use Ron Swanson quote.


Let’s try it:


With Chuck Norris this time:

cat_meme(which_quote = 3)

How about a motivational quote:

cat_meme(which_quote = 2)

Create a motivational quote Twitter bot

The cat meme function gave me the idea to create a Twitter bot that:

  • pulls a motivational quote from
  • gets a random image from
  • blurs the image
  • combines the image and the quote
  • posts the combined image to Twitter

Of course, for this to succeed, you need a Twitter account and a Twitter API key and token.

Load the twitteR library


Setup Twitter authentication

setup_twitter_oauth(consumer_key = "your-consumer_key", 
                    consumer_secret = "your-consumer_secret",
                    access_token = "your-access_token",
                    access_secret = "your-access_secret")

Read the logo (optional)


Create the quote function


Let’s try the function

## [1] ""The artist is nothing without the gift,\nbut the gift is nothing without work.\n - Emile Zola""

If everything worked, this function should return a quote and the image should be saved locally.

Update your Twitter status

This is the easiest part. Just get the text from the motivational_qt() function and the image and send it to Twitter. Please use caution and don’t spam.

updateStatus(text = str_sub(paste0("QuoteBot courtesy @n_ashutosh @opencpu @unsplash @geoffjentry ",
                                   str_replace(motivational_qt(), pattern = "\n", replacement = " ")),
                            end = 140),
             mediaPath = "quote_text.png")
That’s it. I hope you enjoy having some fun with R – but please, please use all of this very responsibly.

The post Little silly fun with R appeared first on

To leave a comment for the author, please follow the link and comment on their blog: R – offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)