Here’s some code for a ggplot theme
library(pacman); p_load(ggplot2, tidyverse)
my_theme <- function(){
theme(
legend.background = element_rect(colour = "black", linewidth = 0.5),
legend.key.width = unit(0.5, "cm"),
legend.key.height = unit(0.4, "cm"),
panel.background = element_rect(fill = "white"),
plot.background = element_rect(fill = "white"),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(colour ="gray80",linewidth = 1,linetype="dashed"),
panel.grid.minor.y = element_blank(),
axis.text.x = element_text(colour ="black",size=rel(1.5),hjust=0.5),
axis.text.y = element_text(colour ="black",size=rel(1.5),vjust=0.5),
axis.title.x = element_text(colour ="black",size=rel(1.2)),
axis.title.y = element_text(colour ="black",size=rel(1.2)),
plot.margin=unit(c(1,0.5,0.5,0.5), "cm"),
plot.title=element_text(hjust=0.5,vjust=5,size=rel(1.5),face="bold"),
plot.caption=element_text(hjust=0,size=rel(1.2)),
axis.line.x=element_line(color="gray80",linewidth = 1,linetype="dashed"),
axis.line.y=element_line(color="gray80",linewidth = 1,linetype="dashed"),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank())}
my_geom_point <- function(...){
geom_point(size = 3, alpha = 0.6, shape = 16, ...)}
Personal_Colours = list(
PaletteOne = c("orangered", "darkred", "#67238a", "#1874cd", "#9B870C", "darkblue"),
PaletteTwo = c("#0C0A3E", "#7B1E7A", "#B33F62", "#F9564F", "#F3C677"))
Personal_Palettes = function(name, n, all_palettes = Personal_Colours, type = c("discrete", "continuous")){
palette = all_palettes[[name]]
if (missing(n)) {
n = length(palette)}
type = match.arg(type)
out = switch(type,
continuous = grDevices::colorRampPalette(palette)(n),
discrete = palette[1:n])
structure(out, name = name, class = "palette")}
scale_colour_personal_d = function(name){
ggplot2::scale_colour_manual(values = Personal_Palettes(name,
type = "discrete"))}
scale_fill_personal_d = function(name){
ggplot2::scale_fill_manual(values = Personal_Palettes(name,
type = "discrete"))}
scale_colour_personal_c = function(name){
ggplot2::scale_colour_gradientn(colours = Personal_Palettes(name,
type = "continuous"))}
scale_fill_personal_c = function(name){
ggplot2::scale_fill_gradientn(colours = Personal_Palettes(name,
type = "continuous"))}
scale_color_personal_d = scale_colour_personal_d
scale_color_personal_c = scale_colour_personal_c
And now some demo plots.
TestScatter <- ggplot(
mtcars,
aes(x = mpg, y = wt)) +
my_theme() +
labs(
x = "Miles per Gallon",
y = "Weight",
title = "Car Weight vs. Miles per Gallon",
caption = "Source: ggplot2")
TestBar <- ggplot(
mtcars, aes(factor(cyl), fill = factor(cyl))) +
my_theme()+
scale_y_continuous(expand = c(0, 0)) +
labs(
x = "Cylinders",
y = "Count",
title = "Car Cylinders and their Counts",
caption = "Source: ggplot2")
TestScatter +
my_geom_point(aes(color = wt)) +
scale_color_personal_c("PaletteOne")
TestScatter +
my_geom_point(aes(color = wt)) +
scale_color_personal_c("PaletteTwo")
TestScatter +
my_geom_point(aes(color = wt)) +
scale_color_personal_c("PaletteTwo") +
theme(
legend.position = c(.026, .097))
TestBar +
geom_bar(width = 1, stat = "count", color = "black", linewidth = 1) +
scale_fill_personal_d("PaletteOne")
TestBar +
geom_bar(width = 1, stat = "count", color = "black", linewidth = 1) +
scale_fill_personal_d("PaletteTwo")
TestBar +
geom_bar(width = 1, stat = "count", color = "black", linewidth = 1) +
scale_fill_personal_d("PaletteTwo") +
theme(
legend.position = c(0.07, .925))
sessionInfo()
## R version 4.2.2 (2022-10-31 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19045)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=English_United States.utf8
## [2] LC_CTYPE=English_United States.utf8
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.utf8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] lubridate_1.9.2 forcats_1.0.0 stringr_1.5.0 dplyr_1.1.0
## [5] purrr_1.0.1 readr_2.1.4 tidyr_1.3.0 tibble_3.1.8
## [9] tidyverse_2.0.0 ggplot2_3.4.1 pacman_0.5.1
##
## loaded via a namespace (and not attached):
## [1] highr_0.10 bslib_0.4.2 compiler_4.2.2 pillar_1.8.1
## [5] jquerylib_0.1.4 tools_4.2.2 digest_0.6.31 timechange_0.2.0
## [9] jsonlite_1.8.4 evaluate_0.20 lifecycle_1.0.3 gtable_0.3.1
## [13] pkgconfig_2.0.3 rlang_1.0.6 cli_3.6.0 rstudioapi_0.14
## [17] yaml_2.3.7 xfun_0.37 fastmap_1.1.0 withr_2.5.0
## [21] knitr_1.42 hms_1.1.2 generics_0.1.3 vctrs_0.5.2
## [25] sass_0.4.5 grid_4.2.2 tidyselect_1.2.0 glue_1.6.2
## [29] R6_2.5.1 fansi_1.0.4 rmarkdown_2.20 farver_2.1.1
## [33] tzdb_0.3.0 magrittr_2.0.3 ellipsis_0.3.2 scales_1.2.1
## [37] htmltools_0.5.4 colorspace_2.1-0 labeling_0.4.2 utf8_1.2.3
## [41] stringi_1.7.12 munsell_0.5.0 cachem_1.0.6