Learn R Programming

echarty (version 1.7.0)

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 other lists:

For boxplot - a named list, see Details and Examples

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

Arguments

df

Required chart data as data.frame.
For format dendrogram df is a list, result of hclust function.
For format flame df is an hierarchical list with name,value,children.

format

Output list format

  • 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.

  • dendrogram = build series data for Hierarchical Clustering dendrogram

  • treePC = build series data for tree charts from parent/children data.frame

  • treeTT = build series data for tree charts from data.frame like Titanic.

  • boxplot = build dataset and source lists, see Details

  • borders = build geoJson string from map_data region borders, see Details

  • flame = list of hierarchical series data to be presented by renderItem

header

for dataset, to include the column names or not, default TRUE. Set it to FALSE for series.data.

...

optional parameters
Optional parameters for boxplot are:

  • layout = 'h' for horizontal(default) or 'v' for vertical layout

  • outliers boolean to add outlier points (default FALSE)

  • jitter value for jitter of numerical values in second column, default 0 (no scatter). Adds scatter series on top of boxplot.

Optional parameter for names:

  • nasep = single character name separator for nested lists, see Examples.
    Purpose is to facilitate conversion from data.frame to nested named lists.

Optional parameter for flame:

  • name = name of subtree to search for.

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
Additional grouping is supported on a column after the second. Groups will show in the legend, if enabled.
Returns a list(dataset, series, xAxis, yAxis) to set params in ec.init. Make sure there is enough data for computation, 4+ values per boxplot.

format='treeTT' expects data.frame df columns pathString,value,(optional itemStyle) for FromDataFrameTable.
It will add column 'pct' with value percentage for each node. See example below.

format='borders' expects df columns long,lat,region,subregion as in map_data.
Result to be used as map in ec.registerMap. See borders code example in examples.R.
This is a slow version for borders, another very fast one is offered as echarty extra, see website.

See Also

some live code samples

Examples

Run this code
library(dplyr)
ds <- iris |> relocate(Species) |>
	 ec.data(format= 'boxplot', jitter= 0.1, layout= 'v')
ec.init(
  dataset= ds$dataset, series= ds$series, xAxis= ds$xAxis, yAxis= ds$yAxis,
  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;}"))
  ))
)

# build required pathString,value and optional itemStyle columns
df <- as.data.frame(Titanic) |> rename(value= Freq) |> mutate(
  pathString= paste('Titanic\nSurvival', Survived, Age, Sex, Class, sep='/'),
	 itemStyle= case_when(Survived=='Yes' ~"color='green'", TRUE ~"color='LightSalmon'")) |>
	 select(pathString, value, itemStyle)
ec.init(
	  series= list(list(
		  data= ec.data(df, format='treeTT'),
		  type= 'tree', symbolSize= ec.clmn("(x) => {return Math.log(x)*10}")
	  )),
	  tooltip= list(formatter= ec.clmn('%@%@%','value','pct'))
)

# column itemStyle_color will become itemStyle= list(color=...) in data list
# attribute names separator (nasep) is "_"
df <- data.frame(name= c('A','B','C'), value= c(1,2,3), 
     itemStyle_color= c('chartreuse','lightblue','pink'),
     itemStyle_decal_symbol= c('rect','diamond','none'),
     emphasis_itemStyle_color= c('darkgreen','blue','red')
)
ec.init(series.param= list(type='pie', data= ec.data(df, 'names', nasep='_')))

Run the code above in your browser using DataLab