Skip to content

Instantly share code, notes, and snippets.

@yonicd
Created July 13, 2017 20:33
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 yonicd/4b36a2e72acef4c179430e0792432293 to your computer and use it in GitHub Desktop.
Save yonicd/4b36a2e72acef4c179430e0792432293 to your computer and use it in GitHub Desktop.
create data.frame of hierarchy structure of scales function call in ggplot2 and ggextensions
library(ggraph)
library(dplyr)
library(d3Tree)
scales2df<-function(pkg){
d<-ls(name = sprintf('package:%s',pkg),pattern = '^scale_(.*?)_(.*?)$')
d1<-sapply(d,function(x){grep('_scale|scale_',as.list(body(x)),value=TRUE)})
d.fun<-sapply(d1,function(a) gsub('sc <- ','',gsub('\\((.*?)$','',a)))
d.fun<-d.fun[sapply(d.fun,length)>0]
df<-data.frame(parent=as.character(d.fun),child=names(d.fun),stringsAsFactors = FALSE)
df%>%dplyr::filter(!parent%in%child)%>%
dplyr::left_join(df%>%dplyr::filter(parent%in%child)%>%dplyr::rename(grandchild=child,child=parent),by='child')%>%
dplyr::mutate(value=NA)%>%dplyr::do(.,cbind(pkg=pkg,.))
}
df1<-rbind(scales2df('ggplot2'),scales2df('ggraph'))%>%dplyr::filter(parent!='if ')
if(length(unique(df1$pkg))>1) df1<-df1%>%select(parent,pkg,child,grandchild,value)
d3Tree::d3tree(list(root = d3Tree::df2tree(rootname='scales',struct=df1),layout = 'radial'),width='100%')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment