| Title: | Standardizing Outputs in the 'Core Surveillance' Style |
|---|---|
| Description: | A comprehensive styling system for consistent visual outputs using 'Core Surveillance' guidelines. Provides 'ggplot2' themes, color palettes, and dual formatting functions to standardize graphs, tables, and reports. Includes Norwegian formatting conventions for domestic use and international journal standards for academic publications. Features number formatting (percentages, per-100k rates, log transformations), date formatting (Norwegian vs ISO 8601), and a hierarchical color system. Focuses on consistency over flexibility to ensure reproducible, professional outputs across research contexts. |
| Authors: | Richard Aubrey White [aut, cre] (ORCID: <https://orcid.org/0000-0002-6747-1726>), Celine Victoria Berg-Hansen [aut] (ORCID: <https://orcid.org/0000-0002-0033-2019>), Chi Zhang [aut], Folkehelseinstituttet [cph] |
| Maintainer: | Richard Aubrey White <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 2026.8.21 |
| Built: | 2026-09-26 06:28:26 UTC |
| Source: | https://github.com/niphr/csstyle |
Reads the lines of a file on GitHub and returns them as a
github_code object. The function rewrites a "blob" URL to the
matching raw URL before it reads the file.
print.github_code prints the object with line numbers and an
optional link back to GitHub.
as_github_code(url)as_github_code(url)
url |
Character string. A GitHub URL to a file. Use either a "blob" URL,
for example |
An object of class github_code: a character vector of the
file's lines with a pretty_url attribute.
print.github_code, whose examples run without a
network connection. vignette("csstyle") demonstrates neither
function.
## Not run: # Kept in \dontrun{}: readLines() fetches the URL, so this example needs an # internet connection. x <- as_github_code("https://github.com/niphr/csstyle/blob/main/README.md") print(x, lines = 1:3) ## End(Not run)## Not run: # Kept in \dontrun{}: readLines() fetches the URL, so this example needs an # internet connection. x <- as_github_code("https://github.com/niphr/csstyle/blob/main/README.md") print(x, lines = 1:3) ## End(Not run)
A list of the named colors, the base color, and the predefined color palettes. The Core Surveillance styling functions take their colors from this list.
colorscolors
A list with the following elements:
Named character vector of 12 colors, "H1" to "H12". "H1" to "H7" are hex codes. "H8" to "H12" are R color names.
The base color, element "H1" of named_colors.
Named list of palettes: primary_1 to
primary_12 (1 to 12 levels), warning_3 (3 levels) and
posneg_2 (2 levels). The posneg family has no 1-level
palette, so scale_color_cs(palette = "posneg") accepts 2 levels
only.
Character vector of the palette family names ("primary", "posneg", "warning").
A list. See the Format section for the elements.
Other csstyle_utilities:
display_all_palettes(),
every_nth(),
set_global()
# Access named colors colors$named_colors["H1"] # View primary palette colors$palettes$primary_3 # See all available palettes names(colors$palettes)# Access named colors colors$named_colors["H1"] # View primary palette colors$palettes$primary_3 # See all available palettes names(colors$palettes)
Creates a plot that shows all available Core Surveillance color palettes with their color codes and names.
display_all_palettes()display_all_palettes()
A ggplot2 plot that shows all the color palettes.
Other csstyle_utilities:
colors,
every_nth(),
set_global()
# Display all available color palettes display_all_palettes()# Display all available color palettes display_all_palettes()
Creates a function that returns every nth element of a vector. Use it for axis breaks in ggplot2.
every_nth(n)every_nth(n)
n |
Integer. Which nth value to return: |
A function that returns every nth discrete value.
Other csstyle_utilities:
colors,
display_all_palettes(),
set_global()
every_nth(4)(c(1:10)) ## Not run: scale_x_discrete(NULL, breaks = every_nth(n = 2)) ## End(Not run)every_nth(4)(c(1:10)) ## Not run: scale_x_discrete(NULL, breaks = every_nth(n = 2)) ## End(Not run)
Converts age codes such as "009" or "005_014" to readable Norwegian age labels.
Converts age codes to readable Norwegian age labels and returns them as a character vector.
format_cstidy_age_as_factor(x) format_cstidy_age_as_character(x)format_cstidy_age_as_factor(x) format_cstidy_age_as_character(x)
x |
Character vector of age codes or age group codes. |
Factor vector of Norwegian age labels, for example "9 år" or "5-14 år".
Character vector of Norwegian age labels, for example "9 år" or "5-14 år".
format_location_code_as_factor applies the same factor
and character pair to Norwegian location codes.
vignette("csstyle") does not demonstrate these two age functions.
csstyle::format_cstidy_age_as_factor("009") csstyle::format_cstidy_age_as_factor("005-014") csstyle::format_cstidy_age_as_character("009") csstyle::format_cstidy_age_as_character("005-014")csstyle::format_cstidy_age_as_factor("009") csstyle::format_cstidy_age_as_factor("005-014") csstyle::format_cstidy_age_as_character("009") csstyle::format_cstidy_age_as_character("005-014")
Formats dates and datetimes with the ISO 8601 conventions that journals use.
format_date_as_journal(x = lubridate::today(), format = "%Y-%m-%d") format_datetime_as_journal( x = lubridate::now(), format = "%Y-%m-%d %H:%M:%S" ) format_datetime_as_journal_file( x = lubridate::now(), format = "%Y_%m_%d_%H%M%S" )format_date_as_journal(x = lubridate::today(), format = "%Y-%m-%d") format_datetime_as_journal( x = lubridate::now(), format = "%Y-%m-%d %H:%M:%S" ) format_datetime_as_journal_file( x = lubridate::now(), format = "%Y_%m_%d_%H%M%S" )
x |
Date or datetime object to format. |
format |
Character string that sets the output format. |
Character vector of journal-formatted dates and datetimes.
format_date_as_nor gives the Norwegian equivalents.
The "Date format comparison" section of vignette("csstyle")
demonstrates format_date_as_journal() and
format_datetime_as_journal(). That section does not demonstrate
format_datetime_as_journal_file().
# Format current date/time using journal conventions (ISO 8601) format_date_as_journal() format_datetime_as_journal() # Format specific dates format_date_as_journal(as.Date("2023-12-25")) format_datetime_as_journal_file(as.POSIXct("2023-12-25 14:30:00"))# Format current date/time using journal conventions (ISO 8601) format_date_as_journal() format_datetime_as_journal() # Format specific dates format_date_as_journal(as.Date("2023-12-25")) format_datetime_as_journal_file(as.POSIXct("2023-12-25 14:30:00"))
Formats dates and datetimes with Norwegian conventions, or with a filename-safe format.
format_date_as_nor(x = lubridate::today(), format = "%d.%m.%Y") format_datetime_as_nor( x = lubridate::now(), format = "%d.%m.%Y kl. %H:%S" ) format_datetime_as_file(x = lubridate::now(), format = "%Y_%m_%d_%H%M%S")format_date_as_nor(x = lubridate::today(), format = "%d.%m.%Y") format_datetime_as_nor( x = lubridate::now(), format = "%d.%m.%Y kl. %H:%S" ) format_datetime_as_file(x = lubridate::now(), format = "%Y_%m_%d_%H%M%S")
x |
Date or datetime object to format. |
format |
Character string that sets the output format. |
Character vector of formatted dates and datetimes.
format_date_as_journal gives the ISO 8601 equivalents.
The "Norwegian date format" section of vignette("csstyle")
demonstrates all three functions on this page.
# Format current date/time using Norwegian conventions format_date_as_nor() format_datetime_as_nor() # Format specific dates format_date_as_nor(as.Date("2023-12-25")) format_datetime_as_file(as.POSIXct("2023-12-25 14:30:00"))# Format current date/time using Norwegian conventions format_date_as_nor() format_datetime_as_nor() # Format specific dates format_date_as_nor(as.Date("2023-12-25")) format_datetime_as_file(as.POSIXct("2023-12-25 14:30:00"))
Converts Norwegian location codes (municipality and county codes) to a factor with human-readable location names.
Converts Norwegian location codes to a character vector of human-readable Norwegian location names.
format_location_code_as_factor( x, label = NULL, label_if_not_unique = NULL, reference = csdata::nor_locations_names(), direction = 1 ) format_location_code_as_character( x, label = NULL, label_if_not_unique = NULL, reference = csdata::nor_locations_names(), direction = 1 )format_location_code_as_factor( x, label = NULL, label_if_not_unique = NULL, reference = csdata::nor_locations_names(), direction = 1 ) format_location_code_as_character( x, label = NULL, label_if_not_unique = NULL, reference = csdata::nor_locations_names(), direction = 1 )
x |
Character vector of Norwegian location codes. |
label |
Column name in the reference data to use for labels. Defaults to the global setting. |
label_if_not_unique |
Column name to use when the location names are not unique. Defaults to the global setting. |
reference |
Data.table of location reference data. Default
|
direction |
Integer. Use 1 for the normal order and -1 for the reversed order. Default 1. |
Factor vector with Norwegian location names as labels.
Character vector of Norwegian location names.
set_global changes the default label and
label_if_not_unique columns.
format_cstidy_age_as_factor applies the same factor and
character pair to cstidy age codes. vignette("csstyle") does not
demonstrate these two location functions.
# Convert location codes to factors (requires csdata package) location_codes <- c("03", "11", "15") format_location_code_as_factor(location_codes) format_location_code_as_character(location_codes)# Convert location codes to factors (requires csdata package) location_codes <- c("03", "11", "15") format_location_code_as_factor(location_codes) format_location_code_as_character(location_codes)
Applies 10^x, then formats with journal conventions and 1 decimal place.
Applies 10^x, then formats with journal conventions and 2 decimal places.
format_num_as_journal_invlog10_1(x) format_num_as_journal_invlog10_2(x)format_num_as_journal_invlog10_1(x) format_num_as_journal_invlog10_2(x)
x |
Numeric vector of values on the log-10 scale. |
Character vector of journal-formatted numbers.
Character vector of journal-formatted numbers.
Other csstyle_journal_formatters:
format_num_as_journal_invlog2_1(),
format_num_as_journal_invloge_1(),
format_num_as_journal_num_0(),
format_num_as_journal_per100k_0(),
format_num_as_journal_perc_0()
# Transform log10 values back to original scale format_num_as_journal_invlog10_1(c(1, 2, 3)) # 10^1, 10^2, 10^3 format_num_as_journal_invlog10_2(c(1, 2, 3))# Transform log10 values back to original scale format_num_as_journal_invlog10_1(c(1, 2, 3)) # 10^1, 10^2, 10^3 format_num_as_journal_invlog10_2(c(1, 2, 3))
Applies 2^x, then formats with journal conventions and 1 decimal place.
Applies 2^x, then formats with journal conventions and 2 decimal places.
format_num_as_journal_invlog2_1(x) format_num_as_journal_invlog2_2(x)format_num_as_journal_invlog2_1(x) format_num_as_journal_invlog2_2(x)
x |
Numeric vector of values on the log-2 scale. |
Character vector of journal-formatted numbers.
Character vector of journal-formatted numbers.
Other csstyle_journal_formatters:
format_num_as_journal_invlog10_1(),
format_num_as_journal_invloge_1(),
format_num_as_journal_num_0(),
format_num_as_journal_per100k_0(),
format_num_as_journal_perc_0()
# Transform log2 values back to original scale format_num_as_journal_invlog2_1(c(1, 2, 3)) # 2^1, 2^2, 2^3 format_num_as_journal_invlog2_2(c(1, 2, 3))# Transform log2 values back to original scale format_num_as_journal_invlog2_1(c(1, 2, 3)) # 2^1, 2^2, 2^3 format_num_as_journal_invlog2_2(c(1, 2, 3))
Applies exp(x), then formats with journal conventions and 1 decimal place.
Applies exp(x), then formats with journal conventions and 2 decimal places.
format_num_as_journal_invloge_1(x) format_num_as_journal_invloge_2(x)format_num_as_journal_invloge_1(x) format_num_as_journal_invloge_2(x)
x |
Numeric vector of values on the natural log scale. |
Character vector of journal-formatted numbers.
Character vector of journal-formatted numbers.
Other csstyle_journal_formatters:
format_num_as_journal_invlog10_1(),
format_num_as_journal_invlog2_1(),
format_num_as_journal_num_0(),
format_num_as_journal_per100k_0(),
format_num_as_journal_perc_0()
# Transform natural log values back to original scale format_num_as_journal_invloge_1(c(0, 1, 2)) # exp(0), exp(1), exp(2) format_num_as_journal_invloge_2(c(0, 1, 2))# Transform natural log values back to original scale format_num_as_journal_invloge_1(c(0, 1, 2)) # exp(0), exp(1), exp(2) format_num_as_journal_invloge_2(c(0, 1, 2))
Formats numeric values with journal conventions and 0 decimal places.
Formats numeric values with journal conventions and 1 decimal place.
Formats numeric values with journal conventions and 2 decimal places.
format_num_as_journal_num_0(x) format_num_as_journal_num_1(x) format_num_as_journal_num_2(x)format_num_as_journal_num_0(x) format_num_as_journal_num_1(x) format_num_as_journal_num_2(x)
x |
Numeric vector to format. |
Character vector of journal-formatted numbers.
Character vector of journal-formatted numbers.
Character vector of journal-formatted numbers.
Other csstyle_journal_formatters:
format_num_as_journal_invlog10_1(),
format_num_as_journal_invlog2_1(),
format_num_as_journal_invloge_1(),
format_num_as_journal_per100k_0(),
format_num_as_journal_perc_0()
# Journal number formatting format_num_as_journal_num_0(c(1234.56, 9876.54, NA)) format_num_as_journal_num_1(c(1234.56, 9876.54, NA)) format_num_as_journal_num_2(c(1234.56, 9876.54, NA))# Journal number formatting format_num_as_journal_num_0(c(1234.56, 9876.54, NA)) format_num_as_journal_num_1(c(1234.56, 9876.54, NA)) format_num_as_journal_num_2(c(1234.56, 9876.54, NA))
Formats numeric values as rates per 100k with journal conventions and 0 decimal places.
Formats numeric values as rates per 100k with journal conventions and 1 decimal place.
Formats numeric values as rates per 100k with journal conventions and 2 decimal places.
format_num_as_journal_per100k_0(x) format_num_as_journal_per100k_1(x) format_num_as_journal_per100k_2(x)format_num_as_journal_per100k_0(x) format_num_as_journal_per100k_1(x) format_num_as_journal_per100k_2(x)
x |
Numeric vector to format. |
Character vector of journal-formatted numbers with a "/100k" suffix.
Character vector of journal-formatted numbers with a "/100k" suffix.
Character vector of journal-formatted numbers with a "/100k" suffix.
Other csstyle_journal_formatters:
format_num_as_journal_invlog10_1(),
format_num_as_journal_invlog2_1(),
format_num_as_journal_invloge_1(),
format_num_as_journal_num_0(),
format_num_as_journal_perc_0()
# Format rates per 100k population format_num_as_journal_per100k_0(c(123.45, 678.90, NA)) format_num_as_journal_per100k_1(c(123.45, 678.90, NA)) format_num_as_journal_per100k_2(c(123.45, 678.90, NA))# Format rates per 100k population format_num_as_journal_per100k_0(c(123.45, 678.90, NA)) format_num_as_journal_per100k_1(c(123.45, 678.90, NA)) format_num_as_journal_per100k_2(c(123.45, 678.90, NA))
Formats numeric values as percentages with journal conventions and 0 decimal places.
Formats numeric values as percentages with journal conventions and 1 decimal place.
Formats numeric values as percentages with journal conventions and 2 decimal places.
format_num_as_journal_perc_0(x) format_num_as_journal_perc_1(x) format_num_as_journal_perc_2(x)format_num_as_journal_perc_0(x) format_num_as_journal_perc_1(x) format_num_as_journal_perc_2(x)
x |
Numeric vector to format. |
Character vector of journal-formatted numbers with a "
Character vector of journal-formatted numbers with a "
Character vector of journal-formatted numbers with a "
Other csstyle_journal_formatters:
format_num_as_journal_invlog10_1(),
format_num_as_journal_invlog2_1(),
format_num_as_journal_invloge_1(),
format_num_as_journal_num_0(),
format_num_as_journal_per100k_0()
# Format percentages format_num_as_journal_perc_0(c(12.34, 56.78, NA)) format_num_as_journal_perc_1(c(12.34, 56.78, NA)) format_num_as_journal_perc_2(c(12.34, 56.78, NA))# Format percentages format_num_as_journal_perc_0(c(12.34, 56.78, NA)) format_num_as_journal_perc_1(c(12.34, 56.78, NA)) format_num_as_journal_perc_2(c(12.34, 56.78, NA))
Applies 10^x, then formats with Norwegian conventions and 1 decimal place.
Applies 10^x, then formats with Norwegian conventions and 2 decimal places.
format_num_as_nor_invlog10_1(x) format_num_as_nor_invlog10_2(x)format_num_as_nor_invlog10_1(x) format_num_as_nor_invlog10_2(x)
x |
Numeric vector of values on the log-10 scale. |
Character vector of Norwegian-formatted numbers.
Character vector of Norwegian-formatted numbers.
Other csstyle_formatters:
format_num_as_nor_invlog2_1(),
format_num_as_nor_invloge_1(),
format_num_as_nor_num_0(),
format_num_as_nor_per100k_0(),
format_num_as_nor_perc_0()
# Transform log10 values back to original scale format_num_as_nor_invlog10_1(c(1, 2, 3)) # 10^1, 10^2, 10^3 format_num_as_nor_invlog10_2(c(1, 2, 3))# Transform log10 values back to original scale format_num_as_nor_invlog10_1(c(1, 2, 3)) # 10^1, 10^2, 10^3 format_num_as_nor_invlog10_2(c(1, 2, 3))
Applies 2^x, then formats with Norwegian conventions and 1 decimal place.
Applies 2^x, then formats with Norwegian conventions and 2 decimal places.
format_num_as_nor_invlog2_1(x) format_num_as_nor_invlog2_2(x)format_num_as_nor_invlog2_1(x) format_num_as_nor_invlog2_2(x)
x |
Numeric vector of values on the log-2 scale. |
Character vector of Norwegian-formatted numbers.
Character vector of Norwegian-formatted numbers.
Other csstyle_formatters:
format_num_as_nor_invlog10_1(),
format_num_as_nor_invloge_1(),
format_num_as_nor_num_0(),
format_num_as_nor_per100k_0(),
format_num_as_nor_perc_0()
# Transform log2 values back to original scale format_num_as_nor_invlog2_1(c(1, 2, 3)) # 2^1, 2^2, 2^3 format_num_as_nor_invlog2_2(c(1, 2, 3))# Transform log2 values back to original scale format_num_as_nor_invlog2_1(c(1, 2, 3)) # 2^1, 2^2, 2^3 format_num_as_nor_invlog2_2(c(1, 2, 3))
Applies exp(x), then formats with Norwegian conventions and 1 decimal place.
Applies exp(x), then formats with Norwegian conventions and 2 decimal places.
format_num_as_nor_invloge_1(x) format_num_as_nor_invloge_2(x)format_num_as_nor_invloge_1(x) format_num_as_nor_invloge_2(x)
x |
Numeric vector of values on the natural log scale. |
Character vector of Norwegian-formatted numbers.
Character vector of Norwegian-formatted numbers.
Other csstyle_formatters:
format_num_as_nor_invlog10_1(),
format_num_as_nor_invlog2_1(),
format_num_as_nor_num_0(),
format_num_as_nor_per100k_0(),
format_num_as_nor_perc_0()
# Transform natural log values back to original scale format_num_as_nor_invloge_1(c(0, 1, 2)) # exp(0), exp(1), exp(2) format_num_as_nor_invloge_2(c(0, 1, 2))# Transform natural log values back to original scale format_num_as_nor_invloge_1(c(0, 1, 2)) # exp(0), exp(1), exp(2) format_num_as_nor_invloge_2(c(0, 1, 2))
Formats numeric values with Norwegian conventions and 0 decimal places.
Formats numeric values with Norwegian conventions and 1 decimal place.
Formats numeric values with Norwegian conventions and 2 decimal places.
format_num_as_nor_num_0(x) format_num_as_nor_num_1(x) format_num_as_nor_num_2(x)format_num_as_nor_num_0(x) format_num_as_nor_num_1(x) format_num_as_nor_num_2(x)
x |
Numeric vector to format. |
Character vector of Norwegian-formatted numbers.
Character vector of Norwegian-formatted numbers.
Character vector of Norwegian-formatted numbers.
Other csstyle_formatters:
format_num_as_nor_invlog10_1(),
format_num_as_nor_invlog2_1(),
format_num_as_nor_invloge_1(),
format_num_as_nor_per100k_0(),
format_num_as_nor_perc_0()
# Basic number formatting format_num_as_nor_num_0(c(1234.56, 9876.54, NA)) format_num_as_nor_num_1(c(1234.56, 9876.54, NA)) format_num_as_nor_num_2(c(1234.56, 9876.54, NA))# Basic number formatting format_num_as_nor_num_0(c(1234.56, 9876.54, NA)) format_num_as_nor_num_1(c(1234.56, 9876.54, NA)) format_num_as_nor_num_2(c(1234.56, 9876.54, NA))
Formats numeric values as rates per 100k with Norwegian conventions and 0 decimal places.
Formats numeric values as rates per 100k with Norwegian conventions and 1 decimal place.
Formats numeric values as rates per 100k with Norwegian conventions and 2 decimal places.
format_num_as_nor_per100k_0(x) format_num_as_nor_per100k_1(x) format_num_as_nor_per100k_2(x)format_num_as_nor_per100k_0(x) format_num_as_nor_per100k_1(x) format_num_as_nor_per100k_2(x)
x |
Numeric vector to format. |
Character vector of Norwegian-formatted numbers with a "/100k" suffix.
Character vector of Norwegian-formatted numbers with a "/100k" suffix.
Character vector of Norwegian-formatted numbers with a "/100k" suffix.
Other csstyle_formatters:
format_num_as_nor_invlog10_1(),
format_num_as_nor_invlog2_1(),
format_num_as_nor_invloge_1(),
format_num_as_nor_num_0(),
format_num_as_nor_perc_0()
# Format rates per 100k population format_num_as_nor_per100k_0(c(123.45, 678.90, NA)) format_num_as_nor_per100k_1(c(123.45, 678.90, NA)) format_num_as_nor_per100k_2(c(123.45, 678.90, NA))# Format rates per 100k population format_num_as_nor_per100k_0(c(123.45, 678.90, NA)) format_num_as_nor_per100k_1(c(123.45, 678.90, NA)) format_num_as_nor_per100k_2(c(123.45, 678.90, NA))
Formats numeric values as percentages with Norwegian conventions and 0 decimal places.
Formats numeric values as percentages with Norwegian conventions and 1 decimal place.
Formats numeric values as percentages with Norwegian conventions and 2 decimal places.
format_num_as_nor_perc_0(x) format_num_as_nor_perc_1(x) format_num_as_nor_perc_2(x)format_num_as_nor_perc_0(x) format_num_as_nor_perc_1(x) format_num_as_nor_perc_2(x)
x |
Numeric vector to format. |
Character vector of Norwegian-formatted numbers with a "
Character vector of Norwegian-formatted numbers with a "
Character vector of Norwegian-formatted numbers with a "
Other csstyle_formatters:
format_num_as_nor_invlog10_1(),
format_num_as_nor_invlog2_1(),
format_num_as_nor_invloge_1(),
format_num_as_nor_num_0(),
format_num_as_nor_per100k_0()
# Format percentages format_num_as_nor_perc_0(c(12.34, 56.78, NA)) format_num_as_nor_perc_1(c(12.34, 56.78, NA)) format_num_as_nor_perc_2(c(12.34, 56.78, NA))# Format percentages format_num_as_nor_perc_0(c(12.34, 56.78, NA)) format_num_as_nor_perc_1(c(12.34, 56.78, NA)) format_num_as_nor_perc_2(c(12.34, 56.78, NA))
Creates a function that generates pretty axis breaks with Norwegian number formatting.
pretty_breaks(n = 5, formatting_fn = csstyle::format_num_as_nor_num_0, ...)pretty_breaks(n = 5, formatting_fn = csstyle::format_num_as_nor_num_0, ...)
n |
Number of desired breaks. Default 5. |
formatting_fn |
Function applied to the break labels. Default
|
... |
Further arguments passed to |
A function that you can use for axis breaks.
format_num_as_nor_num_0 is the default
formatting_fn. every_nth thins labels on a discrete
axis instead. The "Pretty breaks" section of vignette("csstyle")
demonstrates pretty_breaks().
library(ggplot2) # Use pretty breaks with default formatting ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point() + scale_x_continuous(breaks = pretty_breaks(n = 4))library(ggplot2) # Use pretty breaks with default formatting ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point() + scale_x_continuous(breaks = pretty_breaks(n = 4))
Prints the lines of a github_code object with line
numbers. Use the lines argument to print a subset of the lines. Use
include_url or include_url_as_link to also print a link back
to the file on GitHub.
## S3 method for class 'github_code' print(x, ...)## S3 method for class 'github_code' print(x, ...)
x |
A |
... |
Further arguments that control the output. |
The input x, invisibly.
as_github_code builds such an object from a real
GitHub URL. vignette("csstyle") demonstrates neither function.
# as_github_code() needs an internet connection, so this example builds an # equivalent github_code object directly. x <- c("a <- 1", "b <- 2", "a + b") attr(x, "pretty_url") <- "https://github.com/niphr/csstyle/blob/main/demo.R" attr(x, "class") <- c("github_code", class(x)) # All lines, no link print(x) # A subset of lines, with the anchored GitHub link above them print(x, lines = 2:3, include_url = TRUE)# as_github_code() needs an internet connection, so this example builds an # equivalent github_code object directly. x <- c("a <- 1", "b <- 2", "a + b") attr(x, "pretty_url") <- "https://github.com/niphr/csstyle/blob/main/demo.R" attr(x, "class") <- c("github_code", class(x)) # All lines, no link print(x) # A subset of lines, with the anchored GitHub link above them print(x, lines = 2:3, include_url = TRUE)
Saves a ggplot2 plot with A4 paper dimensions.
save_a4(q, filename, landscape = T, scaling_factor = 1)save_a4(q, filename, landscape = T, scaling_factor = 1)
q |
ggplot2 plot object to save. |
filename |
Character string. The output filename, including the extension. |
landscape |
Logical. Set to |
scaling_factor |
Numeric. Scales the A4 dimensions. Default 1. |
save_a4() renders PNG output with the ragg AGG device,
ragg::agg_png. That device is markedly faster than the default
grDevices PNG device for busy, many-geom ggplot2 plots, and it gives
cleaner text. For a filename that is not a PNG, ggsave() selects the
device from the extension.
Nothing (called for side effects).
theme_cs is the Core Surveillance plot theme.
vignette("csstyle") does not demonstrate save_a4().
library(ggplot2) # Create a plot p <- ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point() + theme_cs() ## Not run: # Save in landscape A4 save_a4(p, "myplot.png") # Save in portrait A4 with larger scaling save_a4(p, "myplot_large.png", landscape = FALSE, scaling_factor = 1.5) ## End(Not run)library(ggplot2) # Create a plot p <- ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point() + theme_cs() ## Not run: # Save in landscape A4 save_a4(p, "myplot.png") # Save in portrait A4 with larger scaling save_a4(p, "myplot_large.png", landscape = FALSE, scaling_factor = 1.5) ## End(Not run)
Creates a discrete ggplot2 color scale from a predefined Core Surveillance color palette.
scale_color_cs(..., palette = "primary", direction = 1)scale_color_cs(..., palette = "primary", direction = 1)
... |
Further arguments passed to |
palette |
Name of the palette family. One of "primary", "warning" or "posneg". Default "primary". |
direction |
Direction of the palette. Use 1 for the normal order and -1 for the reversed order. Default 1. |
A ggplot2 discrete color scale.
Other csstyle_ggplot2:
scale_fill_cs(),
theme_cs()
library(ggplot2) # Default primary color scale ggplot(mtcars, aes(x = mpg, y = hp, color = factor(cyl))) + geom_point() + scale_color_cs() # Use warning palette ggplot(mtcars, aes(x = mpg, y = hp, color = factor(cyl))) + geom_point() + scale_color_cs(palette = "warning")library(ggplot2) # Default primary color scale ggplot(mtcars, aes(x = mpg, y = hp, color = factor(cyl))) + geom_point() + scale_color_cs() # Use warning palette ggplot(mtcars, aes(x = mpg, y = hp, color = factor(cyl))) + geom_point() + scale_color_cs(palette = "warning")
Creates a discrete ggplot2 fill scale from a predefined Core Surveillance color palette.
scale_fill_cs(..., palette = "primary", direction = 1)scale_fill_cs(..., palette = "primary", direction = 1)
... |
Further arguments passed to |
palette |
Name of the palette family. One of "primary", "warning" or "posneg". Default "primary". |
direction |
Direction of the palette. Use 1 for the normal order and -1 for the reversed order. Default 1. |
A ggplot2 discrete fill scale.
Other csstyle_ggplot2:
scale_color_cs(),
theme_cs()
library(ggplot2) # Default primary fill scale ggplot(mtcars, aes(x = factor(cyl), fill = factor(cyl))) + geom_bar() + scale_fill_cs() # Use posneg palette ggplot(mtcars, aes(x = factor(vs), fill = factor(vs))) + geom_bar() + scale_fill_cs(palette = "posneg")library(ggplot2) # Default primary fill scale ggplot(mtcars, aes(x = factor(cyl), fill = factor(cyl))) + geom_bar() + scale_fill_cs() # Use posneg palette ggplot(mtcars, aes(x = factor(vs), fill = factor(vs))) + geom_bar() + scale_fill_cs(palette = "posneg")
Sets the global defaults that control how location codes become
labels in format_location_code_as_factor and
format_location_code_as_character. Both functions use these
defaults when a call leaves label or label_if_not_unique
unset.
set_global( location_code_to_factor_label = "location_name", location_code_to_factor_label_if_not_unique = "location_name_description_nb" )set_global( location_code_to_factor_label = "location_name", location_code_to_factor_label_if_not_unique = "location_name_description_nb" )
location_code_to_factor_label |
Column name in the reference data to use
for location labels. Default |
location_code_to_factor_label_if_not_unique |
Column name in the
reference data to use when the chosen labels are not unique. Default
|
Called for the side effect of updating the global configuration. Returns the assigned value invisibly.
Other csstyle_utilities:
colors,
display_all_palettes(),
every_nth()
# Set global location formatting preferences set_global( location_code_to_factor_label = "custom_name", location_code_to_factor_label_if_not_unique = "custom_description" ) # Reset to defaults set_global()# Set global location formatting preferences set_global( location_code_to_factor_label = "custom_name", location_code_to_factor_label_if_not_unique = "custom_description" ) # Reset to defaults set_global()
A ggplot2 theme that follows the Core Surveillance visual guidelines. You can configure the grid lines and the formatting.
Returns a ggplot2 theme component that turns the x-axis tick labels vertical. Add it to a plot as you add any other theme element.
theme_cs( base_size = 16, base_family = "", base_line_size = base_size/22, base_rect_size = base_size/22, legend_position = "right", x_axis_vertical = FALSE, panel_on_top = TRUE, panel.grid.major.x = FALSE, panel.grid.minor.x = FALSE, panel.grid.major.y = TRUE, panel.grid.minor.y = TRUE ) set_x_axis_vertical()theme_cs( base_size = 16, base_family = "", base_line_size = base_size/22, base_rect_size = base_size/22, legend_position = "right", x_axis_vertical = FALSE, panel_on_top = TRUE, panel.grid.major.x = FALSE, panel.grid.minor.x = FALSE, panel.grid.major.y = TRUE, panel.grid.minor.y = TRUE ) set_x_axis_vertical()
base_size |
Base font size in points. Default 16. |
base_family |
Base font family. Default |
base_line_size |
Base line size, relative to |
base_rect_size |
Base rectangle size, relative to |
legend_position |
Position of the legend. One of "right", "left", "top", "bottom" or "none". Default "right". |
x_axis_vertical |
Logical. Set to |
panel_on_top |
Logical. Set to |
panel.grid.major.x |
Logical, or a ggplot2 element. Major vertical grid
lines. Default |
panel.grid.minor.x |
Logical, or a ggplot2 element. Minor vertical grid
lines. Default |
panel.grid.major.y |
Logical, or a ggplot2 element. Major horizontal
grid lines. Default |
panel.grid.minor.y |
Logical, or a ggplot2 element. Minor horizontal
grid lines. Default |
A complete ggplot2 theme object.
A ggplot2 theme component that you can add to a plot.
Other csstyle_ggplot2:
scale_color_cs(),
scale_fill_cs()
library(ggplot2) # Basic usage with default settings ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point() + theme_cs() # With custom settings ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point() + theme_cs(legend_position = "bottom", x_axis_vertical = TRUE) library(ggplot2) # Add vertical x-axis labels to any plot ggplot(mtcars, aes(x = rownames(mtcars), y = mpg)) + geom_col() + set_x_axis_vertical()library(ggplot2) # Basic usage with default settings ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point() + theme_cs() # With custom settings ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point() + theme_cs(legend_position = "bottom", x_axis_vertical = TRUE) library(ggplot2) # Add vertical x-axis labels to any plot ggplot(mtcars, aes(x = rownames(mtcars), y = mpg)) + geom_col() + set_x_axis_vertical()
Opens HTML content in the RStudio viewer pane.
view_html(x)view_html(x)
x |
Character string. The HTML content to show. |
Nothing (called for side effects).
vignette("csstyle") gives an overview of the package. It does
not demonstrate view_html().
# View simple HTML content html_content <- "<h1>Hello World</h1><p>This is a test.</p>" ## Not run: view_html(html_content) ## End(Not run)# View simple HTML content html_content <- "<h1>Hello World</h1><p>This is a test.</p>" ## Not run: view_html(html_content) ## End(Not run)