Skip to content

Instantly share code, notes, and snippets.

@hadley
Created April 21, 2011 03:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadley/933635 to your computer and use it in GitHub Desktop.
Save hadley/933635 to your computer and use it in GitHub Desktop.
a1 <- c(4.2,3.2,11.1,1.3,2.2,2.0)
a2 <- c(3.9,3.2,10.0,0.8,3.1,3.1)
a3 <- c(6.3,2.5,9.8,0.9,2.2,2.4)
a4 <- c(4.4,3.1,9.8,0.8,3.3,2.7)
a5 <- c(4.8,3.0,9.9,0.7,3.3,2.4)
a6 <- c(4.0,3.4,10.5,0.7,3.3,2.1)
a <- rbind(a1,a2,a3,a4,a5,a6)
# Get data into tidy format, starting with a, defined above
rownames(a) <- c("France", "Germany", "Japan", "Britain", "USA", "Turkey")
colnames(a) <- c("Work\nStudy", "Unpaid\nwork", "Eating\nsleeping", "Personal\ncare", "Leisure", "Other")
library(reshape2)
time <- melt(a)
names(time) <- c("country", "activity", "hours")
# Compute excess hours
# No array manipulation magic here
time <- ddply(time, "activity", transform, excess = hours - mean(hours))
# Display using ggplot2. Much easier to see the similarities
# and differences between the two plots. A few less lines of code
# too.
library(ggplot2)
qplot(activity, excess, data = time) +
geom_hline(yintercept = 0, colour = "grey50") +
geom_line(aes(group = 1)) +
facet_wrap(~ country)
qplot(excess, activity, data = time) +
geom_segment(aes(xend = 0, yend = activity)) +
geom_vline(xintercept = 0, colour = "grey50") +
facet_wrap(~ country, nrow = 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment