Skip to content

Commit

Permalink
Merge pull request #39 from MatthewBJane/elf
Browse files Browse the repository at this point in the history
Add Elf theme 🎄
  • Loading branch information
MatthewBJane authored Dec 20, 2023
2 parents 38cb151 + 8d75de1 commit c2b98c0
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 1 deletion.
12 changes: 12 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export(alien_theme_colors)
export(asteroid_city_theme_colors)
export(avatar_theme_colors)
export(barbie_theme_colors)
export(elf_palette)
export(elf_theme_colors)
export(french_dispatch_theme_colors)
export(friends_palette)
export(friends_theme_colors)
Expand All @@ -24,6 +26,9 @@ export(scale_color_alien)
export(scale_color_asteroid_city)
export(scale_color_avatar)
export(scale_color_barbie)
export(scale_color_elf_b)
export(scale_color_elf_c)
export(scale_color_elf_d)
export(scale_color_french_dispatch)
export(scale_color_friends_b)
export(scale_color_friends_c)
Expand All @@ -46,6 +51,9 @@ export(scale_color_spiderman)
export(scale_color_starwars)
export(scale_color_terminator)
export(scale_color_zelda)
export(scale_colour_elf_b)
export(scale_colour_elf_c)
export(scale_colour_elf_d)
export(scale_colour_friends_b)
export(scale_colour_friends_c)
export(scale_colour_friends_d)
Expand All @@ -56,6 +64,9 @@ export(scale_fill_alien)
export(scale_fill_asteroid_city)
export(scale_fill_avatar)
export(scale_fill_barbie)
export(scale_fill_elf_b)
export(scale_fill_elf_c)
export(scale_fill_elf_d)
export(scale_fill_french_dispatch)
export(scale_fill_friends_b)
export(scale_fill_friends_c)
Expand Down Expand Up @@ -87,6 +98,7 @@ export(theme_alien)
export(theme_asteroid_city)
export(theme_avatar)
export(theme_barbie)
export(theme_elf)
export(theme_french_dispatch)
export(theme_friends)
export(theme_futurama)
Expand Down
181 changes: 181 additions & 0 deletions R/theme_elf.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# Christopher T. Kenny
## theme_elf
## 2023-12-14


#' Elf Inspired Theme Color Palette
#'
#' @format character vector of hex code strings
#' @export
#'
#' @examples
#' elf_theme_colors
#'
elf_theme_colors <- c(
background = '#F9CC14',
fill = '#1B8B24',
text = '#DF1F2B',
panel = '#CAEBF2',
strip_text = '#DF1F2B',
leg_text = '#4E829E',
border = '#0D101B',
lighter = '#CAF6A7',
light = '#9EB64F',
medium = '#335C1A',
dark = '#2A4F1E'
)

#' Elf Inspired Color Palette
#'
#' @format character vector of hex code strings
#' @export
#'
#' @examples
#' elf_palette
#'
elf_palette <- c(
'#F4F3EF', '#F9CC14', '#921B0D', '#9EB64F', '#DF1F2B',
'#2A4F1E', '#432D23', '#15375B','#11131E'
)

