Skip to content

Instantly share code, notes, and snippets.

@lightonphiri
Created January 8, 2015 07:59
Show Gist options
  • Save lightonphiri/0330a672f12edae2097c to your computer and use it in GitHub Desktop.
Save lightonphiri/0330a672f12edae2097c to your computer and use it in GitHub Desktop.
[Blogpost Snippet@ lightonphiri.org] ggplot2 - Multiple Plots in One Graph Using gridExtra
# required libraries
require(ggplot2)
require(reshape2)
require(grid)
require(scales)
library(gridExtra)
# p1---plot #1
# Search Performance
p1dataset <- read.table("p1.dat", header=TRUE, sep=",")
p1dataset$TplusP <- p1dataset$Traversal + p1dataset$Parsing
p1dataset$TplusPplusX <- p1dataset$Traversal + p1dataset$Parsing + p1dataset$XPath
p1experimentdataset <- melt(p1dataset[p1dataset$Structure=="h1",][c(1,3,7:8)], id.vars=c(1))
levels(p1experimentdataset$variable)[levels(p1experimentdataset$variable)=="Processing"] <- paste("Total","")
levels(p1experimentdataset$variable)[levels(p1experimentdataset$variable)=="Traversal"] <- paste("Traversal","")
levels(p1experimentdataset$variable)[levels(p1experimentdataset$variable)=="TplusP"] <- paste("Traversal+Parsing","")
levels(p1experimentdataset$variable)[levels(p1experimentdataset$variable)=="TplusPplusX"] <- paste("Traversal+Parsing+XPath","")
p1 = ggplot(data=p1experimentdataset, aes(x=as.factor(Workload), y=(value), group=variable, color=variable, shape=variable, linetype=variable)) + geom_line() + geom_point() + scale_shape_manual(values=c(18,4,13,1)) + scale_linetype_manual(values=c("solid","dotdash","longdash","dotted")) + scale_x_discrete(labels=c("100","200","400","800","1.6k","3.2k","6.4k","12.8k","25.6k","51.2k","102.4k","204.8k","409.6k","819.2k","1638.4k")) + scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x))) + labs(x="",y="") + theme(plot.title=element_text(size=10), legend.title=element_blank(), legend.position=c(0.37,0.808), plot.margin=unit(c(0.2,0.15,0.05,0.05),"cm"), axis.text.x = element_text(angle=90, vjust=0.5)) + ggtitle("Query Performance (log10(ms))")
# p2---plot #2
p2dataset <- read.table("p2.dat", header=TRUE, sep=",")
p2experimentdataset <- melt(p2dataset, id.vars=c(1))
# Rename levels
levels(p2experimentdataset$variable)[levels(p2experimentdataset$variable)=="GetRecord"] <- paste("GetRecord","")
levels(p2experimentdataset$variable)[levels(p2experimentdataset$variable)=="ListIdentifiers"] <- paste("ListIdentifiers","")
levels(p2experimentdataset$variable)[levels(p2experimentdataset$variable)=="ListRecords"] <- paste("ListRecords","")
levels(p2experimentdataset$variable)[levels(p2experimentdataset$variable)=="ListSets"] <- paste("ListSets","")
p2 = ggplot(data=p2experimentdataset, aes(x=as.factor(Workload), y=value, group=variable, color=variable, shape=variable, linetype=variable)) + geom_line() + geom_point() + guides(col = guide_legend(nrow = 2)) + scale_shape_manual(values=c(18,4,13,1)) + scale_linetype_manual(values=c("solid","dotdash","longdash","dotted")) + scale_x_discrete(labels=c("100","200","400","800","1.6k","3.2k","6.4k","12.8k","25.6k","51.2k","102.4k","204.8k","409.6k","819.2k","1638.4k")) + scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x))) + labs(x="",y="") + theme(plot.title=element_text(size=10), legend.title=element_blank(), legend.position=c(0.25,0.75), plot.margin=unit(c(0.2,0.15,0.05,0.05),"cm"), axis.text.x = element_text(angle=90, vjust=0.5)) + ggtitle("OAI-PMH data-provider (log10(ms))")
# p3---plot #3
# Simplicity & Understandability
p3dataset <- read.table("p3.dat", header=TRUE, sep=",")
p3experimentdataset <- p3dataset
levels(p3experimentdataset$Feature)[levels(p3experimentdataset$Feature)=="Metadata"] <- "Metadata"
levels(p3experimentdataset$Feature)[levels(p3experimentdataset$Feature)=="Structure"] <- "Structure"
levels(p3experimentdataset$Scale)[levels(p3experimentdataset$Scale)=="Strongly Agree"] <- "Strongly Agree"
levels(p3experimentdataset$Scale)[levels(p3experimentdataset$Scale)=="Agree"] <- "Agree"
levels(p3experimentdataset$Scale)[levels(p3experimentdataset$Scale)=="Neutral"] <- "Neutral"
levels(p3experimentdataset$Scale)[levels(p3experimentdataset$Scale)=="Disagree"] <- "Disagree"
levels(p3experimentdataset$Scale)[levels(p3experimentdataset$Scale)=="Strongly Disagree"] <- "Strongly Disagree"
p3 = ggplot(p3experimentdataset, aes(x=Feature, y=Count, fill=Scale, width=0.75)) + geom_bar(stat="identity") + labs(x="", y="") + facet_wrap(~ Aspect,ncol=1) + theme(plot.title=element_text(size=10), legend.title=element_blank(), legend.position="bottom", axis.text.x=element_blank(), axis.ticks.x=element_blank(), axis.ticks.y=element_blank()) + scale_fill_brewer(palette="Dark2") + ggtitle("Developer Survey (Ease of Use)") + coord_flip() + guides(fill = guide_legend(ncol = 2))
# p4---plot #4
# Programming Languages Usage
p4dataset <- read.table("p4.dat", header=TRUE, sep=",")
p4experimentdataset <- transform(p4dataset, Language = reorder(Language, Count))
levels(p4experimentdataset$Language)[levels(p4experimentdataset$Language)=="CSharp"] <- "C#"
p4 = ggplot(p4experimentdataset, aes(x=Language, y=Count, ymax=26, fill=Language, width=0.75)) + geom_bar(stat="identity") + labs(x="", y="") + theme(plot.title=element_text(size=10), legend.position="none", legend.title=element_blank(), legend.position="bottom", axis.text.x=element_blank(), axis.ticks.x=element_blank(), axis.ticks.y=element_blank()) + scale_fill_brewer(palette="Dark2") + geom_text(aes(x=Language, y=Count, ymax=max(p4experimentdataset$Count), label=paste(round(((Count/26)*100),0), "%"), hjust=ifelse(sign(Count)>0, 1, 0)), position = position_dodge(width=1)) + ggtitle("Developer Survey (Lang. Prevalence)") + coord_flip()
grid.arrange(p1, p2, p3, p4, ncol=2)
##### p1.dat #####
Workload,Processing,Traversal,Parsing,XPath,Structure
100,24.6673583984,3.9794921875,9.4939575195,11.1939086914,h1
200,47.8742675782,7.3744506836,18.6964111328,21.8034057617,h1
400,97.3832397461,14.5014038086,37.9913330078,44.8905029297,h1
800,191.902282715,27.4054565431,75.6998901367,88.7969360352,h1
1600,386.9901123048,53.4900512698,152.4407348633,181.0593261718,h1
3200,768.3469238283,104.3426513673,304.0778198245,359.9264526365,h1
6400,1531.0562744125,207.4905395493,602.7890625,720.7766723633,h1
12800,3093.1226806625,414.6845703125,1222.3314209,1456.10668945,h1
25600,6172.2185668925,825.3220214825,2454.14416504,2892.75238037,h1
51200,12487.0630493,1705.11480711,4959.819763185,5822.128479005,h1
102400,25108.742065425,3564.3349609225,9915.6099853525,11628.79711915,h1
204800,49301.45025635,6659.371765125,19546.46154785,23095.616943375,h1
409600,100267.334777925,14238.86248785,39959.60455325,46068.867736825,h1
100,27.2279663086,6.4830932617,9.5142211914,11.2306518555,h2
200,51.909362793,11.4969482422,18.5111694336,21.9012451172,h2
400,105.8765258788,22.0095214842,38.2366333008,45.6303710937,h2
800,204.0386962893,38.1530761721,75.9852294922,89.900390625,h2
1600,406.3395385743,70.9324951168,153.6907348638,181.7163085938,h2
3200,795.3247070313,128.1399536135,304.509460449,362.6752929688,h2
6400,1574.1594238275,242.1823120113,612.9107666013,719.066345215,h2
12800,3133.2747192375,460.42803955,1227.220703125,1445.6259765625,h2
25600,6223.12664795,881.5872802775,2456.51092529,2885.0284423825,h2
51200,12518.88171385,1832.44494627,4916.0241699225,5770.4125976575,h2
102400,24983.10913085,3570.7881469825,9865.3925170925,11546.928466775,h2
204800,50112.47698975,7284.495605475,19754.91174315,23073.069641125,h2
409600,101745.170288025,15464.1981811,39962.57855225,46318.393554675,h2
100,30.502746582,9.6138916016,9.4568481445,11.4320068359,h3
200,57.5413818359,17.2737426758,18.326171875,21.9414672852,h3
400,116.115966797,32.7991943361,37.6735839844,45.6431884766,h3
800,224.3782958983,58.4542236326,74.9201660156,91.00390625,h3
1600,441.3248901368,107.9719238285,150.6657714843,182.687194824,h3
3200,859.7318115238,191.785766602,300.7835693358,367.162475586,h3
6400,1810.6535644525,483.9257202143,602.5999145508,724.1279296875,h3
12800,3335.7998046875,661.68395996,1218.290649415,1455.8251953125,h3
25600,6573.066833495,1228.373535155,2440.66192627,2904.03137207,h3
51200,13194.364074675,2423.05975339,4938.8477172825,5832.4566040025,h3
102400,26495.5614624,4947.5816650425,9959.2310791075,11588.74871825,h3
204800,54111.85772705,10790.5238037,20043.704650875,23277.629272475,h3
409600,121782.13922125,33768.90014655,40692.942932125,47320.296142575,h3
##### p2 #####
Workload,GetRecord,ListIdentifiers,ListRecords,ListSets
100,0.01,0.01,0.48,0
200,0.01,0.02,0.98,0.01
400,0.01,0.06,2,0
800,0.02,0.17,4.12,0.01
1600,0.02,0.39,5.41,0.01
3200,0.05,0.77,5.94,0.02
6400,0.08,1.53,6.73,0.04
12800,0.15,3.09,8.37,0.07
25600,0.3,6.25,11.68,0.12
51200,0.59,13.38,18.9,0.24
102400,1.2,28.46,33.46,0.49
204800,2.45,61.86,66.95,0.99
409600,4.87,133.94,139.72,1.99
819200,10.72,548.08,586.45,11.61
1638400,608.11,1922.89,1937.09,609.18
##### p3 #####
Aspect,Feature,Scale,Count
Understandability,Metadata,Strongly Agree,4
Understandability,Metadata,Agree,12
Understandability,Metadata,Neutral,7
Understandability,Metadata,Disagree,3
Understandability,Metadata,Strongly Disagree,0
Simplicity,Metadata,Strongly Agree,2
Simplicity,Metadata,Agree,16
Simplicity,Metadata,Neutral,3
Simplicity,Metadata,Disagree,5
Simplicity,Metadata,Strongly Disagree,0
Understandability,Structure,Strongly Agree,2
Understandability,Structure,Agree,10
Understandability,Structure,Neutral,8
Understandability,Structure,Disagree,6
Understandability,Structure,Strongly Disagree,0
Simplicity,Structure,Strongly Agree,5
Simplicity,Structure,Agree,11
Simplicity,Structure,Neutral,7
Simplicity,Structure,Disagree,3
Simplicity,Structure,Strongly Disagree,0
##### p4 #####
Language,Count
Java,2
CSharp,2
Python,4
HTML5,11
PHP,15
JavaScript,17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment