secondary y-axis

This commit is contained in:
pvictor 2019-07-15 12:04:45 +02:00
parent b2e1a78b7b
commit 944e63e256
4 changed files with 86 additions and 1 deletions

View File

@ -31,6 +31,7 @@ export(ax_title)
export(ax_tooltip)
export(ax_xaxis)
export(ax_yaxis)
export(ax_yaxis2)
export(bar_opts)
export(events_opts)
export(heatmap_opts)

View File

@ -1156,10 +1156,54 @@ ax_yaxis <- function(ax,
crosshairs = NULL,
...) {
params <- c(as.list(environment()), list(...))[-1]
.ax_opt2(ax, "yaxis", l = dropNulls(params))
yaxis <- .get_ax_opt(ax, "yaxis")
if (inherits(yaxis, "yaxis2")) {
yaxis[[1]] <- dropNulls(params)
} else {
yaxis <- dropNulls(params)
class(yaxis) <- c(class(yaxis), "yaxis1")
}
.ax_opt2(ax, "yaxis", l = yaxis)
}
#' Secondary Y-axis options
#'
#' @param ax A \code{apexcharts} \code{htmlwidget} object.
#' @param ... See arguments from \code{\link{ax_yaxis}}.
#'
#' @return A \code{apexcharts} \code{htmlwidget} object.
#' @export
#'
#' @examples
#'
#' library(dplyr)
#' data("economics_long", package = "ggplot2")
#'
#' eco <- economics_long %>%
#' filter(variable %in% c("pce", "pop")) %>%
#' filter(date >= "2000-01-01")
#'
#' apex(eco, aes(x = date, y = value, color = variable), type = "line") %>%
#' ax_yaxis(title = list(text = "Pce")) %>%
#' ax_yaxis2(opposite = TRUE, title = list(text = "Pop"))
#'
ax_yaxis2 <- function(ax, ...) {
params <- dropNulls(list(...))
yaxis <- .get_ax_opt(ax, "yaxis")
if (inherits(yaxis, "yaxis2")) {
yaxis[[2]] <- params
} else {
yaxis <- list(yaxis, params)
class(yaxis) <- c(setdiff(class(yaxis), "yaxis1"), "yaxis2")
ax$x$ax_opts$yaxis <- NULL
}
.ax_opt2(ax, "yaxis", l = yaxis)
}
#' Theme for charts
#'
#' @param ax A \code{apexcharts} \code{htmlwidget} object.

View File

@ -58,4 +58,11 @@ formatNoSci <- function(x) {
return(ax)
}
# Get parameters from an \code{apexcharts} \code{htmlwidget} object.
.get_ax_opt <- function(ax, name) {
ax$x$ax_opts[[name]]
}

33
man/ax_yaxis2.Rd Normal file
View File

@ -0,0 +1,33 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/apex-utils.R
\name{ax_yaxis2}
\alias{ax_yaxis2}
\title{Secondary Y-axis options}
\usage{
ax_yaxis2(ax, ...)
}
\arguments{
\item{ax}{A \code{apexcharts} \code{htmlwidget} object.}
\item{...}{See arguments from \code{\link{ax_yaxis}}}
}
\value{
A \code{apexcharts} \code{htmlwidget} object.
}
\description{
Secondary Y-axis options
}
\examples{
library(dplyr)
data("economics_long", package = "ggplot2")
eco <- economics_long \%>\%
filter(variable \%in\% c("pce", "pop")) \%>\%
filter(date >= "2000-01-01")
apex(eco, aes(x = date, y = value, color = variable), type = "line") \%>\%
ax_yaxis(title = list(text = "Pce")) \%>\%
ax_yaxis2(opposite = TRUE, title = list(text = "Pop"))
}