added add_hline() and add_vline()
This commit is contained in:
parent
721cce9cff
commit
92b2217f56
|
@ -3,8 +3,10 @@
|
|||
export("%>%")
|
||||
export(JS)
|
||||
export(add_event)
|
||||
export(add_hline)
|
||||
export(add_shade)
|
||||
export(add_shade_weekend)
|
||||
export(add_vline)
|
||||
export(aes)
|
||||
export(apex)
|
||||
export(apexchart)
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
|
||||
add_annotation <- function(ax, type_annotation = c("xaxis", "yaxis", "points"),
|
||||
as_date = FALSE, ...) {
|
||||
as_date = FALSE, position = "back", ...) {
|
||||
type_annotation <- match.arg(type_annotation)
|
||||
config <- dropNullsOrEmpty(list(...))
|
||||
if (!is.null(config$label) && is.character(config$label)) {
|
||||
config$label <- list(text = config$label)
|
||||
}
|
||||
if (identical(type_annotation, "yaxis")) {
|
||||
len <- length(config$y)
|
||||
} else {
|
||||
|
@ -14,8 +17,8 @@ add_annotation <- function(ax, type_annotation = c("xaxis", "yaxis", "points"),
|
|||
length.out = len,
|
||||
how = "replace"
|
||||
)
|
||||
extract <- function(el, position) {
|
||||
`[`(el, position)
|
||||
extract <- function(el, index) {
|
||||
`[`(el, index)
|
||||
}
|
||||
annotations <- lapply(
|
||||
X = seq_len(len),
|
||||
|
@ -23,7 +26,7 @@ add_annotation <- function(ax, type_annotation = c("xaxis", "yaxis", "points"),
|
|||
this <- rapply(
|
||||
object = config,
|
||||
f = extract,
|
||||
position = i,
|
||||
index = i,
|
||||
how = "list"
|
||||
)
|
||||
if (isTRUE(as_date)) {
|
||||
|
@ -52,7 +55,7 @@ add_annotation <- function(ax, type_annotation = c("xaxis", "yaxis", "points"),
|
|||
}
|
||||
ax <- ax_annotations(
|
||||
ax = ax,
|
||||
position = "back",
|
||||
position = position,
|
||||
yaxis = annotations
|
||||
)
|
||||
} else if (identical(type_annotation, "points")) {
|
||||
|
@ -78,7 +81,7 @@ add_annotation <- function(ax, type_annotation = c("xaxis", "yaxis", "points"),
|
|||
#' @param text Text for the annotation label.
|
||||
#' @param borderColor Border color for the label.
|
||||
#' @param borderWidth Border width for the label.
|
||||
#' @param textAnchor The alignment of text relative to label’s drawing position.
|
||||
#' @param textAnchor The alignment of text relative to label's drawing position.
|
||||
#' @param position Available options: left or right.
|
||||
#' @param offsetX Sets the left offset for annotation label.
|
||||
#' @param offsetY Sets the top offset for annotation label.
|
||||
|
@ -142,7 +145,8 @@ label <- function(text = NULL,
|
|||
#' @param to Vector of position to end shadow.
|
||||
#' @param color Color of the shadow.
|
||||
#' @param opacity Opacity of the shadow.
|
||||
#' @param label Add a label to the shade, see \code{\link{label}}.
|
||||
#' @param label Add a label to the shade, use a \code{character}
|
||||
#' or see \code{\link{label}} for more controls.
|
||||
#' @param ... Additional arguments, see
|
||||
#' \url{https://apexcharts.com/docs/options/annotations/} for possible options.
|
||||
#'
|
||||
|
@ -216,9 +220,11 @@ add_shade_weekend <- function(ax, color = "#848484", opacity = 0.2, label = NULL
|
|||
#' @param ax An \code{apexcharts} \code{htmlwidget} object.
|
||||
#' @param when Vector of position to place the event.
|
||||
#' @param color Color of the line.
|
||||
#' @param strokeDashArray Creates dashes in borders of SVG path.
|
||||
#' A higher number creates more space between dashes in the border.
|
||||
#' @param label Add a label to the shade, see \code{\link{label}}.
|
||||
#' @param dash Creates dashes in borders of SVG path.
|
||||
#' A higher number creates more space between dashes in the border.
|
||||
#' Use \code{0} for plain line.
|
||||
#' @param label Add a label to the shade, use a \code{character}
|
||||
#' or see \code{\link{label}} for more controls.
|
||||
#' @param ... Additional arguments, see
|
||||
#' \url{https://apexcharts.com/docs/options/annotations/} for possible options.
|
||||
#'
|
||||
|
@ -226,14 +232,14 @@ add_shade_weekend <- function(ax, color = "#848484", opacity = 0.2, label = NULL
|
|||
#' @export
|
||||
#'
|
||||
#' @example examples/add_event.R
|
||||
add_event <- function(ax, when, color = "#E41A1C", strokeDashArray = 4, label = NULL, ...) {
|
||||
add_event <- function(ax, when, color = "#E41A1C", dash = 4, label = NULL, ...) {
|
||||
add_annotation(
|
||||
ax = ax,
|
||||
type_annotation = "xaxis",
|
||||
as_date = TRUE,
|
||||
x = when,
|
||||
borderColor = color,
|
||||
strokeDashArray = strokeDashArray,
|
||||
strokeDashArray = dash,
|
||||
label = label,
|
||||
...
|
||||
)
|
||||
|
@ -244,8 +250,53 @@ add_event <- function(ax, when, color = "#E41A1C", strokeDashArray = 4, label =
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
#' Add horizontal or vertical line
|
||||
#'
|
||||
#' @param ax An \code{apexcharts} \code{htmlwidget} object.
|
||||
#' @param value Vector of position for the line(s).
|
||||
#' @param color Color(s) of the line(s).
|
||||
#' @param dash Creates dashes in borders of SVG path.
|
||||
#' A higher number creates more space between dashes in the border.
|
||||
#' Use \code{0} for plain line.
|
||||
#' @param label Add a label to the shade, use a \code{character}
|
||||
#' or see \code{\link{label}} for more controls.
|
||||
#' @param ... Additional arguments, see
|
||||
#' \url{https://apexcharts.com/docs/options/annotations/} for possible options.
|
||||
#'
|
||||
#' @return An \code{apexcharts} \code{htmlwidget} object.
|
||||
#' @export
|
||||
#'
|
||||
#' @name add-lines
|
||||
#'
|
||||
#' @example examples/add-lines.R
|
||||
add_hline <- function(ax, value, color = "#000", dash = 0, label = NULL, ...) {
|
||||
add_annotation(
|
||||
ax = ax,
|
||||
type_annotation = "yaxis",
|
||||
position = "front",
|
||||
as_date = FALSE,
|
||||
y = value,
|
||||
borderColor = color,
|
||||
strokeDashArray = dash,
|
||||
label = label,
|
||||
...
|
||||
)
|
||||
}
|
||||
#' @export
|
||||
#' @rdname add-lines
|
||||
add_vline <- function(ax, value, color = "#000", dash = 0, label = NULL, ...) {
|
||||
add_annotation(
|
||||
ax = ax,
|
||||
type_annotation = "xaxis",
|
||||
position = "front",
|
||||
as_date = FALSE,
|
||||
x = value,
|
||||
borderColor = color,
|
||||
strokeDashArray = dash,
|
||||
label = label,
|
||||
...
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
|
||||
library(apexcharter)
|
||||
|
||||
# On a column chart
|
||||
apex(
|
||||
data = table(unhcr_popstats_2017$continent_residence),
|
||||
aes(Var1, Freq),
|
||||
"column"
|
||||
) %>%
|
||||
add_hline(value = 2100)
|
||||
|
||||
|
||||
# On a scatter chart
|
||||
apex(
|
||||
data = iris,
|
||||
aes(Sepal.Length, Sepal.Width),
|
||||
"scatter"
|
||||
) %>%
|
||||
add_hline(value = mean(iris$Sepal.Width)) %>%
|
||||
add_vline(value = mean(iris$Sepal.Length))
|
||||
|
||||
# With labels
|
||||
apex(
|
||||
data = iris,
|
||||
aes(Sepal.Length, Sepal.Width),
|
||||
"scatter"
|
||||
) %>%
|
||||
add_hline(
|
||||
value = mean(iris$Sepal.Width),
|
||||
label = "Mean of Sepal.Width"
|
||||
) %>%
|
||||
add_vline(
|
||||
value = mean(iris$Sepal.Length),
|
||||
label = "Mean of Sepal.Length"
|
||||
)
|
|
@ -18,7 +18,15 @@ apex(consumption, aes(date, value, group = type), "spline") %>%
|
|||
to = c("2020-01-20", "2020-02-10")
|
||||
)
|
||||
|
||||
# add other options
|
||||
|
||||
# Add a label
|
||||
apex(consumption, aes(date, value, group = type), "spline") %>%
|
||||
add_shade(
|
||||
from = "2020-01-06", to = "2020-01-20",
|
||||
label = "interesting period"
|
||||
)
|
||||
|
||||
# add label with more options
|
||||
apex(consumption, aes(date, value, group = type), "spline") %>%
|
||||
add_shade(
|
||||
from = "2020-01-06", to = "2020-01-20",
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/annotations.R
|
||||
\name{add-lines}
|
||||
\alias{add-lines}
|
||||
\alias{add_hline}
|
||||
\alias{add_vline}
|
||||
\title{Add horizontal or vertical line}
|
||||
\usage{
|
||||
add_hline(ax, value, color = "#000", dash = 0, label = NULL, ...)
|
||||
|
||||
add_vline(ax, value, color = "#000", dash = 0, label = NULL, ...)
|
||||
}
|
||||
\arguments{
|
||||
\item{ax}{An \code{apexcharts} \code{htmlwidget} object.}
|
||||
|
||||
\item{value}{Vector of position for the line(s).}
|
||||
|
||||
\item{color}{Color(s) of the line(s).}
|
||||
|
||||
\item{dash}{Creates dashes in borders of SVG path.
|
||||
A higher number creates more space between dashes in the border.
|
||||
Use \code{0} for plain line.}
|
||||
|
||||
\item{label}{Add a label to the shade, use a \code{character}
|
||||
or see \code{\link{label}} for more controls.}
|
||||
|
||||
\item{...}{Additional arguments, see
|
||||
\url{https://apexcharts.com/docs/options/annotations/} for possible options.}
|
||||
}
|
||||
\value{
|
||||
An \code{apexcharts} \code{htmlwidget} object.
|
||||
}
|
||||
\description{
|
||||
Add horizontal or vertical line
|
||||
}
|
||||
\examples{
|
||||
|
||||
library(apexcharter)
|
||||
|
||||
# On a column chart
|
||||
apex(
|
||||
data = table(unhcr_popstats_2017$continent_residence),
|
||||
aes(Var1, Freq),
|
||||
"column"
|
||||
) \%>\%
|
||||
add_hline(value = 2100)
|
||||
|
||||
|
||||
# On a scatter chart
|
||||
apex(
|
||||
data = iris,
|
||||
aes(Sepal.Length, Sepal.Width),
|
||||
"scatter"
|
||||
) \%>\%
|
||||
add_hline(value = mean(iris$Sepal.Width)) \%>\%
|
||||
add_vline(value = mean(iris$Sepal.Length))
|
||||
|
||||
# With labels
|
||||
apex(
|
||||
data = iris,
|
||||
aes(Sepal.Length, Sepal.Width),
|
||||
"scatter"
|
||||
) \%>\%
|
||||
add_hline(
|
||||
value = mean(iris$Sepal.Width),
|
||||
label = "Mean of Sepal.Width"
|
||||
) \%>\%
|
||||
add_vline(
|
||||
value = mean(iris$Sepal.Length),
|
||||
label = "Mean of Sepal.Length"
|
||||
)
|
||||
}
|
|
@ -21,7 +21,8 @@ add_shade_weekend(ax, color = "#848484", opacity = 0.2, label = NULL, ...)
|
|||
|
||||
\item{opacity}{Opacity of the shadow.}
|
||||
|
||||
\item{label}{Add a label to the shade, see \code{\link{label}}.}
|
||||
\item{label}{Add a label to the shade, use a \code{character}
|
||||
or see \code{\link{label}} for more controls.}
|
||||
|
||||
\item{...}{Additional arguments, see
|
||||
\url{https://apexcharts.com/docs/options/annotations/} for possible options.}
|
||||
|
@ -58,7 +59,15 @@ apex(consumption, aes(date, value, group = type), "spline") \%>\%
|
|||
to = c("2020-01-20", "2020-02-10")
|
||||
)
|
||||
|
||||
# add other options
|
||||
|
||||
# Add a label
|
||||
apex(consumption, aes(date, value, group = type), "spline") \%>\%
|
||||
add_shade(
|
||||
from = "2020-01-06", to = "2020-01-20",
|
||||
label = "interesting period"
|
||||
)
|
||||
|
||||
# add label with more options
|
||||
apex(consumption, aes(date, value, group = type), "spline") \%>\%
|
||||
add_shade(
|
||||
from = "2020-01-06", to = "2020-01-20",
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
\alias{add_event}
|
||||
\title{Add an event to a chart}
|
||||
\usage{
|
||||
add_event(ax, when, color = "#E41A1C", strokeDashArray = 4, label = NULL, ...)
|
||||
add_event(ax, when, color = "#E41A1C", dash = 4, label = NULL, ...)
|
||||
}
|
||||
\arguments{
|
||||
\item{ax}{An \code{apexcharts} \code{htmlwidget} object.}
|
||||
|
@ -13,10 +13,12 @@ add_event(ax, when, color = "#E41A1C", strokeDashArray = 4, label = NULL, ...)
|
|||
|
||||
\item{color}{Color of the line.}
|
||||
|
||||
\item{strokeDashArray}{Creates dashes in borders of SVG path.
|
||||
A higher number creates more space between dashes in the border.}
|
||||
\item{dash}{Creates dashes in borders of SVG path.
|
||||
A higher number creates more space between dashes in the border.
|
||||
Use \code{0} for plain line.}
|
||||
|
||||
\item{label}{Add a label to the shade, see \code{\link{label}}.}
|
||||
\item{label}{Add a label to the shade, use a \code{character}
|
||||
or see \code{\link{label}} for more controls.}
|
||||
|
||||
\item{...}{Additional arguments, see
|
||||
\url{https://apexcharts.com/docs/options/annotations/} for possible options.}
|
||||
|
|
|
@ -28,7 +28,7 @@ label(
|
|||
|
||||
\item{borderWidth}{Border width for the label.}
|
||||
|
||||
\item{textAnchor}{The alignment of text relative to label’s drawing position.}
|
||||
\item{textAnchor}{The alignment of text relative to label's drawing position.}
|
||||
|
||||
\item{position}{Available options: left or right.}
|
||||
|
||||
|
|
Loading…
Reference in New Issue