Skip to content

Commit 5b675e6

Browse files
committed
added theme_galactic() ggplot2 function
1 parent aabacb2 commit 5b675e6

3 files changed

Lines changed: 154 additions & 0 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export(getPhyloNames_noCache)
66
export(getWikiPic)
77
export(ggciphR)
88
export(showPhylo)
9+
export(theme_galactic)
910
import(datelife)
1011
import(ggtree)

R/theme_galactic.R

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#' theme_galactic
2+
#'
3+
#' A ggplot2 theme for Galactic Polymath styling. Sensible defaults for plots intended for presentations and worksheets
4+
#'
5+
#' @param grid.thickness.maj How heavy do you want grid lines to be? (in case printer makes things lighter); default=.8
6+
#' @param grid.thickness.min How heavy do you want grid lines to be? (in case printer makes things lighter); default=.6
7+
#' @param grid.col What color do you want the grid to be? Default: same as font (#363636)
8+
#' @param border.thickness How heavy do you want the plot border to be?
9+
#' @param border.col Color of plot border. Default: same as font (#363636)
10+
#' @param font Google font to use, "Montserrat" by default; see options with sysfonts::font_families_google()
11+
#' @param regular.wt font weight for regular font style
12+
#' @param bold.wt font weight for bold text
13+
#' @param font.cex a simple multiplier for scaling all text
14+
#' @param axis.lab.col color of axis labels (and title)
15+
#' @param axis.text.col color of axis text (numbers, dates, etc)
16+
#' @param axis.tick.length length of axis ticks (in pt units)
17+
#' @param plot.margin easy access to ggplot margins
18+
#' @export
19+
20+
21+
theme_galactic<-function(grid.thickness.maj=.7,grid.thickness.min=.4,grid.col="#C3C3C3",border.thickness=1,border.col="#6D6D6D",font="Montserrat",regular.wt=400,bold.wt=700,font.cex=1,axis.lab.col="#363636",axis.text.col="#6D6D6D",axis.tick.length=10,plot.margin=ggplot2::margin(t=10,r=10,b=10,l=10)){
22+
gpPal=NULL
23+
utils::data(gpPal,package="galacticPubs")
24+
showtext::showtext_auto()
25+
fam=font
26+
#Only try to download font if online and not already available
27+
if(is.na(match(font,sysfonts::font_families()))){
28+
isOnline=RCurl::url.exists("https://www.google.com")
29+
if(isOnline){
30+
tryCatch(
31+
sysfonts::font_add_google(name=font,family=fam,regular.wt=regular.wt,bold.wt=bold.wt),
32+
error=function(e) cat("\nFont: '",font,"' unavailable."))
33+
}else{
34+
cat("\nYou don't seem to be online. Can't download your requested font.")
35+
}
36+
}
37+
38+
39+
ggplot2::theme_linedraw()+ #base theme to modify
40+
ggplot2::theme(
41+
text=ggplot2::element_text(family=font),
42+
panel.border=ggplot2::element_rect(size=border.thickness,colour=border.col),
43+
panel.grid.major=ggplot2::element_line(size=grid.thickness.maj,colour = grid.col),
44+
panel.grid.minor=ggplot2::element_line(size=grid.thickness.min,colour = grid.col),
45+
plot.margin=plot.margin,
46+
plot.title=ggplot2::element_text(family=font,size=30*font.cex,face="bold",color=axis.lab.col),
47+
plot.subtitle=ggplot2::element_text(family=font,size=22*font.cex,color=gpPal[[1]]$hex[5]),
48+
axis.title=ggplot2::element_text(family=font,size=28*font.cex,face="plain",color=axis.lab.col),
49+
axis.text=ggplot2::element_text(family=font,size=18*font.cex,color=axis.text.col),
50+
axis.ticks=ggplot2::element_line(color=grid.col,size=grid.thickness.maj),
51+
axis.ticks.length=ggplot2::unit(axis.tick.length,"pt"),
52+
axis.title.x = ggplot2::element_text(margin = ggplot2::margin(t = 10, r = 0, b = 0, l = 0)),
53+
axis.title.y = ggplot2::element_text(margin = ggplot2::margin(t = 20, r = 10, b = 0, l = 0)),
54+
legend.text=ggplot2::element_text(family=font,color=axis.text.col,size=18*font.cex),
55+
legend.title=ggplot2::element_text(family=font,color=axis.lab.col,face="bold",size=18*font.cex),
56+
legend.position = "right", legend.text.align = 0, legend.background =ggplot2::element_blank()
57+
)
58+
}
59+
60+
gpLogo<-function(ggObj,xNPC=.9,yNPC=.9,which="horiz_logoWords_GradWhite",size=.1,cloudinaryString=NULL){
61+
logoFile=switch(which,
62+
grad_logo_gradTrans="https://res.cloudinary.com/galactic-polymath/image/upload/v1593304396/logos/GP_logo_grad_transBG_300_tbn4ei.png",
63+
grad_logo_gradWhite="https://res.cloudinary.com/galactic-polymath/image/upload/b_white/v1593304396/logos/GP_logo_grad_transBG_300_tbn4ei.png",
64+
horiz_logoWords_gradTrans="https://res.cloudinary.com/galactic-polymath/image/upload/v1593304395/logos/GP_logo_wordmark_horiz_grad_transBG_300_lqdj7q.png",
65+
horiz_logoWords_gradWhite="https://res.cloudinary.com/galactic-polymath/image/upload/b_white/v1593304395/logos/GP_logo_wordmark_horiz_grad_transBG_300_lqdj7q.png",
66+
horiz_logoWords_whiteAblue="https://res.cloudinary.com/galactic-polymath/image/upload/v1593316226/logos/GP_logo_wordmark_horiz_white_aBlueBG_300_qmuas0.png",
67+
horiz_logoWords_whiteBlack="https://res.cloudinary.com/galactic-polymath/image/upload/v1594949366/logos/GP_logo_wordmark_horiz_white_blackBG_600_buwnlf.png",
68+
"Error"
69+
)
70+
71+
if(logoFile=="Error"){stop("That's not one of the logo file options")}
72+
73+
#Handle additional cloudinary parameters
74+
if(!is.null(cloudinaryString)){
75+
#test if already Cloudinary string in URL
76+
noCloudString=stringr::str_detect(logoFile,"upload\\/v")
77+
78+
if(noCloudString){
79+
#Add strings
80+
splitStr<-stringr::str_split(logoFile,"upload\\/v",simplify=T)
81+
newURL<-paste0(splitStr[1],"upload/",cloudinaryString,"/v",splitStr[2])
82+
}else{
83+
#Add to existing strings
84+
extractStr0<-stringr::str_extract(logoFile,"upload\\/.*\\/v")
85+
extractStr<-gsub("/v","",extractStr0)
86+
splitStr<-stringr::str_split(logoFile,"upload\\/.*\\/v",simplify=T)
87+
newURL<-paste0(splitStr[1],extractStr,",",cloudinaryString,"/v",splitStr[2])
88+
}
89+
}else{newURL<-logoFile}
90+
91+
#read in logo
92+
"https://res.cloudinary.com/galactic-polymath/image/upload/v1593317568/GP_logo_wordmark_horiz_white_blackBG_600_fjj1ii.png"
93+
logoImg<-png::readPNG(RCurl::getURLContent(newURL))
94+
95+
96+
ggObj+ggplot2::annotation_custom(grid::rasterGrob(logoImg,x=ggplot2::unit(xNPC,"npc"),y=ggplot2::unit(yNPC,"npc"),height=ggplot2::unit(size,"npc")))+if(xNPC>1|yNPC>1|xNPC<0|yNPC<0){ggplot2::coord_cartesian(clip = "off")}else{}
97+
98+
}
99+
100+
101+

man/theme_galactic.Rd

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)