3 minute read

[caption id=”attachment_427” align=”aligncenter” width=”740”]convincing Fig: Convincing (Munroe 2011b).[/caption]

First, what is XKCD? Well, the website describes itself as follows:

"Stick-figure strip featuring humour about technology, science, mathematics and relationships, by Randall Munroe."

It brought me laughter many times ever since I knew its existence. There's even a subreddit on Reddit of XKCD! Just recently, I realized the existence of this fantastic R package also called "xkcd" which makes serious R plots into super cute ones! That's worth a new post, so the following will basically be reproducing from the R package tutorial with my own tiny edits to give you some visual pleasure :P

Note that for the R package "xkcd" to take effect, you need first check whether you have the "xkcd" font installed on your computer. Ok, some of you may know that you can produce such graphs in python and latex, but it's actually not that hard to do it in R as well. So I basically tweaked the original code in this tutorial a little bit, and generated the following plot:


And just one more for a belated Happy Mother's day...


With code attached:

data = read.csv("mommy.csv", header = T, stringsAsFactors = F)
data$xmin <- data$hour - 0.25
data$xmax <- data$xmin + 1
data$ymin <- 0
data$ymax <- data$number
xrange <- range(8, 24)
yrange <- range(min(data$ymin) + 10 , max(data$ymax) + 200)
ratioxy <- diff(xrange)/diff(yrange)
timelabel <-  function(text,x,y) {
  te1 <- annotate("text", x=x, y = y + 65, label=text, size = 6,family ="xkcd")
       xkcdline(aes(xbegin=xbegin, ybegin= ybegin, xend=xend,yend=yend),
                data.frame(xbegin=x,ybegin= y + 50, xend=x,yend=y), xjitteramount = 0.5))
n <- 1800
x <- runif(n, xrange[1],xrange[2] )
y <- runif(n, yrange[1],yrange[2] )
inside <- unlist(lapply(1:n, function(i) any(data$xmin <= x[i] & x[i] < data$xmax &
                                               data$ymin <= y[i] & y[i] < data$ymax)))
x <- x[inside]
y <- y[inside]
nman <- length(x)
sizer <- round(runif(nman, 1, 10),0)
angler <- runif(nman, -10,10)

p <- ggplot() +
  geom_text(aes(x,y,label="Mummy",angle=angler,hjust=0, vjust=0),
            family="xkcd",size=sizer,alpha=0.3) +
  xkcdaxis(xrange,yrange) +
  annotate("text", x=16, y = 650,
           label="Happy Mother's day", size = 16,family ="xkcd", color="pink") +
  xlab("daily schedule") +
  ylab("Number of times my mom used to called by me") +
  timelabel("Wake up", 9, 125) +
  timelabel("School", 12.5, 90) +
  timelabel("TV time", 15, 130) +
  timelabel("Homework", 18, 525) +
  timelabel("Dinner", 21, 110) +
  timelabel("tuck me in", 23.5, 60)

It's pretty easy to generate the data yourself, and feed that into the above code. Voila shows the plot!

Hmm... Can't help but wonder when will I be able to use ggplot with The Oatmeal theme.

Just a bit of a discursion at the end, definitely another boon to all of us! Now that plotting in R tickled my fancy, I discovered also that there is an R package called "quantmod", which does a very good job in financial plots. For example, just no more than five lines of code and we can see how Apple (the tech company of course...) is doing recently, which I found to be quite impressive. The code for generating the following plot is as follows: