Last active
November 10, 2019 02:58
-
-
Save tslumley/0bb572d11cb91aa2fcfb5e66b963ed24 to your computer and use it in GitHub Desktop.
Associative transformation of ggplot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setClass("ggthing", representation(content="call")) | |
setClass("ggthings",representation(contents="list")) | |
bracket<-function(e1) NULL | |
setGeneric("bracket") | |
setMethod("bracket",c("ggthing"), | |
function(e1){ | |
new("ggthings",contents=list(e1@content)) | |
} | |
) | |
setMethod("bracket",c("ggthings"), | |
function(e1){ | |
new("ggthings",contents=list(e1@contents)) | |
} | |
) | |
setMethod("+",c("ggthing","ggthing"), | |
function(e1,e2){ | |
new("ggthings",contents=c(e1@content,e2@content)) | |
} | |
) | |
setMethod("+",c("ggthings","ggthing"), | |
function(e1,e2){ | |
new("ggthings",contents=c(e1@contents,e2@content)) | |
} | |
) | |
setMethod("+",c("ggthing","ggthings"), | |
function(e1,e2){ | |
new("ggthings",contents=c(e1@content,e2@contents)) | |
} | |
) | |
setMethod("+",c("ggthings","ggthings"), | |
function(e1,e2){ | |
new("ggthings",contents=c(e1@contents,e2@contents)) | |
} | |
) | |
render<-function(e1) NULL | |
setGeneric("render") | |
setMethod("render","ggthing",function(e1) eval(print(e1@content))) | |
setMethod("render","ggthings",function(e1){ | |
l<-c(e1@contents,recursive=TRUE) | |
g<-Reduce(function(e1,e2) bquote(.(e1)+.(e2)),l) | |
eval(print(g)) | |
} | |
) | |
library(DHBins) | |
ggplot(immune)+geom_map(aes(map_id=DHB,fill=Pcttotal),map=dhmap_hex())+expand_limits(dhmap_hex())+coord_fixed() | |
a<-new("ggthing",content=quote(ggplot(immune))) | |
b<-new("ggthing",content=quote(geom_map(aes(map_id=DHB,fill=Pcttotal),map=dhmap_hex()))) | |
c<-new("ggthing",content=quote(expand_limits(dhmap_hex()))) | |
d<-new("ggthing",content=quote(coord_fixed())) | |
one <- a+b+c+d | |
two <- a+bracket(b+c+d) | |
three<-a+b+bracket(c+d) | |
render(one) | |
render(two) | |
render(three) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment