Skip to content

Instantly share code, notes, and snippets.

@abarbour
Last active December 10, 2015 08:48
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 abarbour/4409664 to your computer and use it in GitHub Desktop.
Save abarbour/4409664 to your computer and use it in GitHub Desktop.
Add rowwise summary lines to ggplot2::facet_grid style plots.
# 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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment