Skip to content

Instantly share code, notes, and snippets.

@tslumley
Last active November 10, 2019 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tslumley/0bb572d11cb91aa2fcfb5e66b963ed24 to your computer and use it in GitHub Desktop.
Save tslumley/0bb572d11cb91aa2fcfb5e66b963ed24 to your computer and use it in GitHub Desktop.
Associative transformation of ggplot
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