-
Notifications
You must be signed in to change notification settings - Fork 13
/
theme_avatar.R
111 lines (103 loc) · 3.42 KB
/
theme_avatar.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
## Velu P K Immonen
## theme_avatar
## 07/26/2023
# COLOR: add, remove, or edit the colors to fit your scheme (hex code preferred, but you can use any type). Names should be
#' Avatar Theme Color Palette
#'
#' @format character vector of hex code strings
#' @export
#' @concept avatar
#'
#' @examples
#' avatar_theme_colors
#'
avatar_theme_colors <- c(
border = '#0F2347',
dark = '#1C3F6E',
medium = '#2E67A0',
light = '#5AACCF',
point = '#EFFC93'
)
# THEME: rename function and theme() arguments according to your theme design, feel free to edit this how you would like
#' Avatar Inspired Theme
#'
#' @param avatar_font should `theme_avatar` use Google Font's IM Fell English? Default is `TRUE`.
#' @param ... additional parameters to pass to `ggplot2::theme()`
#'
#' @return a `ggplot2` `theme` element
#' @export
#' @concept avatar
#'
#' @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 = 'Avatar Scatter Plot') +
#' theme_avatar()
#'
#' 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") +
#' theme_avatar()
#'
theme_avatar <- function(avatar_font = TRUE, ...){
# CUSTOM FONT: add a custom font from google fonts
font_family = ifelse(avatar_font,"avatar","sans") # use this line if you have a custom font
if (avatar_font) {
initialize_font(name = "IM Fell English", family = "avatar")
}
# CUSTOM THEME:
ggplot2::theme(
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
text = element_text(color = avatar_theme_colors["border"], family = font_family),
title = element_text(size=20),
panel.background = element_rect(fill = avatar_theme_colors["dark"]),
panel.border = element_rect(fill = NA, color = avatar_theme_colors["border"],linewidth=1.2),
axis.title = element_text(size=17),
axis.text = element_text(size=13,color = avatar_theme_colors["border"]),
axis.ticks = element_line(color = avatar_theme_colors["border"],linewidth=1),
legend.background = element_rect(fill = avatar_theme_colors["dark"], color = NA),
strip.background = element_rect(fill = avatar_theme_colors["point"],
colour = avatar_theme_colors["border"]),
strip.text = element_text(colour = avatar_theme_colors["border"]),
...
)
}
# COLOR SCALES: Make pretty color scales
#' Avatar Inspired Color Scales
#'
#' @param ... Additional arguments to pass to `ggplot2::scale_[fill/color]_gradient()`
#'
#' @return a `ggplot` scale object
#'
#' @rdname scale_avatar
#' @export
#' @concept avatar
#'
#' @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") +
#' scale_color_avatar()
#'
scale_fill_avatar <- function(...) {
ggplot2::scale_fill_gradient(low = avatar_theme_colors["medium"], high = avatar_theme_colors["dark"], ...)
}
#' @rdname scale_avatar
#' @export
#' @concept avatar
scale_color_avatar <- function(...) {
ggplot2::scale_color_gradient(low = avatar_theme_colors["medium"], high = avatar_theme_colors["dark"], ...)
}