From b313b4598964a464e0954fce75aaa5293e586579 Mon Sep 17 00:00:00 2001 From: pvictor Date: Mon, 13 May 2024 11:20:25 +0200 Subject: [PATCH] added slope charts --- R/apex.R | 20 +++++++++++++++----- man/apex.Rd | 2 +- package-lock.json | 14 +++++++------- vignettes/apexcharter.Rmd | 23 +++++++++++++++++++++++ 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/R/apex.R b/R/apex.R index 8d14a96a..79d02d92 100644 --- a/R/apex.R +++ b/R/apex.R @@ -14,7 +14,7 @@ #' `"pie"`, `"donut"`, #' `"radialBar"`, `"radar"`, `"scatter"`, #' `"heatmap"`, `"treemap"`, -#' `"timeline"` and `"dumbbell"`. +#' `"timeline"`, `"dumbbell"` and `"slope"`. #' @param ... Other arguments passed on to methods. Not currently used. #' @param synchronize Give a common id to charts to synchronize them (tooltip and zoom). #' @param serie_name Name for the serie displayed in tooltip, @@ -44,7 +44,7 @@ apex <- function(data, mapping, choices = c( "column", "bar", "rangeBar", "dumbbell", - "line", "spline", "step", + "line", "spline", "step", "slope", "area", "area-spline", "area-step", "rangeArea", "pie", "donut", @@ -69,7 +69,7 @@ apex <- function(data, mapping, type <- "bubble" } mapdata <- lapply(mapping, rlang::eval_tidy, data = data) - type_no_compute <- c("candlestick", "boxplot", "timeline", "heatmap", "rangeArea", "rangeBar", "dumbbell") + type_no_compute <- c("candlestick", "boxplot", "timeline", "heatmap", "rangeArea", "rangeBar", "dumbbell", "slope") if (is.null(mapdata$y) & !type %in% type_no_compute) { mapdata <- compute_count(mapdata) } @@ -179,7 +179,7 @@ make_series <- function(mapdata, mapping, type = NULL, serie_name = NULL, force_ ))) if (is_grouped(mapping)) { mapdata <- rename_aes(mapdata) - len_grp <- tapply(mapdata$group, mapdata$group, length) + len_grp <- tapply(as.character(mapdata$group), as.character(mapdata$group), length) if (length(unique(len_grp)) > 1 & !isTRUE(type %in% c("scatter", "bubble"))) { warning("apex: all groups must have same length! You can use `tidyr::complete` for this.") } @@ -258,7 +258,7 @@ list1 <- function(x) { correct_type <- function(type) { if (isTRUE(type %in% c("column"))) { "bar" - } else if (isTRUE(type %in% c("spline", "step"))) { + } else if (isTRUE(type %in% c("spline", "step", "slope"))) { "line" } else if (isTRUE(type %in% c("area-spline", "area-step"))) { "area" @@ -344,6 +344,7 @@ choose_config <- function(type, mapdata) { "timeline" = config_timeline(), "candlestick" = config_candlestick(), "boxplot" = config_boxplot(horizontal = box_horiz), + "slope" = config_slope(), list() ) } @@ -464,3 +465,12 @@ config_boxplot <- function(horizontal = FALSE) { ) } +config_slope <- function() { + list( + plotOptions = list( + line = list( + isSlopeChart = TRUE + ) + ) + ) +} diff --git a/man/apex.Rd b/man/apex.Rd index 74e077a6..fbf17370 100644 --- a/man/apex.Rd +++ b/man/apex.Rd @@ -30,7 +30,7 @@ a \code{data.frame}, it will be coerced to with \code{as.data.frame}.} \code{"pie"}, \code{"donut"}, \code{"radialBar"}, \code{"radar"}, \code{"scatter"}, \code{"heatmap"}, \code{"treemap"}, -\code{"timeline"} and \code{"dumbbell"}.} +\code{"timeline"}, \code{"dumbbell"} and \code{"slope"}.} \item{...}{Other arguments passed on to methods. Not currently used.} diff --git a/package-lock.json b/package-lock.json index e9a3d764..eb43b8d7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0", "license": "MIT", "dependencies": { - "apexcharts": "^3.48.0", + "apexcharts": "^3.49.1", "d3-format": "^3.0.1" }, "devDependencies": { @@ -371,9 +371,9 @@ } }, "node_modules/apexcharts": { - "version": "3.48.0", - "resolved": "https://registry.npmjs.org/apexcharts/-/apexcharts-3.48.0.tgz", - "integrity": "sha512-Lhpj1Ij6lKlrUke8gf+P+SE6uGUn+Pe1TnCJ+zqrY0YMvbqM3LMb1lY+eybbTczUyk0RmMZomlTa2NgX2EUs4Q==", + "version": "3.49.1", + "resolved": "https://registry.npmjs.org/apexcharts/-/apexcharts-3.49.1.tgz", + "integrity": "sha512-MqGtlq/KQuO8j0BBsUJYlRG8VBctKwYdwuBtajHgHTmSgUU3Oai+8oYN/rKCXwXzrUlYA+GiMgotAIbXY2BCGw==", "dependencies": { "@yr/monotone-cubic-spline": "^1.0.3", "svg.draggable.js": "^2.2.2", @@ -2030,9 +2030,9 @@ "requires": {} }, "apexcharts": { - "version": "3.48.0", - "resolved": "https://registry.npmjs.org/apexcharts/-/apexcharts-3.48.0.tgz", - "integrity": "sha512-Lhpj1Ij6lKlrUke8gf+P+SE6uGUn+Pe1TnCJ+zqrY0YMvbqM3LMb1lY+eybbTczUyk0RmMZomlTa2NgX2EUs4Q==", + "version": "3.49.1", + "resolved": "https://registry.npmjs.org/apexcharts/-/apexcharts-3.49.1.tgz", + "integrity": "sha512-MqGtlq/KQuO8j0BBsUJYlRG8VBctKwYdwuBtajHgHTmSgUU3Oai+8oYN/rKCXwXzrUlYA+GiMgotAIbXY2BCGw==", "requires": { "@yr/monotone-cubic-spline": "^1.0.3", "svg.draggable.js": "^2.2.2", diff --git a/vignettes/apexcharter.Rmd b/vignettes/apexcharter.Rmd index 21bb0525..803b21d4 100644 --- a/vignettes/apexcharter.Rmd +++ b/vignettes/apexcharter.Rmd @@ -336,3 +336,26 @@ apex(life_expec, aes(country, x = `1972`, xend = `2007`), type = "dumbbell") %>% ) ``` + + +## Slope charts + +Create a slope chart with: + +```{r slope} +data("life_expec", package = "apexcharter") + +apex( + life_expec_long, + mapping = aes(x = as.character(year), y = lifeExp, fill = country), + type = "slope" +) %>% + ax_colors("#112466") %>% + ax_labs( + title = "Life expectancy : 1972 vs. 2007", + subtitle = "Data from Gapminder dataset", + x = "Life expectancy at birth, in years" + ) +``` + +