Little silly fun with R
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
.
require(ggplot2) require(jpeg) require(grid) require(gridExtra) require(dplyr) require(tidyr) require(cowplot) #ggthemes Excel theme colors sigh_colors
Executing this function we get a random background image and bonus: Excel 95 color scheme and turned axis labels.
sighr()
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.
library(readr) library(lubridate) library(dplyr) library(tidyr) library(magick)
Donwload all the data
##download CO2 emissions data mo2Num co2ppm global_temp_l % gather(key = month, value = temp, -Year) global_temp_l
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) dev.off()
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.
bkgrnd_plot
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
library(magick) library(dplyr) library(magrittr) library(httr) library(jsonlite) library(stringr)
Setup your mashape key
hd_key
Create a function to wrap the API call
api_text
Create the meme
This function will change the font type to Impact and adds the quote to a random cat photo from the site thecatapi.com. By default, the function will use Ron Swanson quote.
cat_meme
Let’s try it:
cat_meme()
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 www.mashape.com
- gets a random image from unsplash.com
- 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
library(twitteR) library(stringr)
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)
nand_logo
Create the quote function
motivational_qt
Let’s try the function
motivational_qt() ## [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")
R
– but please, please use all of this very responsibly.The post Little silly fun with R appeared first on nandeshwar.info.
R-bloggers.com 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.