added add_line()

This commit is contained in:
pvictor 2020-07-26 10:46:18 +02:00
parent d44581dc2a
commit 5be96dcd77
7 changed files with 194 additions and 1 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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%",

53
R/mixed-charts.R Normal file
View File

@ -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)
}

38
examples/mixed-charts.R Normal file
View File

@ -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")

72
man/add_line.Rd Normal file
View File

@ -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")
}

24
man/climate_paris.Rd Normal file
View File

@ -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}