Skip to content

Commit

Permalink
added slope charts
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed May 13, 2024
1 parent 7e41893 commit b313b45
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
20 changes: 15 additions & 5 deletions R/apex.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand All @@ -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)
}
Expand Down Expand Up @@ -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.")
}
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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()
)
}
Expand Down Expand Up @@ -464,3 +465,12 @@ config_boxplot <- function(horizontal = FALSE) {
)
}

config_slope <- function() {
list(
plotOptions = list(
line = list(
isSlopeChart = TRUE
)
)
)
}
2 changes: 1 addition & 1 deletion man/apex.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions vignettes/apexcharter.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
```


0 comments on commit b313b45

Please sign in to comment.