A Fun and Easy Way to Try R

Data wonks have more fun than you may think. If you have not yet begun working with the R statistical program (which is mesmerizing, extremely powerful, hard to learn, but weirdly intuitive, and FREE) then here is a fun and relatively easy way to give it a test drive.

It is becoming a tradition that, at the end of every year, data wonks share holiday greetings with R code. They write elegant little snippets of programming syntax that generate picture and greetings in R, like Christmas trees. It helps demonstrate some of R’s amazing capabilities in fun, engaging ways. And it gives others a way to jump in and start manipulating code for themselves.

Ready to try it? Here is what you need to do:

  1. If you do not have it yet, get R. It’s free. It will download and install onto your machine very quickly. To keep things simple, don’t bother yet with an interface tool, like R Studio or R Commander. Once you start working more with R, these are is useful tools. But for now, all you need is the engine.
  1. Start R. Just power it up and look for the prompt. It looks like an old-fashioned DOS prompt.
  1. You’ll need the ggplot2 package for this. So, do this at the prompt:

> install.packages(“ggplot2”)

  1. Copy the following R code, and paste it at the prompt:
rm(list = ls())
library(ggplot2)
# create data
x <- c(8,7,6,7,6,5,6,5,4,5,4,3,4,3,2,3,2,1,0.5,0.1)
dat1 <- data.frame(x1 = 1:length(x), x2 = x)
dat2 <- data.frame(x1 = 1:length(x), x2 = -x)
dat1$xvar <- dat2$xvar <- NA
dat1$yvar <- dat2$yvar <- NA
dat1$siz <- dat2$siz <- NA
dat1$col <- dat2$col <- NA

# set threshold for christmas balls
dec_threshold = -0.5

# create random places, sizes and colors for christmas balls
set.seed(2512)
for (row in 1:nrow(dat1)){
if (rnorm(1) > dec_threshold){
dat1$xvar[row] <- row
dat1$yvar[row] <- sample(1:dat1$x2[row]-1,1)
dat1$siz[row] <- runif(1,0.5,1.5)
dat1$col[row] <- sample(1:5, 1)
}
if (rnorm(1) > dec_threshold){
dat2$xvar[row] <- row
dat2$yvar[row] <- sample(1:dat2$x2[row],1)
dat2$siz[row] <- runif(1,0.5,1.5)
dat2$col[row] <- sample(1:5, 1)
}
}
# plot the christmas tree
ggplot() +
geom_bar(data = dat1, aes(x=x1, y=x2),stat = "identity", fill = '#31a354') +
geom_bar(data = dat2, aes(x=x1, y=x2),stat = "identity", fill = '#31a354') +
geom_point(data = dat1,aes(x = xvar, y = yvar, size = siz, colour = as.factor(col)) ) +
geom_point(data = dat2,aes(x = xvar, y = yvar, size = siz, colour = as.factor(col)) ) +
coord_flip() + theme_minimal()+ theme(legend.position="none",
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())
  1. Hit return, and enjoy the output!

(Credit: This code is from the Analytics Lab in the Netherlands. Thanks to Jeroen Kromme for posting this holiday greeting with its code.)

By Joe Hopper, Ph.D.

Versta Research Website

More articles from Versta Research

Tags: , ,

Comments are closed.