add_secondary_y_conversion: Add a Secondary Y-Axis to a ggplot, Converting from the...

View source: R/add_secondary_y_conversion.R

add_secondary_y_conversionR Documentation

Add a Secondary Y-Axis to a ggplot, Converting from the Primary Axis Units

Description

add_secondary_y_conversion() adds a secondary y-axis to a ggplot. While it is intended to be used to add a metric equivelent axis, it will work for any conversion that is handled by udunits2::ud.convert().

Usage

add_secondary_y_conversion(
  gg,
  from_unit,
  to_unit,
  sec_name = to_unit,
  digits = "get_decimals"
)

Arguments

gg

A ggplot

from_unit

The primary axis units

to_unit

The secondary axis units

sec_name

The name of the secondary axis

digits

The number of digits to the right of the decimal place to show on the secondary axis. See details.

Details

digits

Either a number or the string "get_decimals". If a number, then that is the number of digits to the right of the decimal place that will be shown. If "get_decimals", then the secondary axis will show the same number of digits to the right of the decimal place as the primary axis does.

units

For from_unit and to_unit, the following are common units used in CRSS that will work with udunits2::ud.convert(): "acre_feet", "m^3", "acre_feet/month", "acre_feet/year", "m^3/s", "km^3/s", "1e6m^3/s".

Examples

library(ggplot2)
df <- data.frame(year = 2020:2029, elev = rnorm(10, 3580, 10))
gg <- ggplot(df, aes(year, elev)) + 
  geom_line()
add_secondary_y_conversion(gg, "feet", "meters")


BoulderCodeHub/CRSSIO documentation built on July 2, 2023, 5:15 p.m.