added support for boxplot in apex()

This commit is contained in:
pvictor 2023-02-22 10:45:29 +01:00
parent b69b7b7b4d
commit 896998074e
13 changed files with 160 additions and 33 deletions

View File

@ -52,6 +52,7 @@ export(ax_xaxis)
export(ax_yaxis) export(ax_yaxis)
export(ax_yaxis2) export(ax_yaxis2)
export(bar_opts) export(bar_opts)
export(boxplot_opts)
export(bubble_opts) export(bubble_opts)
export(config_update) export(config_update)
export(events_opts) export(events_opts)

View File

@ -115,7 +115,7 @@ events_opts <- function(click = NULL,
#' @title Bar options #' @title Bar options
#' #'
#' @description Use these options in \code{\link{ax_plotOptions}}. #' @description Use these options in [ax_plotOptions()].
#' #'
#' @param horizontal Logical. This option will turn a column chart into a horizontal bar chart. #' @param horizontal Logical. This option will turn a column chart into a horizontal bar chart.
#' @param endingShape Available Options: \code{"flat"} or \code{"rounded"}. #' @param endingShape Available Options: \code{"flat"} or \code{"rounded"}.
@ -128,7 +128,7 @@ events_opts <- function(click = NULL,
#' #'
#' @note See \url{https://apexcharts.com/docs/options/plotoptions/bar/}. #' @note See \url{https://apexcharts.com/docs/options/plotoptions/bar/}.
#' #'
#' @return A \code{list} of options that can be used in \code{\link{ax_plotOptions}}. #' @return A \code{list} of options that can be used in [ax_plotOptions()].
#' #'
#' @export #' @export
#' #'
@ -170,7 +170,7 @@ bar_opts <- function(horizontal = NULL,
#' @title Heatmap options #' @title Heatmap options
#' #'
#' @description Use these options in \code{\link{ax_plotOptions}}. #' @description Use these options in [ax_plotOptions()].
#' #'
#' @param radius Numeric. Radius of the rectangle inside heatmap. #' @param radius Numeric. Radius of the rectangle inside heatmap.
#' @param enableShades Logical. Enable different shades of color depending on the value #' @param enableShades Logical. Enable different shades of color depending on the value
@ -180,7 +180,7 @@ bar_opts <- function(horizontal = NULL,
#' #'
#' @note See \url{https://apexcharts.com/docs/options/plotoptions/heatmap/}. #' @note See \url{https://apexcharts.com/docs/options/plotoptions/heatmap/}.
#' #'
#' @return A \code{list} of options that can be used in \code{\link{ax_plotOptions}}. #' @return A \code{list} of options that can be used in [ax_plotOptions()].
#' #'
#' @export #' @export
#' #'
@ -228,7 +228,7 @@ heatmap_opts <- function(radius = NULL,
#' @title Radial bar options #' @title Radial bar options
#' #'
#' @description Use these options in \code{\link{ax_plotOptions}}. #' @description Use these options in [ax_plotOptions()].
#' #'
#' @param size Numeric. Manual size of the radialBars instead of calculating automatically from default height / width. #' @param size Numeric. Manual size of the radialBars instead of calculating automatically from default height / width.
#' @param inverseOrder Logical. Whether to make the first value of series innermost or outermost. #' @param inverseOrder Logical. Whether to make the first value of series innermost or outermost.
@ -243,7 +243,7 @@ heatmap_opts <- function(radius = NULL,
#' #'
#' @note See \url{https://apexcharts.com/docs/options/plotoptions/radialbar/}. #' @note See \url{https://apexcharts.com/docs/options/plotoptions/radialbar/}.
#' #'
#' @return A \code{list} of options that can be used in \code{\link{ax_plotOptions}}. #' @return A \code{list} of options that can be used in [ax_plotOptions()].
#' #'
#' @export #' @export
#' #'
@ -301,7 +301,7 @@ radialBar_opts <- function(size = NULL,
#' @title Pie options #' @title Pie options
#' #'
#' @description Use these options in \code{\link{ax_plotOptions}}. #' @description Use these options in [ax_plotOptions()].
#' #'
#' @param size Numeric. Custom size of the pie which will override the default size calculations. #' @param size Numeric. Custom size of the pie which will override the default size calculations.
#' @param donut List with two fields \code{size} (Donut / ring size in percentage relative to the total pie area.) #' @param donut List with two fields \code{size} (Donut / ring size in percentage relative to the total pie area.)
@ -314,7 +314,7 @@ radialBar_opts <- function(size = NULL,
#' #'
#' @note See \url{https://apexcharts.com/docs/options/plotoptions/pie/}. #' @note See \url{https://apexcharts.com/docs/options/plotoptions/pie/}.
#' #'
#' @return A \code{list} of options that can be used in \code{\link{ax_plotOptions}}. #' @return A \code{list} of options that can be used in [ax_plotOptions()].
#' #'
#' @export #' @export
#' #'
@ -351,7 +351,7 @@ pie_opts <- function(size = NULL,
#' @title Bubble options #' @title Bubble options
#' #'
#' @description Use these options in \code{\link{ax_plotOptions}}. #' @description Use these options in [ax_plotOptions()].
#' #'
#' @param minBubbleRadius Minimum radius size of a bubble. #' @param minBubbleRadius Minimum radius size of a bubble.
#' If a bubble value is too small to be displayed, this size will be used. #' If a bubble value is too small to be displayed, this size will be used.
@ -361,7 +361,7 @@ pie_opts <- function(size = NULL,
#' #'
#' @note See \url{https://apexcharts.com/docs/options/plotoptions/bubble/}. #' @note See \url{https://apexcharts.com/docs/options/plotoptions/bubble/}.
#' #'
#' @return A \code{list} of options that can be used in \code{\link{ax_plotOptions}}. #' @return A \code{list} of options that can be used in [ax_plotOptions()].
#' @export #' @export
#' #'
#' @examples #' @examples
@ -386,3 +386,36 @@ bubble_opts <- function(minBubbleRadius, maxBubbleRadius, ...) {
) )
} }
#' @title Boxplot options
#'
#' @description Use these options in [ax_plotOptions()].
#'
#' @param color.upper Color for the upper quartile (Q3 to median) of the box plot.
#' @param color.lower Color for the lower quartile (median to Q1) of the box plot.
#' @param ... Additional parameters.
#'
#' @note See \url{https://apexcharts.com/docs/options/plotoptions/boxplot/}.
#'
#' @return A \code{list} of options that can be used in [ax_plotOptions()].
#' @export
#'
#' @examples
#' data("mpg", package = "ggplot2")
#' apex(mpg, aes(class, hwy), "boxplot") %>%
#' ax_plotOptions(
#' boxPlot = boxplot_opts(color.upper = "#848484", color.lower = "#848484" )
#' )
boxplot_opts <- function(color.upper, color.lower, ...) {
dropNulls(
list(
colors = dropNulls(list(
upper = color.upper,
lower = color.lower
)),
...
)
)
}

View File

@ -185,11 +185,12 @@ ax_chart <- function(ax,
#' Specific options for chart #' Specific options for chart
#' #'
#' @template ax-default #' @template ax-default
#' @param bar See \code{\link{bar_opts}}. #' @param bar See [bar_opts()].
#' @param heatmap See \code{\link{heatmap_opts}}. #' @param heatmap See [heatmap_opts()].
#' @param radialBar See \code{\link{radialBar_opts}}. #' @param radialBar See [radialBar_opts()].
#' @param pie See \code{\link{pie_opts}}. #' @param pie See [pie_opts()].
#' @param bubble See \code{\link{bubble_opts}}. #' @param bubble See [bubble_opts()].
#' @param boxPlot See [boxplot_opts()].
#' @param ... Additional parameters. #' @param ... Additional parameters.
#' #'
#' #'
@ -234,6 +235,7 @@ ax_plotOptions <- function(ax,
radialBar = NULL, radialBar = NULL,
pie = NULL, pie = NULL,
bubble = NULL, bubble = NULL,
boxPlot = NULL,
...) { ...) {
params <- c(as.list(environment()), list(...))[-1] params <- c(as.list(environment()), list(...))[-1]
.ax_opt2(ax, "plotOptions", l = dropNulls(params)) .ax_opt2(ax, "plotOptions", l = dropNulls(params))

View File

@ -54,7 +54,8 @@ apex <- function(data, mapping,
"heatmap", "heatmap",
"treemap", "treemap",
"timeline", "timeline",
"candlestick" "candlestick",
"boxplot"
) )
) )
data <- as.data.frame(data) data <- as.data.frame(data)
@ -67,7 +68,7 @@ apex <- function(data, mapping,
type <- "bubble" type <- "bubble"
} }
mapdata <- lapply(mapping, rlang::eval_tidy, data = data) mapdata <- lapply(mapping, rlang::eval_tidy, data = data)
if (is.null(mapdata$y) & !type %in% c("candlestick", "timeline", "heatmap", "rangeArea")) { if (is.null(mapdata$y) & !type %in% c("candlestick", "boxplot", "timeline", "heatmap", "rangeArea")) {
mapdata <- compute_count(mapdata) mapdata <- compute_count(mapdata)
} }
if (type %in% c("pie", "donut", "radialBar", "polarArea")) { if (type %in% c("pie", "donut", "radialBar", "polarArea")) {
@ -118,7 +119,9 @@ apex <- function(data, mapping,
# Construct series # Construct series
#' @importFrom rlang %||% #' @importFrom rlang %||%
make_series <- function(mapdata, mapping, type = NULL, serie_name = NULL, force_datetime_names = FALSE) { make_series <- function(mapdata, mapping, type = NULL, serie_name = NULL, force_datetime_names = FALSE) {
if (identical(type, "candlestick")) { if (identical(type, "boxplot")) {
series <- parse_boxplot_data(mapdata, serie_name = serie_name)
} else if (identical(type, "candlestick")) {
if (!all(c("x", "open", "high", "low", "close") %in% names(mapping))) if (!all(c("x", "open", "high", "low", "close") %in% names(mapping)))
stop("For candlestick charts 'x', 'open', 'high', 'low', and 'close' aesthetics must be provided.", call. = FALSE) stop("For candlestick charts 'x', 'open', 'high', 'low', and 'close' aesthetics must be provided.", call. = FALSE)
if (!is.null(mapdata$group)) if (!is.null(mapdata$group))
@ -250,6 +253,8 @@ correct_type <- function(type) {
"area" "area"
} else if (identical(type, "timeline")) { } else if (identical(type, "timeline")) {
"rangeBar" "rangeBar"
} else if (identical(type, "boxplot")) {
"boxPlot"
} else { } else {
type type
} }
@ -308,6 +313,9 @@ choose_config <- function(type, mapdata) {
datetime <- is_x_datetime(mapdata) datetime <- is_x_datetime(mapdata)
range_x <- range_num(mapdata$x) range_x <- range_num(mapdata$x)
range_y <- range_num(mapdata$y) range_y <- range_num(mapdata$y)
if (identical(type, "boxplot")) {
box_horiz <- !is.numeric(mapdata$y) & is.numeric(mapdata$x)
}
switch( switch(
type, type,
"bar" = config_bar(horizontal = TRUE), "bar" = config_bar(horizontal = TRUE),
@ -323,6 +331,7 @@ choose_config <- function(type, mapdata) {
"bubble" = config_scatter(range_x = range_x, range_y = range_y, datetime = datetime), "bubble" = config_scatter(range_x = range_x, range_y = range_y, datetime = datetime),
"timeline" = config_timeline(), "timeline" = config_timeline(),
"candlestick" = config_candlestick(), "candlestick" = config_candlestick(),
"boxplot" = config_boxplot(horizontal = box_horiz),
list() list()
) )
} }
@ -436,3 +445,13 @@ config_candlestick <- function() {
) )
} }
config_boxplot <- function(horizontal = FALSE) {
list(
plotOptions = list(
bar = list(
horizontal = horizontal
)
)
)
}

View File

@ -127,3 +127,29 @@ parse_candlestick_data <- function(.list) {
)) ))
} }
parse_boxplot_data <- function(.list, serie_name = NULL) {
if (!is.numeric(.list$y) & is.numeric(.list$x)) {
.list[c("x", "y")] <- .list[c("y", "x")]
}
boxed <- boxplot(y ~ x, data = .list, plot = FALSE)
list(dropNulls(list(
serie_name = serie_name,
type = "boxPlot",
data = lapply(
X = seq_along(boxed$names),
FUN = function(i) {
list(
x = boxed$names[i],
y = c(
boxed$stats[1, i],
boxed$stats[2, i],
boxed$stats[3, i],
boxed$stats[4, i],
boxed$stats[5, i]
)
)
}
)
)))
}

View File

@ -11,21 +11,24 @@ ax_plotOptions(
radialBar = NULL, radialBar = NULL,
pie = NULL, pie = NULL,
bubble = NULL, bubble = NULL,
boxPlot = NULL,
... ...
) )
} }
\arguments{ \arguments{
\item{ax}{An \code{\link[=apexchart]{apexchart()}} \code{htmlwidget} object.} \item{ax}{An \code{\link[=apexchart]{apexchart()}} \code{htmlwidget} object.}
\item{bar}{See \code{\link{bar_opts}}.} \item{bar}{See \code{\link[=bar_opts]{bar_opts()}}.}
\item{heatmap}{See \code{\link{heatmap_opts}}.} \item{heatmap}{See \code{\link[=heatmap_opts]{heatmap_opts()}}.}
\item{radialBar}{See \code{\link{radialBar_opts}}.} \item{radialBar}{See \code{\link[=radialBar_opts]{radialBar_opts()}}.}
\item{pie}{See \code{\link{pie_opts}}.} \item{pie}{See \code{\link[=pie_opts]{pie_opts()}}.}
\item{bubble}{See \code{\link{bubble_opts}}.} \item{bubble}{See \code{\link[=bubble_opts]{bubble_opts()}}.}
\item{boxPlot}{See \code{\link[=boxplot_opts]{boxplot_opts()}}.}
\item{...}{Additional parameters.} \item{...}{Additional parameters.}
} }

