Package 'csstyle'

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

Help Index


Create a github_code object from a GitHub URL

Description

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.

Usage

as_github_code(url)

Arguments

url

Character string. A GitHub URL to a file. Use either a "blob" URL, for example https://github.com/owner/repo/blob/main/file.R, or the matching raw URL.

Value

An object of class github_code: a character vector of the file's lines with a pretty_url attribute.

See Also

print.github_code, whose examples run without a network connection. vignette("csstyle") demonstrates neither function.

Examples

## 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)

Color definitions for Core Surveillance styling

Description

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.

Usage

colors

Format

A list with the following elements:

named_colors

Named character vector of 12 colors, "H1" to "H12". "H1" to "H7" are hex codes. "H8" to "H12" are R color names.

base

The base color, element "H1" of named_colors.

palettes

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.

palette_names

Character vector of the palette family names ("primary", "posneg", "warning").

Value

A list. See the Format section for the elements.

See Also

Other csstyle_utilities: display_all_palettes(), every_nth(), set_global()

Examples

# Access named colors
colors$named_colors["H1"]

# View primary palette
colors$palettes$primary_3

# See all available palettes
names(colors$palettes)

Display all available color palettes

Description

Creates a plot that shows all available Core Surveillance color palettes with their color codes and names.

Usage

display_all_palettes()

Value

A ggplot2 plot that shows all the color palettes.

See Also

Other csstyle_utilities: colors, every_nth(), set_global()

Examples

# Display all available color palettes
display_all_palettes()

Returns every nth discrete value

Description

Creates a function that returns every nth element of a vector. Use it for axis breaks in ggplot2.

Usage

every_nth(n)

Arguments

n

Integer. Which nth value to return: n = 2 returns every 2nd value.

Value

A function that returns every nth discrete value.

See Also

Other csstyle_utilities: colors, display_all_palettes(), set_global()

Examples

every_nth(4)(c(1:10))
## Not run: 
scale_x_discrete(NULL, breaks = every_nth(n = 2))

## End(Not run)

Format cstidy age codes as factor with Norwegian text

Description

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.

Usage

format_cstidy_age_as_factor(x)

format_cstidy_age_as_character(x)

Arguments

x

Character vector of age codes or age group codes.

Value

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".

See Also

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.

Examples

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")

Format date and datetime using journal conventions (ISO 8601)

Description

Formats dates and datetimes with the ISO 8601 conventions that journals use.

Usage

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"
)

Arguments

x

Date or datetime object to format.

format

Character string that sets the output format.

Value

Character vector of journal-formatted dates and datetimes.

See Also

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().

Examples

# 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 date and datetime using Norwegian conventions

Description

Formats dates and datetimes with Norwegian conventions, or with a filename-safe format.

Usage

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")

Arguments

x

Date or datetime object to format.

format

Character string that sets the output format.

Value

Character vector of formatted dates and datetimes.

See Also

format_date_as_journal gives the ISO 8601 equivalents. The "Norwegian date format" section of vignette("csstyle") demonstrates all three functions on this page.

Examples

# 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"))

Convert Norwegian location codes to factor with readable labels

Description

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.

Usage

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
)

Arguments

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 csdata::nor_locations_names().

direction

Integer. Use 1 for the normal order and -1 for the reversed order. Default 1.

Value

Factor vector with Norwegian location names as labels.

Character vector of Norwegian location names.

See Also

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.

Examples

# 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)

Format numbers on inverse log-10 scale with journal conventions (1 decimal place)

Description

Applies 10^x, then formats with journal conventions and 1 decimal place.

Applies 10^x, then formats with journal conventions and 2 decimal places.

Usage

format_num_as_journal_invlog10_1(x)

format_num_as_journal_invlog10_2(x)

Arguments

x

Numeric vector of values on the log-10 scale.

Value

Character vector of journal-formatted numbers.

Character vector of journal-formatted numbers.

See Also

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()

Examples

# 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))

Format numbers on inverse log-2 scale with journal conventions (1 decimal place)

Description

Applies 2^x, then formats with journal conventions and 1 decimal place.

Applies 2^x, then formats with journal conventions and 2 decimal places.

Usage

format_num_as_journal_invlog2_1(x)

format_num_as_journal_invlog2_2(x)

Arguments

x

Numeric vector of values on the log-2 scale.

Value

Character vector of journal-formatted numbers.

Character vector of journal-formatted numbers.

See Also

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()

Examples

# 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))

Format numbers on inverse natural log scale with journal conventions (1 decimal place)

Description

Applies exp(x), then formats with journal conventions and 1 decimal place.

