sun, 11-jan-2015, 10:34

I spent some time this weekend playing with a couple interesting new R packages that should help with some of the difficulty manipulating data with the base packages. Getting data into a format appropriate for plotting or running statistical models often seems to take more time than anything else, and the process can be very frustrating because of seemingly non-sensical error messages from R.

R guru Hadley Wickham has written some of the best packages for data manipulation (reshape2, plyr) and plotting (ggplot2). He’s got a new pair of packages (tidyr and dplyr) and a theory of getting data into the proper format (http://vita.had.co.nz/papers/tidy-data.pdf) that look very promising.

A couple other tools I wanted to look at include a new way of piping data from one operation to another (magrittr), which is a basic part of the Unix philosophy of having many tools that do one thing well and stringing them together to do neat things, and an interactive graphic package, ggvis, that should be really good for data investigation.

For this investigation, I’m looking at the names of our dogs, past and present, and how popular they are as people names in the United States. This data comes from the Social Security Administration and is available in the R package babynames.

install.packages("babynames")
library(babynames)
head(babynames)
Source: local data frame [6 x 5]

year sex      name    n       prop
1 1880   F      Mary 7065 0.07238359
2 1880   F      Anna 2604 0.02667896
3 1880   F      Emma 2003 0.02052149
4 1880   F Elizabeth 1939 0.01986579
5 1880   F    Minnie 1746 0.01788843
6 1880   F  Margaret 1578 0.01616720

The data has the number of registrations (n) and proportion of total registrations (prop) for each year from 1880 through 2013 for all name and sex combinations.

What I want to see is how popular out dog names are. All of our dogs have been adopted, but some we chose names for them (Nika, Piper, Kiva), and the rest came with names we didn’t change (Deuce, Buddy, Koidern, Lennier, Martin, Monte and the second Piper). Dog mushers often choose a theme for a litter of puppies, which accounts for some of the unusual names (Deuce came from a litter of classic cars, Lennier came from a litter of Babylon 5 character names, Koidern from a litter of Yukon River tributary names).

So what I want to do is subset the babynames database to just the names of our dogs, combine male and female names together, and plot the popularity of these names over time.

Here’s how that’s done using the magrittr pipe operator (%>%):

library(dplyr)
library(magrittr)

dog_names <- babynames %>%
   filter(name %in% c("Nika", "Piper", "Buddy", "Koidern", "Deuce",
                     "Kiva", "Lennier", "Martin", "Monte")) %>%
   group_by(year, name) %>%
   summarise(prop=sum(prop)) %>%
   transform(name=factor(name)) %>%
   ungroup() %>%
   arrange(name, year)

We are assigning the result of all the pipes to dog_names. In order, we take the babynames data set and filter it by our dog’s names. Then we group by year and name, and summarize the proportion values by sum. At this point we have data with just our dog’s names, with the proportions of both male and female baby names combined. Next, we convert the name variable to a factor, remove the grouping, and sort by name and year.

Here’s what it looks like now:

> head(dog_names)
year  name         prop
1 1893 Buddy 4.130866e-05
2 1894 Buddy 5.604573e-05
3 1896 Buddy 8.522045e-05
4 1898 Buddy 3.784782e-05
5 1899 Buddy 4.340353e-05
6 1900 Buddy 6.167015e-05

Now we’ve got tidy filtered and sorted data, so let’s plot it. I’ve been using ggplot2 for many years, and I think it’s the best way to produce publication quality figures. But usually you want to do some investigation of the data before doing that, and doing this in ggplot2 involves many cycles of code manipulation, plotting, viewing in order to see what you’ve got and how you want the final version to look.

ggvis is a new package that displays data interactively in a web browser. It also supports the pipe operator, so you can pipe the data directly into the plotting routine. It’s somewhat similar to ggplot2, but has some new conventions that are required in order to handle interactivity. Here’s a plot of my dog names data. The first part is the same as before, but I’m piping the result directly into ggivs.

library(ggvis)
babynames %>%
   filter(name %in% c("Nika", "Piper", "Buddy", "Koidern", "Deuce",
                     "Kiva", "Lennier", "Martin", "Monte")) %>%
   group_by(year, name) %>%
   summarise(prop=sum(prop)) %>%
   transform(name=factor(name)) %>%
   ungroup() %>%
   arrange(name, year) %>%
   ggvis(~year, ~prop, stroke=~name, fill=~name) %>%
   # layer_lines(strokeWidth:=2) %>%
   layer_points(size:=15) %>%
   add_axis("x", title="Year", format="####") %>%
   add_axis("y", title="Proportion of total names", title_offset=50) %>%
   add_legend(c("stroke", "fill"), title="Name")
ggvis version

Popularity of our dog names, ggvis version

Typically, I prefer to include lines and points in a timeseries plot like this, but I couldn't get ggvis to color the lines and the points without some very strange fill artifacts.

Here’s what I’d consider to be a high quality version of this, generated with ggplot:

library(ggplot2)
library(scales)

q <- ggplot(data=dog_names, aes(x=year, y=prop, colour=name)) +
         geom_point(size=1.75) +
         geom_line() +
         theme_bw() +
         scale_colour_brewer(palette="Set1") +
         scale_x_continuous(name="Year", breaks=pretty_breaks(n=10)) +
         scale_y_continuous(name="Proportion of total names", breaks=pretty_breaks(n=10))
rescale <- 0.50
svg("dog_names_ggplot2.svg", height=9*rescale, width=16*rescale)
print(q)
dev.off()
ggplo2 version

Popularity of our dog names, ggplot2 version

I think the two plots are pretty similar, and I’m impressed with how good the ggvis plot looks and how similar the language is to ggplot2. And I really like the pipe operator compared with a long list of individual statements or the way you add things together with ggplot2.

Both plots suffer from having too many groups (seven), which means it becomes difficult to interpret the colors on the plot. Choosing a good palette is key to this, and is one of those parts of figure production that can really take a long time. I don’t think my choices in the ggplot2 version is optimal, but I got tired of looking. The other problem is the collection of dog names with very low proportions among human babies. Because they’re all overlapping near the axis, this data is obscured. Both problems could be solved by stacking two plots on top of each other, one with the more popular names (Martin, Piper, Buddy and Monte) and one with the less popular ones (Deuce, Kiva, Nika) using different scales for the proportion axis.

What does the plot show? Among our dog’s names, Martin was the most popular, but it’s popularity has been declining since the 60s, and the name Piper has been increasing since 2000. Both Monte and Buddy were popular in the past, but have declined to low levels recently.

For reference, here are the number of babies in 2013 that were given names matching those of our dogs:

babynames %>%
   filter(name %in% c("Nika", "Piper", "Buddy", "Koidern", "Deuce",
                     "Kiva", "Lennier", "Martin", "Monte")
         & year==2013) %>%
   group_by(year, name) %>%
   summarise(n=sum(n)) %>%
   transform(name=factor(name)) %>%
   ungroup() %>%
   arrange(desc(n))

year   name    n
1 2013  Piper 3166
2 2013 Martin 1330
3 2013  Monte   81
4 2013   Nika   67
5 2013  Buddy   21
6 2013   Kiva   18
7 2013  Deuce    5
tags: dogs  R  ggplot2  names  dplyr  magrittr  ggvis 
Meta Photolog Archives