Skip to content

Commit

Permalink
Update tests (#13)
Browse files Browse the repository at this point in the history
* Update `df_covid` (#1)

* Update package data

* Add versioning to NEWS.md and DESCRIPTION

* Fix na values (#5)

* Update `df_covid` (#1) (#2)

* Update package data

* Add versioning to NEWS.md and DESCRIPTION

* fixing for NA values

* fixing for NA values

Co-authored-by: Seth Caldwell <caldwellst@gmail.com>

* Update NEWS.md (#6)

* versioning (#7)

* Hjust y labels (#9)

* Update y-axis label justification

* Update y-axis label justification

* Remove deprecated argument from element_line

* Update tests

* Move ultra-light to last option in multicolor palette

* Implement display_pal

* Update README links

* Address comments

---------

Co-authored-by: Rawa Ganjani <rawaomer@outlook.com>
  • Loading branch information
caldwellst and Rawa-Ganjani authored Aug 16, 2023
1 parent c90824b commit 697174f
Show file tree
Hide file tree
Showing 35 changed files with 473 additions and 262 deletions.
1 change: 0 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
^README\.Rmd$
^\.Rproj\.user$
^\.github$
^\.pre-commit-config\.yaml$
^codecov\.yml$
^data-raw$
^gghdx\.Rproj$
81 changes: 0 additions & 81 deletions .pre-commit-config.yaml

This file was deleted.

9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: gghdx
Title: HDX Theme, Scales, and Other Conveniences for 'ggplot2'
Version: 0.0.2
Version: 0.1
Authors@R:
person(
given = "Seth",
Expand All @@ -14,13 +14,16 @@ Description: An HDX theme, color palettes, and scales for 'ggplot2' to allow
License: GPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
Imports:
dplyr,
ggplot2,
ggthemes,
magrittr,
purrr,
rlang,
showtext,
sysfonts,
tibble
Depends:
R (>= 2.10)
Expand All @@ -31,8 +34,6 @@ Suggests:
knitr,
rmarkdown,
scales,
showtext,
sysfonts,
testthat (>= 3.0.0)
VignetteBuilder: knitr
URL: https://github.com/caldwellst/gghdx
Expand Down
17 changes: 13 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@ export(hdx_color_names)
export(hdx_colors)
export(hdx_colour_names)
export(hdx_colours)
export(hdx_display_pal)
export(hdx_hex)
export(hdx_pal)
export(hdx_pal_discrete)
export(hdx_pal_gray)
export(hdx_pal_mint)
export(hdx_pal_sapphire)
export(hdx_pal_tomato)
export(load_source_sans_pro)
export(scale_color_gradient2_hdx)
export(scale_color_gradient_hdx)
export(scale_color_gradient_hdx_mint)
export(scale_color_gradient_hdx_sapphire)
export(scale_color_gradient_hdx_tomato)
export(scale_color_hdx)
export(scale_color_hdx_discrete)
export(scale_color_hdx_gray)
export(scale_color_hdx_grey)
export(scale_color_hdx_mint)
export(scale_color_hdx_sapphire)
export(scale_color_hdx_tomato)
Expand All @@ -27,7 +32,9 @@ export(scale_colour_gradient_hdx)
export(scale_colour_gradient_hdx_mint)
export(scale_colour_gradient_hdx_sapphire)
export(scale_colour_gradient_hdx_tomato)
export(scale_colour_hdx)
export(scale_colour_hdx_discrete)
export(scale_colour_hdx_gray)
export(scale_colour_hdx_grey)
export(scale_colour_hdx_mint)
export(scale_colour_hdx_sapphire)
export(scale_colour_hdx_tomato)
Expand All @@ -36,7 +43,9 @@ export(scale_fill_gradient_hdx)
export(scale_fill_gradient_hdx_mint)
export(scale_fill_gradient_hdx_sapphire)
export(scale_fill_gradient_hdx_tomato)
export(scale_fill_hdx)
export(scale_fill_hdx_discrete)
export(scale_fill_hdx_gray)
export(scale_fill_hdx_grey)
export(scale_fill_hdx_mint)
export(scale_fill_hdx_sapphire)
export(scale_fill_hdx_tomato)
Expand Down
13 changes: 13 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# gghdx 0.1.0

* Right adjusting y-axis labels in `theme_hdx()`.
* Replace deprecated `size` argument from `ggplot2` calls.
* Move `ultra-light` colors to end of `hdx_pal()` discrete palette.
* Make output of `hdx_hex()` unnamed to avoid errors in `ggplot2` and
allow both grey and gray spellings of colors.
* Change `..._gradient_...` scale functions to have `white` as the low color.
* Add `display_hdx_pal()` function to allow visualizing the HDX color palettes.
* Improved error catching when fonts are not available.
* Added gray color palette, `hdx_pal_gray()`.

# gghdx 0.0.2

* Cleaned `df_covid` for consistent days between dates on x-axis plots.
* Changed `NA` colors from white to gray.

# gghdx 0.0.1

Expand Down
68 changes: 68 additions & 0 deletions R/display_hdx_pal.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#' Display HDX palette
#'
#' Displays the HDX color palettes. By default, shows all values for all
#' palettes. You can change the number of values for each palette or only
#' show a subset of the available palettes (from `hdx_pal_...()`).
#'
#' @param n Number of colors for each palette to show.
#' @param palette Character vector of palettes to show.
#'
#' @return Plot of HDX color palettes.
#' @export
hdx_display_pal <- function(
n = NULL,
palette = c("discrete", "gray", "mint", "sapphire", "tomato")
) {
# check the palette argument is correct
palette <- rlang::arg_match(
arg = palette,
multiple = TRUE
)

pal_funs <- list(
discrete = hdx_pal_discrete(),
gray = hdx_pal_gray(),
mint = hdx_pal_mint(),
sapphire = hdx_pal_sapphire(),
tomato = hdx_pal_tomato()
)

purrr::map(
.x = palette,
.f = \(pal) {
pal_fun <- pal_funs[[pal]]
if (is.null(n)) {
n <- attributes(pal_fun)$max_n
}

data.frame(
palette = pal,
value = pal_fun(n),
idx = 1:n
)
}
) %>%
purrr::list_rbind() %>%
ggplot2::ggplot(
ggplot2::aes(
x = idx,
y = palette,
fill = I(value)
)
) +
ggplot2::geom_tile(
color = "white",
lwd = 1
) +
theme_hdx() +
ggplot2::theme(
panel.grid = ggplot2::element_blank(),
axis.line = ggplot2::element_blank(),
axis.text.x = ggplot2::element_blank()
) +
ggplot2::labs(
x = "",
y = "",
title = "gghdx palettes"
)
}
25 changes: 9 additions & 16 deletions R/gghdx.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ gghdx <- function(showtext = TRUE,
horizontal = TRUE) {
colors <- hdx_colors()

# check the fonts are loaded correctly
if (showtext) {
load_source_sans_pro()
}

check_font(base_family)

# set the theme
ggplot2::theme_set(
theme_hdx(
Expand All @@ -37,24 +44,10 @@ gghdx <- function(showtext = TRUE,
)

# set default scales
options("ggplot2.discrete.fill" = scale_fill_hdx)
options("ggplot2.discrete.colour" = scale_color_hdx)
options("ggplot2.discrete.fill" = scale_fill_hdx_discrete)
options("ggplot2.discrete.colour" = scale_color_hdx_discrete)
options("ggplot2.continuous.fill" = scale_fill_gradient_hdx_mint)
options("ggplot2.continuous.colour" = scale_color_gradient_hdx_sapphire)

if (showtext) {
st_chk <- requireNamespace("showtext", quietly = TRUE)
sf_chk <- requireNamespace("sysfonts", quietly = TRUE)
if (!(st_chk && sf_chk)) {
stop(
"The showtext and sysfonts packages must be installed to automatically",
"load and use the Source Sans Pro font.",
call. = FALSE
)
}
sysfonts::font_add_google("Source Sans Pro")
showtext::showtext_auto()
}
}

#' Default geometry fills
Expand Down
9 changes: 8 additions & 1 deletion R/hdx_colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ hdx_hex <- function(color_names) {
if (all(is.na(color_names)) && ln == 1) {
return(NA_character_)
}
# allow for alternate spelling of gray
color_names <- gsub(
pattern = "grey",
replacement = "gray",
x = color_names
)

if (!all(color_names %in% hdx_color_names()) || ln == 0) {
stop(
Expand All @@ -65,7 +71,8 @@ hdx_hex <- function(color_names) {
call. = FALSE
)
}
hdx_colors()[color_names]

unname(hdx_colors()[color_names])
}

#' @rdname hdx_color
Expand Down
Loading

0 comments on commit 697174f

Please sign in to comment.