library(knitr) opts_chunk$set(fig.width = 5, fig.height = 5, message = FALSE) set.seed(2015-04-01)
The ggfreehand package allows one to add freehand red circles to a plot. This can improve many plots, but most importantly those that are to be posted on the Stack Exchange network.
For example, we'll use the stackr package to download all the answers from about a Stack Overflow user (me):
library(stackr) answers <- stack_users(712603, "answers", num_pages = 10, pagesize = 100) library(dplyr) answers <- tbl_df(answers)
If you'd rather not use the stackr package, note that the ggfreehand
package provides this data (as of 4/1/15) as a built-in dataset:
library(ggfreehand) data(answers)
Suppose we are interested in number of answers per month, much as I analyze here. We could find the number of answers per month like so:
library(dplyr) library(lubridate) library(ggplot2) answers_per_month <- answers %>% mutate(month = round_date(creation_date, "month")) %>% count(month)
This lets us use ggplot2 to create a graph of answers per month over time:
library(ggplot2) ggplot(answers_per_month, aes(month, n)) + geom_line()
User wooble noted a critical problem with this graph in this comment:
-1; needs more freehand red circles on the graphs.
This is indeed a huge problem! As described here in the "Many Memes of Meta", the freehand red circle is a long-standing tradition of Meta, one that has been scientifically proven to draw the user's attention more effectively than any other shape [citation needed].
Unfortunately ggplot2 doesn't provide a layer for adding freehand circles, but the ggfreehand
package does! Let's say we want to circle the two months in which I answered the most questions.
top_2 <- answers_per_month %>% top_n(2, n) top_2
We can now add those as freehand circles with the geom_freehand
layer.
ggplot(answers_per_month, aes(month, n)) + geom_line() + geom_freehand(data = top_2)
This is much more worthy of being posted on Meta Stack Overflow. If those circles are too messy or too neat for you, you can turn down or up the noisiness
parameter:
ggplot(answers_per_month, aes(month, n)) + geom_line() + geom_freehand(data = top_2, noisiness = 2) ggplot(answers_per_month, aes(month, n)) + geom_line() + geom_freehand(data = top_2, noisiness = 10)
You can also make the circle bigger or smaller. (The result is normalized such that a radius of 1 causes the freehand circle to take up 1/25 of the plot's height and width).
ggplot(answers_per_month, aes(month, n)) + geom_line() + geom_freehand(data = top_2, radius = 3) ggplot(answers_per_month, aes(month, n)) + geom_line() + geom_freehand(data = top_2, radius = .2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.