apexcharter icon indicating copy to clipboard operation
apexcharter copied to clipboard

Creating box plot with apexcharter

Open NetZissou opened this issue 3 years ago • 2 comments

Hi, is there any way that we could generate a box plot using the apexcharter interface? If not, would you mind providing an example of using R to interact with the raw apexchart API to create a boxplot? Thanks!

NetZissou avatar Jun 18 '21 18:06 NetZissou

Hello,

Currently not supported in apex(), but you can use the full API like this to produce a boxplot:

library(apexcharter)
data("mpg", package = "ggplot2")
boxed <- boxplot(hwy ~ class, data = mpg)

apexchart() %>% 
  ax_chart(type = "boxPlot") %>% 
  ax_xaxis(type = "categories", categories = boxed$names) %>%
  ax_series(
    list(
      name = "boxes",
      type = "boxPlot",
      data = lapply(
        X = seq_along(boxed$names),
        FUN = function(i) {
          list(
            x = boxed$names[i],
            y = c(
              boxed$stats[1, i],
              boxed$stats[2, i],
              boxed$stats[3, i],
              boxed$stats[4, i],
              boxed$stats[5, i]
            )
          )
        }
      )
    )
  )

image

You can also display outliers but doesn't seems to work well with category axis.

Victor

pvictor avatar Jun 21 '21 09:06 pvictor

Thank you, Victor. This is exactly what I am looking for. One question on top of this, could we add a smooth line on top of these boxes? It seems like the functions right now only support charts rendered by apex().

NetZissou avatar Jun 21 '21 14:06 NetZissou