dreamRs / apexcharter

:bar_chart: R Htmlwidget for ApexCharts.js
https://dreamrs.github.io/apexcharter
Other
138 stars 15 forks source link

Creating box plot with apexcharter #52

Open NetZissou opened 3 years ago

NetZissou commented 3 years ago

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!

pvictor commented 3 years ago

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

NetZissou commented 3 years ago

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().