#' Elf Inspired Theme
#'
#' @param elf_font should `theme_elf` use custom font? Default is `TRUE`.
#' @param ... additional parameters to pass to `ggplot2::theme()`
#'
#' @return a `ggplot2` `theme` element
#' @export
#'
#' @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') +
#' geom_point() +
#' labs(title = 'elf Scatter Plot') +
#' theme_elf()
#'
#' ggplot(mpg, aes(cty)) +
#' geom_density(aes(fill=factor(cyl)), alpha=0.8, color = NA) +
#' labs(title="Density plot",
#' subtitle="City Mileage Grouped by Number of cylinders",
#' caption="Source: mpg",
#' x="City Mileage",
#' fill="# Cylinders") +
#' theme_elf() +
#' scale_fill_elf_d()
#'
#'
theme_elf <- function(elf_font = TRUE, ...) {

# CUSTOM FONT: add a custom font from google fonts
font_family <- ifelse(elf_font, 'elf', 'sans') # use this line if you have a custom font - change elf to match the font name used
if (elf_font) {
initialize_font(name = "Rufina", family = "elf")
}

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

# COLOR SCALES: Make pretty color scales

#' Elf Inspired Color Scales
#'
#' @param ... Additional arguments to pass to `ggplot2::binned_scale` for `_b`,
#' `ggplot2::scale_[fill/color]_gradient` for `_c`, or `ggplot2::discrete_scale`
#' for `_d`
#'
#' @return a `ggplot` scale object
#'
#' @rdname scale_elf
#' @export
#'
#' @examples
#' library(ggplot2)
#'
#' ggplot(mpg, aes(cty)) +
#' geom_density(aes(fill=factor(cyl)), alpha=0.8) +
#' labs(title="Density plot",
#' subtitle="City Mileage Grouped by Number of cylinders",
#' caption="Source: mpg",
#' x="City Mileage",
#' fill="# Cylinders") +
#' facet_wrap(~(hwy > 29)) +
#' theme_elf() +
#' scale_fill_elf_d()
#'
scale_color_elf_c <- function(...) {
ggplot2::scale_color_gradient(..., low = elf_theme_colors["light"], high = elf_theme_colors["dark"])
}

#' @rdname scale_elf
#' @export
scale_fill_elf_c <- function(...) {
ggplot2::scale_fill_gradient(..., low = elf_theme_colors["light"], high = elf_theme_colors["dark"])
}

#' @rdname scale_elf
#' @export
scale_color_elf_b <- function(...) {
if (!requireNamespace('scales', quietly = TRUE)) {
stop('This function requires the `scales` R package.')
}
ramp <- scales::colour_ramp(c(elf_theme_colors["light"], elf_theme_colors["dark"]))
ggplot2::binned_scale('color', 'elf', palette = ramp, ...)
}

#' @rdname scale_elf
#' @export
scale_fill_elf_b <- function(...) {
if (!requireNamespace('scales', quietly = TRUE)) {
stop('This function requires the `scales` R package.')
}
ramp <- scales::colour_ramp(c(elf_theme_colors["light"], elf_theme_colors["dark"]))
ggplot2::binned_scale('fill', 'elf', palette = ramp, ...)
}


#' @rdname scale_elf
#' @export
scale_color_elf_d <- function(...) {
ggplot2::discrete_scale(aesthetics = 'color', scale_name = elf_palette,
palette = rot_pal(elf_palette), ...)
}

#' @rdname scale_elf
#' @export
scale_fill_elf_d <- function(...) {
ggplot2::discrete_scale(aesthetics = 'fill', scale_name = elf_palette, ...,
palette = rot_pal(elf_palette))
}

#' @rdname scale_elf
#' @export
scale_colour_elf_d <- scale_color_elf_d

#' @rdname scale_elf
#' @export
scale_colour_elf_c <- scale_color_elf_c

#' @rdname scale_elf
#' @export
scale_colour_elf_b <- scale_color_elf_b
3 changes: 2 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ themepark_themes <- as.data.frame(t(data.frame(
c("grand_budapest","Katya Kustova"),
c("asteroid_city","Katya Kustova"),
c("french_dispatch","Katya Kustova"),
c("moonrise_kingdom","Katya Kustova")
c("moonrise_kingdom","Katya Kustova"),
c("elf", "Christopher T. Kenny")
)))
colnames(themepark_themes) = c("theme","creator")
rownames(themepark_themes) = 1:nrow(themepark_themes)
1 change: 1 addition & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Name | Theme | Creator(s) |
**Asteroid City** | `theme_asteroid_city()` | Katya Kustova |
**French Dispatch** | `theme_french_dispatch()` | Katya Kustova |
**Moonrise Kingdom** | `theme_moonrise_kingdom()` | Katya Kustova |
**Elf** | `theme_elf()` | Christopher T. Kenny |


## Installation
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ add it or respond with suggested edits. Have fun with it!
| **Asteroid City** | `theme_asteroid_city()` | Katya Kustova |
| **French Dispatch** | `theme_french_dispatch()` | Katya Kustova |
| **Moonrise Kingdom** | `theme_moonrise_kingdom()` | Katya Kustova |
| **Elf** | `theme_elf()` | Christopher T. Kenny |

## Installation

Expand Down
20 changes: 20 additions & 0 deletions man/elf_palette.Rd

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

20 changes: 20 additions & 0 deletions man/elf_theme_colors.Rd

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

58 changes: 58 additions & 0 deletions man/scale_elf.Rd

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

40 changes: 40 additions & 0 deletions man/theme_elf.Rd

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

Loading

0 comments on commit c2b98c0

Please sign in to comment.