From 5be96dcd7731f162884fe6460167cb24abe7d3a2 Mon Sep 17 00:00:00 2001 From: pvictor Date: Sun, 26 Jul 2020 10:46:18 +0200 Subject: [PATCH] added add_line() --- NAMESPACE | 1 + R/apex.R | 2 ++ R/apexcharter.R | 5 ++- R/mixed-charts.R | 53 ++++++++++++++++++++++++++++++ examples/mixed-charts.R | 38 ++++++++++++++++++++++ man/add_line.Rd | 72 +++++++++++++++++++++++++++++++++++++++++ man/climate_paris.Rd | 24 ++++++++++++++ 7 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 R/mixed-charts.R create mode 100644 examples/mixed-charts.R create mode 100644 man/add_line.Rd create mode 100644 man/climate_paris.Rd diff --git a/NAMESPACE b/NAMESPACE index 05cd6f2..d891882 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ export(JS) export(add_event) export(add_event_marker) export(add_hline) +export(add_line) export(add_point) export(add_shade) export(add_shade_weekend) diff --git a/R/apex.R b/R/apex.R index 7e4b2c7..982b704 100644 --- a/R/apex.R +++ b/R/apex.R @@ -94,6 +94,8 @@ apex <- function(data, mapping, type = "column", ..., max = max(mapdata$x, na.rm = TRUE) ) } + ax$x$data <- data + class(ax) <- c(class(ax), "apex") return(ax) } diff --git a/R/apexcharter.R b/R/apexcharter.R index 6ffa18c..612a473 100644 --- a/R/apexcharter.R +++ b/R/apexcharter.R @@ -35,7 +35,10 @@ apexchart <- function(ax_opts = list(), auto_update = TRUE, width = NULL, height height = height, package = "apexcharter", elementId = elementId, - preRenderHook = add_locale_apex, + preRenderHook = function(widget) { + widget$x$data <- NULL + add_locale_apex(widget) + }, sizingPolicy = htmlwidgets::sizingPolicy( defaultWidth = "100%", defaultHeight = "100%", diff --git a/R/mixed-charts.R b/R/mixed-charts.R new file mode 100644 index 0000000..c5248d0 --- /dev/null +++ b/R/mixed-charts.R @@ -0,0 +1,53 @@ + +#' Add a line to a chart +#' +#' @param ax An \code{\link{apex}} \code{htmlwidget} object. +#' @param mapping Default list of aesthetic mappings to use for chart. +#' @param data A \code{data.frame} to use to add a line, if \code{NULL} (default), +#' the \code{data.frame} provided in \code{apex()} will be used. +#' @param type Type of line. +#' @param serie_name Name for the serie displayed in tooltip and legend. +#' +#' @return A \code{apexcharts} \code{htmlwidget} object. +#' @export +#' +#' @example examples/mixed-charts.R +add_line <- function(ax, mapping, data = NULL, type = c("line", "spline"), serie_name = NULL) { + type <- match.arg(type) + if (!inherits(ax, "apex")) + stop("add_line: ax must have been created with apex() function.", call. = FALSE) + if (is.null(ax$x$mixed_type)) { + apex_type <- ax$x$ax_opts$chart$type + ax$x$mixed_type <- apex_type + } else { + apex_type <- ax$x$mixed_type + } + if (!isTRUE(apex_type %in% c("line", "bar", "scatter"))) + stop("add_line: apex() must be a column or scatter chart.", call. = FALSE) + ax$x$ax_opts$chart$type <- "line" + if (is.null(data)) + data <- ax$x$data + data <- as.data.frame(data) + mapdata <- lapply(mapping, rlang::eval_tidy, data = data) + ax$x$ax_opts$series <- c( + ax$x$ax_opts$series, + make_series(mapdata, mapping, type, serie_name) + ) + if (identical(apex_type, "scatter")) { + if (is.null(ax$x$ax_opts$markers$size)) { + ax$x$ax_opts$markers$size <- c(6, 0) + } else { + ax$x$ax_opts$markers$size <- c(ax$x$ax_opts$markers$size, 0) + } + } + if (identical(apex_type, "bar")) { + if (is.null(ax$x$ax_opts$stroke$width)) { + ax$x$ax_opts$stroke$width <- c(0, 4) + } else { + ax$x$ax_opts$stroke$width <- c(ax$x$ax_opts$stroke$width, 4) + } + } + return(ax) +} + + diff --git a/examples/mixed-charts.R b/examples/mixed-charts.R new file mode 100644 index 0000000..d95f079 --- /dev/null +++ b/examples/mixed-charts.R @@ -0,0 +1,38 @@ +library(apexcharter) + + +# Bar ---- + +data("climate_paris") + +# Add a line on a column's chart +apex(climate_paris, aes(month, precipitation), type = "column") %>% + add_line(aes(month, temperature)) + +# Add secondary axis +apex(climate_paris, aes(month, precipitation), type = "column") %>% + add_line(aes(month, temperature)) %>% + ax_yaxis( + title = list(text = "Precipitation (in mm)") + ) %>% + ax_yaxis2( + opposite = TRUE, + decimalsInFloat = 0, + title = list(text = "Temperature (in degree celsius)") + ) %>% + ax_dataLabels( + enabled = TRUE, enabledOnSeries = list(1) + ) + + + +# Scatter ---- + +# add smooth line on scatter plot +apex(cars, aes(speed, dist), type = "scatter") %>% + add_line(aes(x, y), data = lowess(cars), serie_name = "lowess") + + + + + diff --git a/man/add_line.Rd b/man/add_line.Rd new file mode 100644 index 0000000..65b1ed0 --- /dev/null +++ b/man/add_line.Rd @@ -0,0 +1,72 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/mixed-charts.R +\name{add_line} +\alias{add_line} +\title{Add a line to a chart} +\usage{ +add_line( + ax, + mapping, + data = NULL, + type = c("line", "spline"), + serie_name = NULL +) +} +\arguments{ +\item{ax}{An \code{\link{apex}} \code{htmlwidget} object.} + +\item{mapping}{Default list of aesthetic mappings to use for chart.} + +\item{data}{A \code{data.frame} to use to add a line, if \code{NULL} (default), +the \code{data.frame} provided in \code{apex()} will be used.} + +\item{type}{Type of line.} + +\item{serie_name}{Name for the serie displayed in tooltip and legend.} +} +\value{ +A \code{apexcharts} \code{htmlwidget} object. +} +\description{ +Add a line to a chart +} +\examples{ +library(apexcharter) + + +# Bar ---- + +data("climate_paris") + +# Add a line on a column's chart +apex(climate_paris, aes(month, precipitation), type = "column") \%>\% + add_line(aes(month, temperature)) + +# Add secondary axis +apex(climate_paris, aes(month, precipitation), type = "column") \%>\% + add_line(aes(month, temperature)) \%>\% + ax_yaxis( + title = list(text = "Precipitation (in mm)") + ) \%>\% + ax_yaxis2( + opposite = TRUE, + decimalsInFloat = 0, + title = list(text = "Temperature (in degree celsius)") + ) \%>\% + ax_dataLabels( + enabled = TRUE, enabledOnSeries = list(1) + ) + + + +# Scatter ---- + +# add smooth line on scatter plot +apex(cars, aes(speed, dist), type = "scatter") \%>\% + add_line(aes(x, y), data = lowess(cars), serie_name = "lowess") + + + + + +} diff --git a/man/climate_paris.Rd b/man/climate_paris.Rd new file mode 100644 index 0000000..efbc25c --- /dev/null +++ b/man/climate_paris.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.R +\docType{data} +\name{climate_paris} +\alias{climate_paris} +\title{Paris Climate} +\format{ +A data frame with 12 observations and the following 3 variables: +\describe{ +\item{\code{month}}{Month} +\item{\code{temperature}}{Temperature (in degree celsius).} +\item{\code{precipitation}}{Precipitation (in mm).} +} +} +\source{ +Wikipedia (\url{https://fr.wikipedia.org/wiki/Climat_de_Paris}) +} +\usage{ +climate_paris +} +\description{ +Average temperature and precipitation in Paris for the period 1971-2000. +} +\keyword{datasets}