Skip to content

Commit

Permalink
Merge pull request #41 from MatthewBJane/dune
Browse files Browse the repository at this point in the history
Add `theme_dune` + functionality for local font files
  • Loading branch information
MatthewBJane authored Mar 20, 2024
2 parents cb3b2bc + bec84c9 commit 63e711f
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 2 deletions.
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(alien_theme_colors)
export(asteroid_city_theme_colors)
export(avatar_theme_colors)
export(barbie_theme_colors)
export(dune_theme_colors)
export(elf_palette)
export(elf_theme_colors)
export(french_dispatch_theme_colors)
Expand All @@ -26,6 +27,8 @@ export(scale_color_alien)
export(scale_color_asteroid_city)
export(scale_color_avatar)
export(scale_color_barbie)
export(scale_color_dune)
export(scale_color_dune_discrete)
export(scale_color_elf_b)
export(scale_color_elf_c)
export(scale_color_elf_d)
Expand Down Expand Up @@ -64,6 +67,8 @@ export(scale_fill_alien)
export(scale_fill_asteroid_city)
export(scale_fill_avatar)
export(scale_fill_barbie)
export(scale_fill_dune)
export(scale_fill_dune_discrete)
export(scale_fill_elf_b)
export(scale_fill_elf_c)
export(scale_fill_elf_d)
Expand Down Expand Up @@ -98,6 +103,7 @@ export(theme_alien)
export(theme_asteroid_city)
export(theme_avatar)
export(theme_barbie)
export(theme_dune)
export(theme_elf)
export(theme_french_dispatch)
export(theme_friends)
Expand Down
150 changes: 150 additions & 0 deletions R/theme_dune.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
## Luke Pilling
## theme_dune
## 20-Mar-2024

## Font from the amazing https://www.dafont.com/dune-rise.font

# COLOR: add, remove, or edit the colors to fit your scheme. Names should be

#' DUNE Theme Color Palette
#'
#' @format character vector of hex code strings
#' @export
#' @concept dune
#'
#' @examples
#' dune_theme_colors
#'
dune_theme_colors <- c(
background = '#FAEDAB',
text = '#433012',
panel = '#FFEB82',
border = '#A44E11',
lighter = '#F7EEE1',
light = '#DB9128',
medium = '#B67523',
dark = '#433012'
)

dune_theme_colors_discrete <- c("#220F08", "#471C09", "#642F07", "#A2510F", "#B67523", "#DAA849", "#F3D867", "#FFEA7F")



# THEME: rename function and theme() arguments according to your theme design, feel free to edit this how you would like

#' DUNE Inspired Theme
#'
#' @param dune_font should `theme_dune` use https://www.dafont.com/dune-rise.font? Default is `TRUE`.
#' @param ... additional parameters to pass to `ggplot2::theme()`
#'
#' @return a `ggplot2` `theme` element
#' @export
#' @concept dune
#'
#' @examples
#' library(ggplot2)
#'
#' ggplot(data = data.frame(x = rnorm(50, 0, 1), y = rnorm(50,0,1)), aes(x = x, y = y)) +
#' geom_smooth(method = 'lm',
#' color = dune_theme_colors["dark"],
#' fill = dune_theme_colors["light"]) +
#' geom_point(color = dune_theme_colors["medium"]) +
#' labs(title = 'DUNE Scatter Plot') +
#' theme_dune()
#'
#' ggplot(mpg, aes(cty)) +
#' geom_density(aes(fill=factor(cyl)), alpha=0.8) +
#' labs(title="Density plot",
#' subtitle="Mileage Grouped by cylinders",
#' caption="Source: mpg",
#' x="City Mileage",
#' fill="# Cylinders") +
#' theme_dune()+
#' scale_fill_dune_discrete()
#'
theme_dune <- function(dune_font = TRUE, ...){

# CUSTOM FONT: add a custom font from google fonts
font_family = ifelse(dune_font,"Dune_Rise","sans") # use this line if you have a custom font
if (dune_font) {
ThemePark:::initialize_font(name = "Dune_Rise", family = "Dune_Rise", local = "Dune_Rise.ttf")
}

# CUSTOM THEME:
ggplot2::theme(
plot.background = element_rect(fill = dune_theme_colors["background"]),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
text = element_text(color = dune_theme_colors["text"], family = font_family),
title = element_text(size=20),
panel.background = element_rect(fill = dune_theme_colors["panel"]),
panel.border = element_rect(fill = NA, color = dune_theme_colors["border"], linewidth=1.2),
axis.title = element_text(size=17),
axis.text = element_text(size=13,color = dune_theme_colors["text"]),
axis.ticks = element_line(color = dune_theme_colors["border"],linewidth=1),
legend.background = element_rect(fill = dune_theme_colors["panel"], color = NA),
strip.background = element_rect(fill = dune_theme_colors["lighter"], colour = dune_theme_colors["border"]),
strip.text = element_text(colour = dune_theme_colors["text"]),
...
)
}

