Learn R Programming

echarty (version 1.4.7)

ec.data: Data helper

Description

Make data lists from a data.frame

Usage

ec.data(df, format = "dataset", header = FALSE)

Value

A list for dataset.source, series.data or a list of named lists.

For boxplot - a named list, see Details and Examples

For dendrogram & treePC - a tree structure, see format in tree data

Arguments

df

Chart data in data.frame format, required.
Except when format is 'dendrogram', then df is a list, result of hclust function.

format

A key on how to format the output list

  • 'dataset' = list to be used in dataset (default), or in series.data (without header).

  • 'values' = list for customized series.data

  • 'names' = named lists useful for named data like sankey links.

  • 'boxplot' = build dataset and source lists, see Details

  • 'dendrogram' = build series data for Hierarchical Clustering dendrogram

  • 'treePC' = build series data for sunburst,tree,treemap from parent/children data.frame

  • 'treeTK' = build series data for sunburst,tree,treemap from data.frame like Titanic. Supports column itemStyle.

header

Boolean to include the column names in dataset, default TRUE.
Set this to FALSE when used in series.data.

Details

format='boxplot' requires the first two df columns as:

  • column for the non-computational categorical axis

  • column with (numeric) data to compute the five boxplot values

Grouped df is supported. Groups will show in the legend, if enabled.
Returns a list(dataset, series, axlbl) to set the chart. axlbl is the category axis label list when data grouped.
Make sure there is enough data for computation, like >4 values per boxplot. Otherwise ECharts may exit with a Object.transform error.

See Also

some live code samples

Examples

Run this code
library(dplyr)
variety <- rep(LETTERS[1:7], each=40)
treatment <- rep(c("high","low"), each=20)
note <- seq(1:280)+sample(1:150, 280, replace=TRUE)
ds <- data.frame(variety, note, treatment) |> group_by(treatment) |> 
        ec.data(format='boxplot')
ec.init(
  dataset= ds$dataset,
  series=  ds$series,
  yAxis= list(type= 'category',  # categorical yAxis = horizontal boxplots
              axisLabel= ds$axlbl),
  xAxis= list(show= TRUE),       # categorical xAxis = vertical boxplots
  legend= list(show= TRUE)
)

ds <- airquality |> mutate(Day=round(Day/10)) |> relocate(Day,Wind) |> ec.data(format='boxplot')
ec.init(
  dataset= ds$dataset, 
  series= ds$series, 
  yAxis= list(type= 'category'), 
  xAxis= list(show= TRUE),
  legend= list(show= TRUE) #, tooltip= list(show=TRUE)
)  

hc <- hclust(dist(USArrests), "complete")
ec.init(preset= FALSE,
        series= list(list(
          type= 'tree', orient= 'TB', roam= TRUE, initialTreeDepth= -1,
          data= ec.data(hc, format='dendrogram'),
          # layout= 'radial', symbolSize= ec.clmn(scale= 0.33),
          ## exclude added labels like 'pXX', leaving only the originals
          label= list(formatter= htmlwidgets::JS(
            "function(n) { out= /p\\d+/.test(n.name) ? '' : n.name; return out;}"))
        ))
)

Run the code above in your browser using DataLab