Make data lists from a data.frame
Arguments
- df
Required chart data as data.frame.
Except when format is dendrogram, then df is a list, result of hclust function.- 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
- 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.
Value
A list for dataset.source, series.data or other lists:
For boxplot - a named list, see Details and Examples
For dendrogram & treePC - a tree structure, see format in tree 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
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.
See also
some live code samples
Examples
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('%@<br>%@%','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='_')))