View source: R/add_secondary_y_conversion.R
add_secondary_y_conversion | R Documentation |
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()
.
add_secondary_y_conversion(
gg,
from_unit,
to_unit,
sec_name = to_unit,
digits = "get_decimals"
)
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. |
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.
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".
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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.