Skip to content

Commit

Permalink
feat: ✨ added e_boxplot_group_ for specific usecases
Browse files Browse the repository at this point in the history
  • Loading branch information
munoztd0 committed Nov 25, 2024
1 parent 063883b commit 06c2e5d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ Suggests:
leaflet,
tibble
Depends: R (>= 4.1.0)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
URL: https://echarts4r.john-coene.com/, https://github.com/JohnCoene/echarts4r
BugReports: https://github.com/JohnCoene/echarts4r/issues/
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export(e_bar_3d_)
export(e_bezier_curve_g)
export(e_boxplot)
export(e_boxplot_)
export(e_boxplot_group)
export(e_brush)
export(e_button)
export(e_calendar)
Expand Down
62 changes: 62 additions & 0 deletions R/add_.R
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,68 @@ e_boxplot_ <- function(e, serie, name = NULL, outliers = TRUE, ...) {
e
}

#' @rdname e_boxplot_group
#' @export
e_boxplot_group <- function(e, serie, name = NULL, outliers = TRUE, ...) {
if (missing(serie)) {
stop("must pass serie", call. = FALSE)
}

# Prepare group names if available
group_names <- if (!is.null(name)) name else names(e$x$data)

# Reset series to ensure clean rendering
e$x$opts$series <- list()

# Iterate through grouped data
for (i in seq_along(e$x$data)) {
# Extract data for current group
current_data <- e$x$data[[i]]

# Compute boxplot statistics for each category if there are multiple categories
if(is.null(e$x$mapping$x)) {
categories <- serie
category_boxplots <- list(boxplot.stats(current_data[[serie]])$stats)
} else {
categories <- unique(current_data[[e$x$mapping$x]])

category_boxplots <- lapply(categories, function(cat) {
cat_data <- current_data[current_data[[e$x$mapping$x]] == cat, ]
boxplot.stats(cat_data[[serie]])$stats
})
}

# Create series for each group
series_item <- list(
name = group_names[i],
type = "boxplot",
data = category_boxplots
)

# Add series
e$x$opts$series[[i]] <- series_item

# Add outliers if requested

if (isTRUE(outliers)) {
e <- .add_outliers(e, serie, i)
}

}

# Set x-axis with all categories

e$x$opts$xAxis[[1]]$data <- categories
e$x$opts$xAxis[[1]]$type <- "category"


# Add legend
e$x$opts$legend$data <- as.list(group_names)
e$x$opts$legend$show <- TRUE

e
}

#' @rdname e_tree
#' @export
e_tree_ <- function(e, rm_x = TRUE, rm_y = TRUE, ...) {
Expand Down
2 changes: 1 addition & 1 deletion man/connections.Rd

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

0 comments on commit 06c2e5d

Please sign in to comment.