# COLOR SCALES: Make pretty color scales

#' DUNE Inspired Color Scales
#'
#' @param ... Additional arguments to pass to `ggplot2::scale_[fill/color]_gradient()`
#'
#' @return a `ggplot` scale object
#'
#' @rdname scale_dune
#' @export
#' @concept dune
#'
#' @examples
#' library(ggplot2)
#'
#' ggplot(mpg) +
#' geom_point(aes(y = class, x = hwy, color = cyl)) +
#' labs(title="MPG by Vehicle Type",
#' caption="Source: mpg",
#' x = "City Mileage",
#' color ="# Cylinders") +
#' theme_dune() +
#' scale_color_dune()
#'
#' ggplot(mpg, aes(cty)) +
#' geom_density(aes(fill=factor(cyl)), alpha=0.8) +
#' labs(title="Density plot",
#' subtitle="Mileage Grouped by cylinders",
#' caption="Source: mpg",
#' x="City Mileage",
#' fill="# Cylinders") +
#' theme_dune()+
#' scale_fill_dune_discrete()
#'
scale_fill_dune <- function(...) {
ggplot2::scale_fill_gradient(low = dune_theme_colors["dark"], high = dune_theme_colors["light"], ...)
}

#' @rdname scale_dune
#' @export
#' @concept dune
scale_color_dune <- function(...) {
ggplot2::scale_color_gradient(low = dune_theme_colors["dark"], high = dune_theme_colors["light"], ...)
}

#' @rdname scale_dune
#' @export
#' @concept dune
scale_fill_dune_discrete <- function(...) {
ggplot2::scale_fill_manual(values = dune_theme_colors_discrete, ...)
}

#' @rdname scale_dune
#' @export
#' @concept dune
scale_color_dune_discrete <- function(...) {
ggplot2::scale_color_manual(values = dune_theme_colors_discrete, ...)
}

8 changes: 6 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ rot_pal <- function(pal) {
#' @param ... additional arguments to pass to `sysfonts::font_add_google`
#' we don't want an R documentation file in `man/` for this, as it's internal
#' @noRd
initialize_font <- function(name, family = name, ...) {
initialize_font <- function(name, family = name, local=NULL, ...) {
if (missing(name)) stop('`name` must be specified.')
curr_families <- sysfonts::font_families()
if (!family %in% curr_families) {
sysfonts::font_add_google(name = name, family = family, ...)
if (is.null(local)) {
sysfonts::font_add_google(name = name, family = family, ...)
} else {
sysfonts::font_add(family = family, paste0(path.package("ThemePark"), "/fonts/", local))
}
} else {
invisible(curr_families)
}
Expand Down
Binary file added inst/fonts/Dune_Rise.ttf
Binary file not shown.
94 changes: 94 additions & 0 deletions inst/fonts/Dune_Rise_License.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
Copyright (c) 2021, Fontswan(https://fontswan.com),
with Reserved Font Name Dune Rise.

This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL


-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).

"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.

"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are
not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
2 changes: 2 additions & 0 deletions man/ThemePark-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions man/dune_theme_colors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions man/scale_dune.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 63e711f

Please sign in to comment.