Skip to content

Instantly share code, notes, and snippets.

@thoughtfulbloke
Created September 5, 2021 05:55
Show Gist options
  • Save thoughtfulbloke/24cf859da1334b2ecad3af4e1ee208c8 to your computer and use it in GitHub Desktop.
Save thoughtfulbloke/24cf859da1334b2ecad3af4e1ee208c8 to your computer and use it in GitHub Desktop.
takes an unthemed ggplot graph and saves it in my preferred theme style, timestamped, on the desktop
davidise_graph <- function(x, savepath="~/Desktop/output", show_graph = TRUE){
# x is an unthemed ggplot graph but all the scales & labs are applied
# since this function wants a ggplot graph, I am assuming ggplot is loaded)
# assume Open Sans installed as a font
library(showtext)
# sort out your own font path declarations
font_add(family = "OpenSans", regular = "~/Library/Fonts/OpenSans-Light.ttf",
bolditalic = "~/Library/Fonts/OpenSans-BoldItalic.ttf",
italic = "~/Library/Fonts/OpenSans-LightItalic.ttf",
bold="~/Library/Fonts/OpenSans-Regular.ttf")
showtext_auto()
theme_davidhood <- function(){
theme_minimal(base_family="OpenSans") %+replace%
theme(panel.grid = element_blank(),
axis.line.x = element_line(size=0.1),
axis.line.y = element_line(size=0.1),
axis.ticks = element_line(size=0.2),
strip.background = element_rect(fill= "#FFFFFF", colour="#EFEFEF"),
strip.text = element_text(size = 13,
margin = margin(t = 5, r = 5, b = 5, l = 5, unit = "pt")),
strip.placement = "inside",
panel.background = element_rect(fill = "#FFFFFF", colour = "#FFFFFF"),
panel.spacing = unit(1.5, "lines"),
plot.title = element_text(size = 14,
lineheight = 1.23,
margin=margin(t = 0, r = 0, b = 10, l = 10, unit = "pt"),
hjust=0),
plot.background = element_rect(fill = "#FAFAFA"),
axis.title = element_text(size=13),
plot.caption = element_text(margin=margin(t = 5, r = 5, b = 5, l = 5, unit = "pt"),
size=11, hjust=1),
plot.caption.position = "plot",
plot.margin = margin(0.3, 0.4, 0.3, 0.3, "cm"))
}
y <- x + theme_davidhood()
if(show_graph){print(y)}
savepath <- paste0(savepath,strftime(Sys.time(), format="_%Y_%m_%d_%H_%M_%S.png"))
ggsave(filename=savepath, plot=y,dpi=72, units="in",
bg="white", height = 5.556, width=9.877)
showtext_auto(FALSE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment