Skip to content

Commit

Permalink
Merge pull request #123 from CMAP-REPOS/version1.2
Browse files Browse the repository at this point in the history
Version1.2
  • Loading branch information
matthewstern authored Jun 25, 2021
2 parents 878be4b + 74f7bfe commit 2c7912d
Show file tree
Hide file tree
Showing 41 changed files with 1,462 additions and 862 deletions.
29 changes: 23 additions & 6 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Automatically rebuilds pkgdown website any time master branch is updated.
# Also builds pkgdown on "gh-pages-test" on commits to pull requests.
# Based on <https://github.com/r-lib/actions/blob/master/examples/pkgdown.yaml>.
# Conditional based on <https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables>.
on:
push:
branches: master
pull_request:

name: pkgdown

Expand Down Expand Up @@ -56,18 +59,32 @@ jobs:
install.packages("pkgdown")
shell: Rscript {0}

- name: Check Whitney availability
- name: Check Whitney availability in R
run: |
message(paste(sysfonts::font_paths(), collapse = "\n"))
all_fonts <- sysfonts::font_files()
message(paste(all_fonts[all_fonts$family %in% c("Whitney Medium", "Whitney Book", "Whitney Semibold") & all_fonts$face=="Regular", "file"], collapse = "\n"))
all_fonts <- systemfonts::system_fonts()
message("WHITNEY FONTS AUTOMATICALLY AVAILABLE TO SYSTEMFONTS:")
message(paste(all_fonts$name[grepl("^Whitney", all_fonts$name)], collapse = "\n"))
user_dir <- paste0(Sys.getenv("HOME"), "/Library/Fonts")
library_fonts <- list.files(user_dir)
message(paste0("WHITNEY FONTS IN ", user_dir, " (MUST BE REGISTERED):"))
message(paste(library_fonts[grepl("^Whitney", library_fonts)], collapse = "\n"))
shell: Rscript {0}

- name: Install package
run: R CMD INSTALL .

- name: Deploy package
- name: Deploy package to live branch
if: ${{ github.ref == 'refs/heads/main' }}
run: |
echo "This is $GITHUB_REF. Deploying to gh-pages branch."
git config --local user.email "[email protected]"
git config --local user.name "GitHub Actions"
Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE, clean = TRUE)'
Rscript -e 'pkgdown::deploy_to_branch(clean = TRUE)'
- name: Deploy package to test branch
if: ${{ github.ref != 'refs/heads/main' }}
run: |
echo "This is $GITHUB_REF. Deploying to gh-pages-test branch."
git config --local user.email "[email protected]"
git config --local user.name "GitHub Actions"
Rscript -e 'pkgdown::deploy_to_branch(branch="gh-pages-test", clean = TRUE)'
14 changes: 7 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: cmapplot
Title: CMAP Themes and Color Palettes
Version: 1.1.0
Version: 1.2.0
Authors@R: c(
person("Matthew", "Stern",
role = c("aut", "cre"),
Expand All @@ -24,7 +24,7 @@ Authors@R: c(
role = "aut",
email = "[email protected]"),
person("Chicago Metropolitan Agency for Planning",
role = "cph"))
role = c("cph", "fnd")))
Description: Provides themes and color scales for 'ggplot2', based on Chicago
Metropolitan Agency for Planning (CMAP) design guidelines.
URL: https://cmap-repos.github.io/cmapplot, https://github.com/CMAP-REPOS/cmapplot
Expand All @@ -45,20 +45,20 @@ Imports:
grid,
gridExtra,
gridtext,
lubridate,
magrittr,
purrr,
ragg,
rlang,
rstudioapi,
scales,
stringr,
sysfonts
systemfonts,
tibble
Suggests:
knitr,
lubridate,
readxl,
RCurl,
rmarkdown,
testthat,
tibble,
tidyverse
RoxygenNote: 7.1.1
VignetteBuilder: knitr
21 changes: 16 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(GeomPointLast)
export(GeomRecessions)
export(GeomRecessionsText)
export(GeomTextLast)
export(abbr_years)
export(apply_cmap_default_aes)
export(cmap_color_continuous)
export(cmap_color_discrete)
Expand All @@ -18,14 +19,14 @@ export(cmap_fill_continuous)
export(cmap_fill_discrete)
export(cmap_fill_highlight)
export(cmap_fill_race)
export(cmap_gradients)
export(cmap_palettes)
export(cmapplot_globals)
export(fetch_pal)
export(finalize_plot)
export(geom_recessions)
export(geom_text_lastonly)
export(get_cmapplot_global)
export(get_cmapplot_globals)
export(gg_lwd_convert)
export(integer_breaks)
export(set_cmapplot_global)
export(theme_cmap)
export(unapply_cmap_default_aes)
export(update_recessions)
Expand All @@ -37,17 +38,27 @@ import(grDevices)
import(graphics)
import(grid)
import(gridtext)
import(ragg)
import(rlang)
import(rstudioapi)
import(scales)
import(systemfonts)
importFrom(generics,intersect)
importFrom(ggpubr,get_legend)
importFrom(glue,glue)
importFrom(glue,glue_collapse)
importFrom(gridExtra,arrangeGrob)
importFrom(lubridate,day)
importFrom(lubridate,decimal_date)
importFrom(lubridate,month)
importFrom(lubridate,year)
importFrom(purrr,compact)
importFrom(purrr,map)
importFrom(purrr,walk2)
importFrom(stats,na.omit)
importFrom(stringr,str_length)
importFrom(stringr,str_replace)
importFrom(stringr,str_trunc)
importFrom(sysfonts,font_files)
importFrom(tibble,tribble)
importFrom(utils,modifyList)
importFrom(utils,read.csv)
31 changes: 31 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
# cmapplot 1.2.0
PR # 123

This is a version-level update that includes many fixes and new features and introduces new dependencies, most significantly RStudio 1.4 or greater.

### New features
* cmapplot now utilizes the new systemfonts package for custom font rendering, rather than sysfonts on Windows and X11fonts on Mac and other Unix-based machines. In addition, raster exports from `finalize_plot` now rely on the new raster drawing package ragg. These changes improve font accuracy and consistency across platforms. Note that this improvement does not yet extend to svg outputs, but may in the future (#134).
* When an axis breaks are 4-digit years, new function `abbr_years` allows the conversion of specific years to 2-digit abbreviations, as is common on some designed CMAP graphics.
* `cmapplot_globals`, which contains key package constants, is now an internal environment rather than an exported list. It can be accessed via new functions `get_cmapplot_globals` and `get_cmapplot_global`. Constants can now be overridden by the user for the current session by using `set_cmapplot_global`. Note that this does not yet apply to geom aesthetics set by the package, but may in the future (issue #117).
* All palettes programmed into the package have been moved into a tibble at `cmapplot_globals$palettes`. A new function `fetch_pal` can be used to get details about any specific palette.

### Bug fixes
* Continuous color gradients can now be used on discrete color scales. E.g. `cmap_color_discrete` and `cmap_fill_discrete` can now call gradients as well as discrete palettes (see #70 and #119)
* Nomenclature in help files (e.g. "palette" vs "scale") has been adjusted for clarity.
* internal table `recessions` has been updated to include the business cycle contraction that began in Feb 202 (no end date for this cycle yet).
* `geom_recessions` and related `update_recessions` have been updated to allow for ongoing recessions, improve NBER source for recessions table, and decrease likelihood of data fetch errors.

### Backend changes
* pkgdown site now correctly displays Whitney fonts and margin description images
* backend script reorganization
* improvements to pkgdown build github action, including ability to publish a test site

### Backward compatibility notes
* **This package will now only render Whitney Fonts in RStudio when RStudio version >= 1.4**
* `finalize_plot`'s `window` mode has been disabled for now, due to inability to use ragg drivers in independent window devices. Use `mode = "plot"` and click the "Zoom" button in the plot window instead.
* `cmapplot_globals`, the exported list of package constants, has been removed (See new features `set_cmapplot_global` etc)
* Color lists `cmap_palettes` and `cmap_gradients` have been removed (This information has been moved to `cmapplot_globals$palettes`. To access palette colors directly, use, say `fetch_pal("reds")` rather than `cmap_gradients$reds`.
* `viz_palette` and `viz_gradient` now take as a first argument either the name of a palette (e.g. `"reds"`) or the color palette itself (e.g. `fetch_pal("reds")`). `viz_palette(cmap_gradient$reds)` no longer works.
* `integer_breaks` removed from package


# cmapplot 1.1.0
PR #111 | February 24, 2021

Expand Down
124 changes: 124 additions & 0 deletions R/axis_handling.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#'Axis handling helper functions
#'
#'`abbr_years()` is a helper functions that allows users to abbreviate year
#'labels to their two-digit representation (e.g., 2008 to '08), but not
#'abbreviate any specified breaks. It does so by creating a new function that
#'takes the breaks supplied by \code{ggplot2} as its only argument. The
#'function was modeled after the syntax and approach of the labeling functions
#'in the \code{scales::label_*} family.
#'
#'@importFrom stringr str_length
#'@importFrom lubridate year month day
#'@importFrom stats na.omit
#'
#'@examples
#'
#'# basic functionality
#'abbr_years()(c(2010:2020))
#'abbr_years(full_by_year = 2000)(c(1990:2010))
#'
#'
#' # Default implementation - this will abbreviate all labels except the first
#' # for both continuous and date scales.
#'
#' df2 <- dplyr::mutate(transit_ridership, year2 = as.Date(lubridate::date_decimal(year)))
#' df1 <- dplyr::filter(df2, year >= 2000)
#'
#' ggplot(df1,
#' aes(x = year, y = ridership, color = system)) +
#' geom_line() +
#' scale_x_continuous(labels = abbr_years())
#'
#' ggplot(df1,
#' aes(x = year2, y = ridership, color = system)) +
#' geom_line() +
#' scale_x_date(labels = abbr_years(dateaxis = TRUE))
#'
#' # If customizations are desired, users can use \code{full_by_pos} and/or
#' # \code{full_by_year} to maintain the full version of the specified labels.
#'
#' ggplot(df2,
#' aes(x = year2, y = ridership, color = system)) +
#' geom_line() +
#' scale_x_date(labels = abbr_years(full_by_year = c(2000), dateaxis = TRUE))
#'
#' # You can also remove the default maintenance of the first label and only
#' # specify specific years.
#' ggplot(df2,
#' aes(x = year, y = ridership, color = system)) +
#' geom_line() +
#' scale_x_continuous(labels = abbr_years(full_by_pos = NULL,
#' full_by_year = c(1990,2020)))
#'
#'
#'@param full_by_pos Vector of integers, the position of breaks that should not
#' be abbreviated. This defaults to \code{c(1)}, which retains the original
#' first label and abbreviates subsequent ones. If all breaks should be
#' abbreviated, this can be set to NULL.
#'@param full_by_year Vector of integers, the value of breaks that should not be
#' abbreviated. Defaults to NULL.
#'@param dateaxis Bool. \code{FALSE}, the default, directs the function to treat
#' the breaks as integers. If set to \code{TRUE} the function will instead
#' treat the breaks as date objects. \code{TRUE} should be used when called
#' within a \code{scale_*_date} ggplot element.
#'
#'@export
abbr_years <- function(full_by_pos = c(1),
full_by_year = NULL,
dateaxis = FALSE) {

fxn <- function(breaks) {

# If a date axis, breaks are stored by ggplot as the number of days since
# the origin date of January 1, 1970. These must be converted to integer
# years, but this should error if all breaks don't fall on the same calendar
# day of a distinct year.
if (dateaxis) {
dates <- as.Date(breaks, origin = "1970-01-01")

if (length(unique(month(stats::na.omit(dates)))) != 1 |
length(unique(day(stats::na.omit(dates)))) != 1) {
message(paste(
paste("Currently, breaks are:", paste(dates[!is.na(dates)], collapse = ", ")),
"This function only works if all breaks are on identical calendar days.",
sep = "\n")
)
stop("Breaks cannot be abbreviated.", call. = FALSE)
}

breaks <- lubridate::year(dates)
}

# Stop if the breaks are not in a four-digit format.
if (!all(stringr::str_length(breaks) == 4, na.rm = TRUE)) {
message(paste(
paste("Currently, breaks are:", paste(breaks[!is.na(breaks)], collapse = ", ")),
"Remove any breaks that contain decimals. Consider `breaks = scales::pretty_breaks()`",
"If the axis is in date format, use `abbr_years(dateaxis = TRUE)`.",
sep = "\n")
)
stop("Breaks cannot be abbreviated.", call. = FALSE)
}

# Abbreviate all values
abbr <- paste0("'",substr(breaks,3,4))

# If there is a leading NA, increment up positions accordingly
leading_na <- which.min(is.na(breaks)) - 1
if(!is.null(full_by_pos)) {
full_by_pos <- full_by_pos + leading_na
}

# Convert specified years into positions
if(!is.null(full_by_year)) {
full_by_pos <- sort(unique(c(full_by_pos,match(full_by_year,breaks))))
}

# Add back full years for specified positions
abbr[full_by_pos] <- breaks[full_by_pos]

return(abbr)
}

return(fxn)
}
Loading

0 comments on commit 2c7912d

Please sign in to comment.