quick line, spline & area

This commit is contained in:
pvictor 2019-02-14 16:37:40 +01:00
parent 2b9bbba54d
commit 00bde78ee3
3 changed files with 96 additions and 8 deletions

View File

@ -4,7 +4,7 @@
#' @param data Default dataset to use for chart. If not already a \code{data.frame}, it will be coerced to with \code{as.data.frame}.
#' @param mapping Default list of aesthetic mappings to use for chart
#' @param type Specify the chart type. Available Options: \code{"column"}, \code{"bar"}, \code{"line"},
#' \code{"area"}, \code{"pie"}, \code{"donut"}, \code{"radialBar"}, \code{"scatter"}, \code{"bubble"}, \code{"heatmap"}.
#' \code{"area"}, \code{"spline"}, \code{"pie"}, \code{"donut"}, \code{"radialBar"}, \code{"scatter"}, \code{"bubble"}, \code{"heatmap"}.
#' @param ... Other arguments passed on to methods. Not currently used.
#' @param width A numeric input in pixels.
#' @param height A numeric input in pixels.
@ -16,22 +16,23 @@
#' @importFrom utils modifyList
#'
apex <- function(data, mapping, type = "column", ..., width = NULL, height = NULL, elementId = NULL) {
type <- match.arg(type, c("column", "bar", "line", "area", "pie", "donut", "radialBar", "scatter", "bubble", "heatmap"))
type <- match.arg(type, c("column", "bar", "line", "area", "spline", "area-spline", "pie", "donut", "radialBar", "scatter", "bubble", "heatmap"))
data <- as.data.frame(data)
mapdata <- lapply(mapping, rlang::eval_tidy, data = data)
opts <- list(
chart = list(type = correct_type(type)),
series = make_series(data, mapping, type)
series = make_series(mapdata, mapping, type)
)
opts <- modifyList(opts, choose_config(type, mapping))
opts <- modifyList(opts, choose_config(type, is_datetime(mapdata)))
apexcharter(ax_opts = opts, width = width, height = height, elementId = elementId)
}
# Construct series
make_series <- function(data, mapping, type) {
mapdata <- lapply(mapping, rlang::eval_tidy, data = data)
make_series <- function(mapdata, mapping, type) {
mapdata <- as.data.frame(mapdata)
series_names <- "Series"
series <- list()
if (identical(names(mapping), c("x", "y"))) {
series_names <- rlang::as_name(mapping$y)
series <- list(list(
@ -54,12 +55,18 @@ make_series <- function(data, mapping, type) {
}
is_datetime <- function(mapdata) {
inherits(mapdata$x, what = c("Date", "POSIXt"))
}
# Change type of charts for helpers type
correct_type <- function(type) {
if (identical(type, "column")) {
"bar"
} else if (identical(type, "spline")) {
"line"
} else {
type
}
@ -69,11 +76,14 @@ correct_type <- function(type) {
# Switch between auto configs according to type & mapping
choose_config <- function(type, mapping) {
choose_config <- function(type, datetime) {
switch(
type,
"bar" = config_bar(horizontal = TRUE),
"column" = config_bar(horizontal = FALSE),
"line" = config_line(datetime = datetime),
"area" = config_line(datetime = datetime),
"spline" = config_line(curve = "smooth", datetime = datetime),
list()
)
}
@ -98,3 +108,20 @@ config_bar <- function(horizontal = FALSE) {
}
config
}
# Config for line, spline, area, area-spline
config_line <- function(curve = "straight", datetime = FALSE) {
config <- list(
stroke = list(
curve = curve
)
)
if (isTRUE(datetime)) {
config <- c(config, list(
xaxis = list(type = "datetime")
))
}
config
}

View File

@ -0,0 +1,61 @@
# ------------------------------------------------------------------------
#
# Title : Quick ApexCharts Examples
# By : Victor
# Date : 2019-02-14
#
# ------------------------------------------------------------------------
# Packages ----------------------------------------------------------------
library(apexcharter)
library(ggplot2)
library(dplyr)
# Bar & Column ------------------------------------------------------------
data("mpg")
n_manufac <- count(mpg, manufacturer)
apex(data = n_manufac, type = "bar", mapping = aes(x = manufacturer, y = n))
apex(data = n_manufac, type = "column", mapping = aes(x = manufacturer, y = n))
n_manufac_year <- count(mpg, manufacturer, year)
apex(data = n_manufac_year, type = "bar", mapping = aes(x = manufacturer, y = n, fill = year))
apex(data = n_manufac_year, type = "column", mapping = aes(x = manufacturer, y = n, fill = year))
# Line, Spline & Area -----------------------------------------------------
data("economics")
economics <- tail(economics, 100)
economics_long <- economics_long %>%
group_by(variable) %>%
slice(1:100)
apex(data = economics, type = "line", mapping = aes(x = date, y = uempmed))
apex(data = economics, type = "spline", mapping = aes(x = date, y = uempmed))
apex(data = economics, type = "area", mapping = aes(x = date, y = uempmed))
apex(data = economics_long, type = "line", mapping = aes(x = date, y = value01, fill = variable))
apex(data = economics_long, type = "spline", mapping = aes(x = date, y = value01, fill = variable))
apex(data = economics_long, type = "area", mapping = aes(x = date, y = value01, fill = variable))

View File

@ -13,7 +13,7 @@ apex(data, mapping, type = "column", ..., width = NULL,
\item{mapping}{Default list of aesthetic mappings to use for chart}
\item{type}{Specify the chart type. Available Options: \code{"column"}, \code{"bar"}, \code{"line"},
\code{"area"}, \code{"pie"}, \code{"donut"}, \code{"radialBar"}, \code{"scatter"}, \code{"bubble"}, \code{"heatmap"}.}
\code{"area"}, \code{"spline"}, \code{"pie"}, \code{"donut"}, \code{"radialBar"}, \code{"scatter"}, \code{"bubble"}, \code{"heatmap"}.}
\item{...}{Other arguments passed on to methods. Not currently used.}