public
Last active

Add rowwise summary lines to ggplot2::facet_grid style plots.

  • Download Gist
rlpspec_allbench.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
# Add rowwise summary lines to ggplot2::facet_grid style plots.
# Andy Barbour
# geokook.wordpress.com
# December 2012
 
## clear workspace
rm(list=ls())
 
library(multitaper)
library(rbenchmark)
library(plyr)
library(ggplot2)
library(reshape2)
 
## load the benchmark dataset
# https://docs.google.com/open?id=0B6znTWvKzXDScjBTOWZzYmNqWEU
load("rlpspec_allbench.Rda")
stopifnot(exists("allbench"))
 
## convert lapply output to a usable form: data.frame
allbench.df <- plyr::ldply(allbench)
 
## subset only information we care about
# (many thanks to this valuable stackoverflow post:
# http://stackoverflow.com/questions/4605206/drop-columns-r-data-frame )
allbench.df.drp <- subset(allbench.df,
select = c(test, num_terms, user.self, sys.self, elapsed, relative)
)
 
## reduce data.frame with melt
allbench.df.mlt <- reshape2::melt(allbench.df.drp, id.vars=c("test","num_terms"))
 
## calculate the summary information to be plotted:
## 'value' can be anything, but here we use meadian values from Hmisc::smean.cl.normal, which
## calculates confidence limits using a t-test
tmpd <- plyr::ddply(allbench.df.mlt,
.(variable, num_terms),
summarise,
summary="medians", # not important for plotting, just a name
value=mean_cl_normal(value)[1,1])
 
## create copies for each test and map to data.frame
tests <- unique(allbench.df$test)
allmeds <- plyr::ldply(lapply(X=tests, FUN=function(x,df=tmpd){df$test <- x; return(df)}))
 
## plot the benchmark data
g <- ggplot(data=allbench.df.mlt, aes(x=log10(num_terms),
y=log2(value),
colour=test,
group=test)) +
scale_colour_discrete(guide="none") +
geom_point()
## create (and inspect if necessary) a facetted version
g2 <- g + facet_grid(variable~test, scales="free_y")
## add the summary data as a line
print(g3 <- g2 + geom_path(colour="black", data=allmeds, aes(group=test)))
ggsave(g3, file="rlpspec_allbench.png")

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.