smv-model/r/graph.r

134 lines
3.8 KiB
R
Executable File

##########
##########
########## Create Graph
##########
##########
createGraph <- function(data, meta){
margin = 48
#--------[ Create Graph ]--------#
g <- ggplot(data=data, aes(x=age))
#--------[ Titles and Labels ]--------#
g <- g + ggtitle(meta['title']) +
labs(subtitle=meta['source']) +
labs(x=meta['xtitle'], y=meta['ytitle']) +
scale_color_discrete(name=meta['ltitle'])
#--------[ Set X-Axis Ticks ]--------#
g <- g + scale_x_continuous(breaks=seq(0,100,5))
#--------[ Border ]--------#
g <- g + theme(panel.background = element_rect(color = 'black'))
#--------[ Set Font and Margin ]--------#
g <- g + theme(plot.title=element_text(size=20, face="bold"),plot.subtitle=element_text(size=8))+
theme(plot.margin=margin(c(margin,margin,margin,margin)))
#--------[ Legend Position ]--------#
if(meta['lpos']=='none'){ g <- g + theme(legend.position='none') }
if(meta['lpos']=='tl'){ g <- g + theme(legend.justification=c(0,1), legend.position=c(0.02,0.98)) }
if(meta['lpos']=='tc'){ g <- g + theme(legend.justification=c(0.5,1), legend.position=c(0.5,0.98)) }
if(meta['lpos']=='tr'){ g <- g + theme(legend.justification=c(1,1), legend.position=c(0.98,0.98)) }
if(meta['lpos']=='bl'){ g <- g + theme(legend.justification=c(0,0), legend.position=c(0.02,0.02)) }
if(meta['lpos']=='bc'){ g <- g + theme(legend.justification=c(0.5,0), legend.position=c(0.5,0.02)) }
if(meta['lpos']=='br'){ g <- g + theme(legend.justification=c(1,0), legend.position=c(0.98,0.02)) }
if(meta['lpos']=='ml'){ g <- g + theme(legend.justification=c(0,0.5), legend.position=c(0.02,0.5)) }
if(meta['lpos']=='mr'){ g <- g + theme(legend.justification=c(1,0.5), legend.position=c(0.98,0.5)) }
#--------[ Print and Return ]--------#
# print(g)
return(g)
}
##########
##########
########## Draw Graph
##########
##########
drawGraph <- function(g, data, key, label, anno=FALSE){
#--------[ Plot Data ]--------#
g <- g + geom_line(data=data, aes(y=data[,key], color=label))+
geom_point(data=data, aes(y=data[,key], color=label))
#--------[ Annotations ]--------#
if(anno==TRUE){
g <- g + geom_text(data=data, aes(label=data[,key], y=data[,key]+(max(data[,key])*0.02), color=label))
}
# print(g)
return(g)
}
drawGraphMaster <- function(g, data, key, label){
#--------[ Plot Data ]--------#
g <- g + geom_line(data=data, aes(y=data[,key], color=label),size=1.1) +
geom_point(data=data, aes(y=data[,key], color=label))
# print(g)
return(g)
}
drawGraphClean <- function(g, data, key, label){
#--------[ Plot Data ]--------#
g <- g + geom_line(data=data, aes(y=data[,key], color=label))
# print(g)
return(g)
}
drawGraphAlpha <- function(g, data, key, label){
#--------[ Plot Data ]--------#
g <- g + geom_line(data=data, aes(y=data[,key], color=label),alpha=0.15)
# print(g)
return(g)
}
drawGraphAlphaMid <- function(g, data, key, label){
#--------[ Plot Data ]--------#
g <- g + geom_line(data=data, aes(y=data[,key], color=label),alpha=0.35)
# print(g)
return(g)
}
##########
##########
########## Save Graph
##########
##########
saveGraph <- function(graph, meta){
print(graph)
if(!file.exists(paste('graphs/', meta['set'], sep=''))){
dir.create(paste('graphs/', meta['set'], sep=''))
dir.create(paste('graphs/', meta['set'], '/png', sep=''))
dir.create(paste('graphs/', meta['set'], '/svg', sep=''))
}
tmpX = strtoi(meta['aspect1'])
tmpY = strtoi(meta['aspect2'])
filename = paste('graphs/', meta['set'], '/png/', meta['id'],'.png', sep='')
ggsave(file=filename, plot=graph, width=tmpX, height=tmpY)
filename = paste('graphs/', meta['set'], '/svg/', meta['id'],'.svg', sep='')
ggsave(file=filename, plot=graph, width=tmpX, height=tmpY)
}