Applies exp(x), then formats with journal conventions and 2 decimal places.

Usage

format_num_as_journal_invloge_1(x)

format_num_as_journal_invloge_2(x)

Arguments

x

Numeric vector of values on the natural log scale.

Value

Character vector of journal-formatted numbers.

Character vector of journal-formatted numbers.

See Also

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()

Examples

# 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))

Format numbers with journal conventions (0 decimal places)

Description

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.

Usage

format_num_as_journal_num_0(x)

format_num_as_journal_num_1(x)

format_num_as_journal_num_2(x)

Arguments

x

Numeric vector to format.

Value

Character vector of journal-formatted numbers.

Character vector of journal-formatted numbers.

Character vector of journal-formatted numbers.

See Also

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()

Examples

# 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))

Format numbers per 100k with journal conventions (0 decimal places)

Description

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.

Usage

format_num_as_journal_per100k_0(x)

format_num_as_journal_per100k_1(x)

format_num_as_journal_per100k_2(x)

Arguments

x

Numeric vector to format.

Value

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.

See Also

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()

Examples

# 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 numbers as percentages with journal conventions (0 decimal places)

Description

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.

Usage

format_num_as_journal_perc_0(x)

format_num_as_journal_perc_1(x)

format_num_as_journal_perc_2(x)

Arguments

x

Numeric vector to format.

Value

Character vector of journal-formatted numbers with a "

Character vector of journal-formatted numbers with a "

Character vector of journal-formatted numbers with a "

See Also

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()

Examples

# 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 numbers on inverse log-10 scale with Norwegian conventions (1 decimal place)

Description

Applies 10^x, then formats with Norwegian conventions and 1 decimal place.

Applies 10^x, then formats with Norwegian conventions and 2 decimal places.

Usage

format_num_as_nor_invlog10_1(x)

format_num_as_nor_invlog10_2(x)

Arguments

x

Numeric vector of values on the log-10 scale.

Value

Character vector of Norwegian-formatted numbers.

Character vector of Norwegian-formatted numbers.

See Also

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()

Examples

# 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))

Format numbers on inverse log-2 scale with Norwegian conventions (1 decimal place)

Description

Applies 2^x, then formats with Norwegian conventions and 1 decimal place.

Applies 2^x, then formats with Norwegian conventions and 2 decimal places.

Usage

format_num_as_nor_invlog2_1(x)

format_num_as_nor_invlog2_2(x)

Arguments

x

Numeric vector of values on the log-2 scale.

Value

Character vector of Norwegian-formatted numbers.

Character vector of Norwegian-formatted numbers.

See Also

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()

Examples

# 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))

Format numbers on inverse natural log scale with Norwegian conventions (1 decimal place)

Description

Applies exp(x), then formats with Norwegian conventions and 1 decimal place.

Applies exp(x), then formats with Norwegian conventions and 2 decimal places.

Usage

format_num_as_nor_invloge_1(x)

format_num_as_nor_invloge_2(x)

Arguments

x

Numeric vector of values on the natural log scale.

Value

Character vector of Norwegian-formatted numbers.

Character vector of Norwegian-formatted numbers.

See Also

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()

Examples

# 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))

Format numbers with Norwegian conventions (0 decimal places)

Description

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.

Usage

format_num_as_nor_num_0(x)

format_num_as_nor_num_1(x)

format_num_as_nor_num_2(x)

Arguments

x

Numeric vector to format.

Value

Character vector of Norwegian-formatted numbers.

Character vector of Norwegian-formatted numbers.

Character vector of Norwegian-formatted numbers.

See Also

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()

Examples

# 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))

Format numbers per 100k with Norwegian conventions (0 decimal places)

Description

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.

Usage

format_num_as_nor_per100k_0(x)

format_num_as_nor_per100k_1(x)

format_num_as_nor_per100k_2(x)

Arguments

x

Numeric vector to format.

Value

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.

See Also

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()

Examples

# 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 numbers as percentages with Norwegian conventions (0 decimal places)

Description

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.

Usage

format_num_as_nor_perc_0(x)

format_num_as_nor_perc_1(x)

format_num_as_nor_perc_2(x)

Arguments

x

Numeric vector to format.

Value

Character vector of Norwegian-formatted numbers with a "

Character vector of Norwegian-formatted numbers with a "

Character vector of Norwegian-formatted numbers with a "

See Also

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()

Examples

# 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))

Pretty breaks

Description

Creates a function that generates pretty axis breaks with Norwegian number formatting.

Usage