View File

@ -33,10 +33,10 @@ bar_opts(
\item{...}{Additional parameters.} \item{...}{Additional parameters.}
} }
\value{ \value{
A \code{list} of options that can be used in \code{\link{ax_plotOptions}}. A \code{list} of options that can be used in \code{\link[=ax_plotOptions]{ax_plotOptions()}}.
} }
\description{ \description{
Use these options in \code{\link{ax_plotOptions}}. Use these options in \code{\link[=ax_plotOptions]{ax_plotOptions()}}.
} }
\note{ \note{
See \url{https://apexcharts.com/docs/options/plotoptions/bar/}. See \url{https://apexcharts.com/docs/options/plotoptions/bar/}.

31
man/boxplot_opts.Rd Normal file
View File

@ -0,0 +1,31 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/apex-options.R
\name{boxplot_opts}
\alias{boxplot_opts}
\title{Boxplot options}
\usage{
boxplot_opts(color.upper, color.lower, ...)
}
\arguments{
\item{color.upper}{Color for the upper quartile (Q3 to median) of the box plot.}
\item{color.lower}{Color for the lower quartile (median to Q1) of the box plot.}
\item{...}{Additional parameters.}
}
\value{
A \code{list} of options that can be used in \code{\link[=ax_plotOptions]{ax_plotOptions()}}.
}
\description{
Use these options in \code{\link[=ax_plotOptions]{ax_plotOptions()}}.
}
\note{
See \url{https://apexcharts.com/docs/options/plotoptions/boxplot/}.
}
\examples{
data("mpg", package = "ggplot2")
apex(mpg, aes(class, hwy), "boxplot") \%>\%
ax_plotOptions(
boxPlot = boxplot_opts(color.upper = "#848484", color.lower = "#848484" )
)
}

View File

@ -16,10 +16,10 @@ If a bubble value is too large to cover the chart, this size will be used.}
\item{...}{Additional parameters.} \item{...}{Additional parameters.}
} }
\value{ \value{
A \code{list} of options that can be used in \code{\link{ax_plotOptions}}. A \code{list} of options that can be used in \code{\link[=ax_plotOptions]{ax_plotOptions()}}.
} }
\description{ \description{
Use these options in \code{\link{ax_plotOptions}}. Use these options in \code{\link[=ax_plotOptions]{ax_plotOptions()}}.
} }
\note{ \note{
See \url{https://apexcharts.com/docs/options/plotoptions/bubble/}. See \url{https://apexcharts.com/docs/options/plotoptions/bubble/}.

View File

@ -24,10 +24,10 @@ heatmap_opts(
\item{...}{Additional parameters.} \item{...}{Additional parameters.}
} }
\value{ \value{
A \code{list} of options that can be used in \code{\link{ax_plotOptions}}. A \code{list} of options that can be used in \code{\link[=ax_plotOptions]{ax_plotOptions()}}.
} }
\description{ \description{
Use these options in \code{\link{ax_plotOptions}}. Use these options in \code{\link[=ax_plotOptions]{ax_plotOptions()}}.
} }
\note{ \note{
See \url{https://apexcharts.com/docs/options/plotoptions/heatmap/}. See \url{https://apexcharts.com/docs/options/plotoptions/heatmap/}.

View File

@ -31,10 +31,10 @@ and \code{background} (The background color of the pie).}
\item{...}{Additional parameters.} \item{...}{Additional parameters.}
} }
\value{ \value{
A \code{list} of options that can be used in \code{\link{ax_plotOptions}}. A \code{list} of options that can be used in \code{\link[=ax_plotOptions]{ax_plotOptions()}}.
} }
\description{ \description{
Use these options in \code{\link{ax_plotOptions}}. Use these options in \code{\link[=ax_plotOptions]{ax_plotOptions()}}.
} }
\note{ \note{
See \url{https://apexcharts.com/docs/options/plotoptions/pie/}. See \url{https://apexcharts.com/docs/options/plotoptions/pie/}.

View File

@ -39,10 +39,10 @@ radialBar_opts(
\item{...}{Additional parameters.} \item{...}{Additional parameters.}
} }
\value{ \value{
A \code{list} of options that can be used in \code{\link{ax_plotOptions}}. A \code{list} of options that can be used in \code{\link[=ax_plotOptions]{ax_plotOptions()}}.
} }
\description{ \description{
Use these options in \code{\link{ax_plotOptions}}. Use these options in \code{\link[=ax_plotOptions]{ax_plotOptions()}}.
} }
\note{ \note{
See \url{https://apexcharts.com/docs/options/plotoptions/radialbar/}. See \url{https://apexcharts.com/docs/options/plotoptions/radialbar/}.

View File

@ -244,4 +244,16 @@ apex(
## Boxplot
Create boxplot (without outliers for now) with:
```{r boxplot}
data("mpg", package = "ggplot2")
apex(mpg, aes(hwy, class), "boxplot") %>%
ax_plotOptions(
boxPlot = boxplot_opts(color.upper = "#8BB0A6", color.lower = "#8BB0A6" )
) %>%
ax_stroke(colors = list("#2A5769"))
```