Reduce complexity and add support for system fonts

This commit is contained in:
dchakro 2020-08-25 17:49:57 +03:00
parent 3a22985ed4
commit 138478394c

View file

@ -8,15 +8,15 @@ DC_theme_generator <- function(type, legend=T, ticks="out", x.axis.angle=0, hjus
## Options: ## Options:
# type: 'L', 'square' # type: 'L', 'square'
# ticks: 'in', 'out' # ticks: 'in', 'out'
# fontfamily: 'serif' , 'sans' , 'mono' # fontfamily: 'serif' , 'sans' , 'mono', 'Helvetica', 'Palatino'
# ax.fontstyle = 'plain', 'italic', 'bold', 'bold.italic' # ax.fontstyle = 'plain', 'italic', 'bold', 'bold.italic'
ticks <- tolower(ticks) ticks <- tolower(ticks)
fontfamily <- tolower(fontfamily) # Supports system fonts, and doesn't transform them to lower case as font names are case-sensitive.
fontfamily <- ifelse(test = tolower(fontfamily) %in% c("sans","serif","mono"),yes = tolower(fontfamily),no = fontfamily)
if( type == "square"){ if( type == "square"){
if( legend ){ # Type: square
# Type: square , with legend"
themeToReturn <- ggplot2::theme( themeToReturn <- ggplot2::theme(
panel.border = element_rect(linetype = 1, panel.border = element_rect(linetype = 1,
colour = "black", colour = "black",
@ -49,53 +49,14 @@ DC_theme_generator <- function(type, legend=T, ticks="out", x.axis.angle=0, hjus
size = rel(fontsize.cex), size = rel(fontsize.cex),
color = "black", color = "black",
margin=unit(c(0.3,0.3,0.3,0.3), "cm")), margin=unit(c(0.3,0.3,0.3,0.3), "cm")),
legend.key= element_rect(fill=NA,
colour = NA)
)
}else{
# Type: square , without legend"
themeToReturn <- ggplot2::theme(
panel.border = element_rect(linetype = 1,
colour = "black",
fill=NA,
size=1),
panel.background=element_blank(),
panel.grid.major=element_line(color=NA),
axis.ticks = element_line(colour = "black"),
text = element_text(family = fontfamily),
plot.title = element_text(family = fontfamily,
face="bold",
color = "black",
size = rel(fontsize.cex)),
axis.ticks.length = unit(ifelse(ticks=="in",-0.2,0.2),"cm"),
axis.title = element_text(family = fontfamily,
face = ax.fontstyle,
size=rel(fontsize.cex)),
axis.text.y = element_text(family = fontfamily,
face = "plain",
size = rel(fontsize.cex),
hjust = 0.5,
angle = 0,
color="black",
margin=unit(c(0.3,0.3,0.3,0.3), "cm")),
axis.text.x = element_text(family = fontfamily,
face = "plain",
angle = x.axis.angle,
hjust = hjust,
vjust = vjust,
size = rel(fontsize.cex),
color = "black",
margin=unit(c(0.3,0.3,0.3,0.3), "cm")),
legend.key= element_rect(fill=NA, legend.key= element_rect(fill=NA,
colour = NA), colour = NA),
legend.position="none" legend.position=ifelse(test = legend,yes = "right" ,no = "none")
) )
}
} }
if( type == 'L' ){ if( type == 'L' ){
if( legend ){ # Type: L
# Type: L , with legend"
themeToReturn <- ggplot2::theme( themeToReturn <- ggplot2::theme(
axis.line = element_line(colour = "black", axis.line = element_line(colour = "black",
size=0.5), size=0.5),
@ -128,48 +89,10 @@ DC_theme_generator <- function(type, legend=T, ticks="out", x.axis.angle=0, hjus
size = rel(fontsize.cex), size = rel(fontsize.cex),
color = "black", color = "black",
margin=unit(c(0.3,0.3,0.3,0.3), "cm")), margin=unit(c(0.3,0.3,0.3,0.3), "cm")),
legend.key = element_rect(fill=NA,
colour = NA)
)
} else {
# Type: L , without legend
themeToReturn <- ggplot2::theme(
axis.line = element_line(colour = "black",
size=0.5),
panel.border = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.ticks = element_line(colour = "black"),
text = element_text(family = fontfamily),
plot.title = element_text(family = fontfamily,
face="bold",
color = "black",
size = rel(fontsize.cex)),
axis.ticks.length = unit(ifelse(ticks=="in",-0.2,0.2), "cm"),
axis.title = element_text(family = fontfamily,
face = ax.fontstyle,
size=rel(fontsize.cex)),
axis.text.y = element_text(family = fontfamily,
face = "plain",
size = rel(fontsize.cex),
hjust = 0.5,
angle = 0,
color = "black",
margin=unit(c(0.3,0.3,0.3,0.3), "cm")),
axis.text.x = element_text(family = fontfamily,
face = "plain",
angle = x.axis.angle,
hjust = hjust,
vjust = vjust,
size = rel(fontsize.cex),
color = "black",
margin=unit(c(0.3,0.3,0.3,0.3), "cm")),
legend.key = element_rect(fill=NA, legend.key = element_rect(fill=NA,
colour = NA), colour = NA),
legend.position="none" legend.position=ifelse(test = legend,yes = "right" ,no = "none")
) )
} }
}
return(themeToReturn) return(themeToReturn)
} }