pretty_breaks(n = 5, formatting_fn = csstyle::format_num_as_nor_num_0, ...)

Arguments

n

Number of desired breaks. Default 5.

formatting_fn

Function applied to the break labels. Default format_num_as_nor_num_0().

...

Further arguments passed to pretty().

Value

A function that you can use for axis breaks.

See Also

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().

Examples

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))

Print a github_code object

Description

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.

Usage

## S3 method for class 'github_code'
print(x, ...)

Arguments

x

A github_code object, as created by as_github_code.

...

Further arguments that control the output. lines is an integer vector of the line numbers to print. include_url is logical. Set it to TRUE to also print the GitHub link. include_url_as_link is logical. Set it to TRUE to print the GitHub link as HTML.

Value

The input x, invisibly.

See Also

as_github_code builds such an object from a real GitHub URL. vignette("csstyle") demonstrates neither function.

Examples

# 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)

Save a ggplot2 plot at A4 size

Description

Saves a ggplot2 plot with A4 paper dimensions.

Usage

save_a4(q, filename, landscape = T, scaling_factor = 1)

Arguments

q

ggplot2 plot object to save.

filename

Character string. The output filename, including the extension.

landscape

Logical. Set to TRUE for landscape orientation. Default TRUE.

scaling_factor

Numeric. Scales the A4 dimensions. Default 1.

Details

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.

Value

Nothing (called for side effects).

See Also

theme_cs is the Core Surveillance plot theme. vignette("csstyle") does not demonstrate save_a4().

Examples

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)

ggplot2 color scale using Core Surveillance color palettes

Description

Creates a discrete ggplot2 color scale from a predefined Core Surveillance color palette.

Usage

scale_color_cs(..., palette = "primary", direction = 1)

Arguments

...

Further arguments passed to ggplot2::discrete_scale().

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.

Value

A ggplot2 discrete color scale.

See Also

Other csstyle_ggplot2: scale_fill_cs(), theme_cs()

Examples

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")

ggplot2 fill scale using Core Surveillance color palettes

Description

Creates a discrete ggplot2 fill scale from a predefined Core Surveillance color palette.

Usage

scale_fill_cs(..., palette = "primary", direction = 1)

Arguments

...

Further arguments passed to ggplot2::discrete_scale().

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.

Value

A ggplot2 discrete fill scale.

See Also

Other csstyle_ggplot2: scale_color_cs(), theme_cs()

Examples

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")

Set global configuration for location code formatting

Description

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.

Usage

set_global(
  location_code_to_factor_label = "location_name",
  location_code_to_factor_label_if_not_unique = "location_name_description_nb"
)

Arguments

location_code_to_factor_label

Column name in the reference data to use for location labels. Default "location_name".

location_code_to_factor_label_if_not_unique

Column name in the reference data to use when the chosen labels are not unique. Default "location_name_description_nb".

Value

Called for the side effect of updating the global configuration. Returns the assigned value invisibly.

See Also

Other csstyle_utilities: colors, display_all_palettes(), every_nth()

Examples

# 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()

Core Surveillance ggplot2 theme for consistent styling

Description

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.

Usage

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()

Arguments

base_size

Base font size in points. Default 16.

base_family

Base font family. Default "".

base_line_size

Base line size, relative to base_size. Default base_size / 22.

base_rect_size

Base rectangle size, relative to base_size. Default base_size / 22.

legend_position

Position of the legend. One of "right", "left", "top", "bottom" or "none". Default "right".

x_axis_vertical

Logical. Set to TRUE to turn the x-axis labels vertical. Default FALSE.

panel_on_top

Logical. Set to TRUE to draw the panel on top of the data. Default TRUE.

panel.grid.major.x

Logical, or a ggplot2 element. Major vertical grid lines. Default FALSE.

panel.grid.minor.x

Logical, or a ggplot2 element. Minor vertical grid lines. Default FALSE.

panel.grid.major.y

Logical, or a ggplot2 element. Major horizontal grid lines. Default TRUE.

panel.grid.minor.y

Logical, or a ggplot2 element. Minor horizontal grid lines. Default TRUE.

Value

A complete ggplot2 theme object.

A ggplot2 theme component that you can add to a plot.

See Also

Other csstyle_ggplot2: scale_color_cs(), scale_fill_cs()

Examples

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()

View HTML in the RStudio viewer

Description

Opens HTML content in the RStudio viewer pane.

Usage

view_html(x)

Arguments

x

Character string. The HTML content to show.

Value

Nothing (called for side effects).

See Also

vignette("csstyle") gives an overview of the package. It does not demonstrate view_html().

Examples

# 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)