Title: | Interactive Charts of Nested and Hierarchical Data with 'D3.js' |
---|---|
Description: | Builds interactive 'd3.js' hierarchical visualisation easily. D3partitionR makes it easy to build and customize sunburst, circle treemap, treemap, partition chart, ... |
Authors: | Antoine Guillot [aut, cre], Mike Bostock [aut, cph] (d3.js library, http://d3js.org, nested treemap, icicle, zoomable circle treemap and sunb), Susie Lu [aut, cph] (d3-legend, http://d3-legend.susielu.com/), Kerry Rodden [aut, cph] (Breadcrumb Code for fixed sized breadcrumb.), Nadieh Bremer [aut, cph] (Placement of labels on arcs, https://www.visualcinnamon.com/2015/09/placing-text-on-arcs.html), Hub Spot [aut, cph] (tether.js) |
Maintainer: | Antoine Guillot <[email protected]> |
License: | AGPL-3 |
Version: | 0.5.0 |
Built: | 2025-03-14 05:14:28 UTC |
Source: | https://github.com/antoineguillot2/d3partitionr |
Append data to a D3partitionR object
add_data(D3partitionR_object, data, steps, count = "value", color = "name", label = "name", tooltip = "name", aggregate_fun = NULL)
add_data(D3partitionR_object, data, steps, count = "value", color = "name", label = "name", tooltip = "name", aggregate_fun = NULL)
D3partitionR_object |
The D3partitionR object to which the data should be appended |
data |
a data.frame object |
steps |
The vector of steps to be used |
count |
The variable to be used as the count variable, typically, the number of occurences. |
color |
a variable to use as color (default: name) |
label |
a variable to use as label (default: name) |
tooltip |
a variable to use as tooltip (default: name) |
aggregate_fun |
A named list of function which will be used to aggregates to variables used in color, label or tooltips. This only applies to variable in the provided dataset. |
The D3partitionR object with the appended data
Add informations (for instance new names, colors, ....) to the nodes of a D3_partitionR object
add_nodes_data(D3partitionR_object, nodes_data)
add_nodes_data(D3partitionR_object, nodes_data)
D3partitionR_object |
The D3partitionR object to which the data should be appended |
nodes_data |
a names list where the name of each element is the name of a node. The data will be appended to the node in the nested list |
The D3partitionR object with the appended nodes data
Add a title to a D3partitionR object
add_title(D3partitionR_object, text, style = NULL)
add_title(D3partitionR_object, text, style = NULL)
D3partitionR_object |
The D3partitionR object to which the data should be appended |
text |
Title text |
style |
A valid CSS string which will be applied to the title) |
A D3partitionR object
Aggregate a data.frame in long format with a column containing steps of each session For instance the function can be used with a frame of the form Unique ID - Step - Value 1 - ... -Value N
aggregate_sessions_to_path(data, step_col = "step", id_col = "ID", values_cols = NULL, agg_function_path = sum, agg_function_session = sum, sep = "->")
aggregate_sessions_to_path(data, step_col = "step", id_col = "ID", values_cols = NULL, agg_function_path = sum, agg_function_session = sum, sep = "->")
data |
A dataframe |
step_col |
The name of the column containig the steps. The steps are assumed to be ordered |
id_col |
Column containing the unique identifier of each session |
values_cols |
Names of the other columns to keep. Default: NULL |
agg_function_path |
Aggregation function on a path level |
agg_function_session |
Aggregation function on a session level |
sep |
String used to separate the different steps. Default: "->" |
A data.table with the columns specified in count_col, value_cols and one column per step in the path
Compile D3partitionR object to plot it
compile_D3_partitionR(D3partitionR_object)
compile_D3_partitionR(D3partitionR_object)
D3partitionR_object |
The D3partitionR object to which the data should be appended |
A D3partitionR compiled object
Return al the leaf names
compute_unique_leaf_name(nested_list)
compute_unique_leaf_name(nested_list)
nested_list |
A nested_list where each node has a name attribute |
Creates a D3partitionR object
D3partitionR()
D3partitionR()
A blank D3partitionR object (S3 class)
Output and render functions for using D3partitionR within Shiny applications and interactive Rmd documents.
D3partitionROutput(outputId, width = "100%", height = "400px") renderD3partitionR(expr, env = parent.frame(), quoted = FALSE)
D3partitionROutput(outputId, width = "100%", height = "400px") renderD3partitionR(expr, env = parent.frame(), quoted = FALSE)
outputId |
output variable to read from |
width , height
|
Must be a valid CSS unit (like |
expr |
An expression that generates a D3partitionR |
env |
The environment in which to evaluate |
quoted |
Is |
Transform a dataframe to a nested lists structure (i.e. hierarchical).
df_to_nest(data, step_cols, nodes_data = NULL, count_col = "value", value_cols = NULL, agg_function = sum, na_behavior = "rm")
df_to_nest(data, step_cols, nodes_data = NULL, count_col = "value", value_cols = NULL, agg_function = sum, na_behavior = "rm")
data |
The data frame to convert to the nested structure. It needs to have several comlumns, each ones account for a given step |
step_cols |
vector containing the names of the columns which should be used as steps. The vector should be ordered. ex: c('step1','step2','step3') |
nodes_data |
A named list to add addition informations to each nodes |
count_col |
Number of occurences in this path (succesion of steps). Default: NULL |
value_cols |
Names of the other columns to keep. Default: NULL |
agg_function |
aggregation function to be applied to value_cols.Ex: mean, sum. Default: sum. Weighted version can also be used, the weighting will be done using the counting variable |
na_behavior |
How to deal with missing data ? |
A data.table with the columns specified in count_col, value_cols and one column per step in the path
Find the maximum values of a given var in a tree
find_min_max_tree(nested_list, variable = "value")
find_min_max_tree(nested_list, variable = "value")
nested_list |
A nested_list where each node has a name attribute |
variable |
A nested_list where each node has a name attribute |
Return al the possible nodes names
get_all_nodes_names(nested_list, variable = "name")
get_all_nodes_names(nested_list, variable = "name")
nested_list |
A nested_list where each node has a name attribute |
variable |
the variable to collect |
Check if a variable is present in a D3partitionR object
is_present_variable(variable, D3partitionR_object)
is_present_variable(variable, D3partitionR_object)
variable |
The variable which presence is to be checked |
D3partitionR_object |
The D3partitionR object |
TRUE/FALSE
Plot D3partitionR object
## S3 method for class 'D3partitionR' plot(x, width = NULL, height = NULL, elementId = NULL, sizingPolicy = NULL, ...)
## S3 method for class 'D3partitionR' plot(x, width = NULL, height = NULL, elementId = NULL, sizingPolicy = NULL, ...)
x |
A D3partitionR object to plot |
width |
width of the widget in pixel/percent |
height |
height of the widget in pixel/percent |
elementId |
html id of the widget |
sizingPolicy |
sizing policy |
... |
parameters for method consistency |
require(titanic) require(data.table) ## Reading data titanic_data = data.table(titanic::titanic_train) ##Agregating data to have unique sequence for the 4 variables var_names=c('Sex','Embarked','Pclass','Survived') data_plot=titanic_data[,.N,by=var_names] data_plot[,(var_names):=lapply(var_names,function(x){data_plot[[x]]=paste0(x,' ',data_plot[[x]]) })] ## Plotting the chart library("magrittr") d3=D3partitionR() %>% add_data(data_plot,count = 'N',steps=c('Sex','Embarked','Pclass','Survived')) %>% add_title('Titanic') ## Not run: plot(d3) ## End(Not run)
require(titanic) require(data.table) ## Reading data titanic_data = data.table(titanic::titanic_train) ##Agregating data to have unique sequence for the 4 variables var_names=c('Sex','Embarked','Pclass','Survived') data_plot=titanic_data[,.N,by=var_names] data_plot[,(var_names):=lapply(var_names,function(x){data_plot[[x]]=paste0(x,' ',data_plot[[x]]) })] ## Plotting the chart library("magrittr") d3=D3partitionR() %>% add_data(data_plot,count = 'N',steps=c('Sex','Embarked','Pclass','Survived')) %>% add_title('Titanic') ## Not run: plot(d3) ## End(Not run)
Check if the scale variable is discrete or continuous
scale_type(color_variable, D3partitionR_object)
scale_type(color_variable, D3partitionR_object)
color_variable |
The color variable to be assessed |
D3partitionR_object |
The D3partitionR object |
TRUE/FALSE
Set the chart_type
set_chart_type(D3partitionR_object, chart_type)
set_chart_type(D3partitionR_object, chart_type)
D3partitionR_object |
The D3partitionR object to which the data should be appended |
chart_type |
type fo chart to use (in c('sunburst','treemap','circle_treemap','partition_chart','icicle') ) |
A D3partitionR object
Add a custom discrete color scale
set_continuous_color_scale(D3partitionR_object, color_palette)
set_continuous_color_scale(D3partitionR_object, color_palette)
D3partitionR_object |
The D3partitionR object to which the data should be appended |
color_palette |
a vector of two colors, the first one is use on the bottom of the scale, the other on the top. |
A D3partitionR object
Add a custom discrete color scale
set_discrete_color_scale(D3partitionR_object, color_palette)
set_discrete_color_scale(D3partitionR_object, color_palette)
D3partitionR_object |
The D3partitionR object to which the data should be appended |
color_palette |
A vector (or a named vector with levels of the variable color) |
A D3partitionR object
Set the labels parameters
set_labels_parameters(D3partitionR_object, visible = T, cut_off = 3, style = NULL)
set_labels_parameters(D3partitionR_object, visible = T, cut_off = 3, style = NULL)
D3partitionR_object |
The D3partitionR object to which the data should be appended |
visible |
boolean, should the labels be diplayed ? Default: TRUE |
cut_off |
a numeric variable between 0 and 100. Nodes which represent less than cut_off percents of the current root will have their labels hidden. |
style |
a valid CSS string to be applied to the labels. Default: NULL |
A D3partitionR object
Set the legend parameter
set_legend_parameters(D3partitionR_object, visible = T, zoom_subset = F, width = 100)
set_legend_parameters(D3partitionR_object, visible = T, zoom_subset = F, width = 100)
D3partitionR_object |
The D3partitionR object to which the data should be appended |
visible |
boolean, should the trail be diplayed ? Default: TRUE |
zoom_subset |
boolean, if TRUE, only the modalities present in the children of the zoomed rrot are displayed kin the legend. |
width |
legend width in pixel |
A D3partitionR object
Configuration of a D3partitionR object as a Shiny input
set_shiny_input(D3partitionR_object, input_id, enabled_inputs = list(clicked_node = T, leaves = T, nodes = T, ancestors = T, children_path = F))
set_shiny_input(D3partitionR_object, input_id, enabled_inputs = list(clicked_node = T, leaves = T, nodes = T, ancestors = T, children_path = F))
D3partitionR_object |
The D3partitionR object to which the data should be appended |
input_id |
The id of the input |
enabled_inputs |
which inputs should be enabled ? defaut to list(clicked_node=T,leaf=T,nodes=T,ancestors=T,child_path=F) |
A D3partitionR object
Set the tooltips parameter
set_tooltip_parameters(D3partitionR_object, visible = T, style = NULL, builder = "table")
set_tooltip_parameters(D3partitionR_object, visible = T, style = NULL, builder = "table")
D3partitionR_object |
The D3partitionR object to which the data should be appended |
visible |
boolean, should the trail be diplayed ? Default: TRUE |
style |
a valid CSS string to be applied to the tooltip. Default: NULL |
builder |
Tooltip builder to use for the tooltip. Can either one of the predefined tooltip ('table','basic') or a js expression returning a tooltip. |
A D3partitionR object
Enable/disable the trail of steps
set_trail(D3partitionR_object, visible = T)
set_trail(D3partitionR_object, visible = T)
D3partitionR_object |
The D3partitionR object to which the data should be appended |
visible |
boolean, should the trail be diplayed ? Default: TRUE |
A D3partitionR object
Strip a dataframe containing a step into separate columns
strip_path(data, path_col = "path", count_col = "count", value_cols = NULL, sep = "->")
strip_path(data, path_col = "path", count_col = "count", value_cols = NULL, sep = "->")
data |
A dataframe containing the path. |
path_col |
Name of the column containing the path. The path should be a string of the format "step 1 -> step 2 -> step 3" .Default: "path" |
count_col |
Name of the column containing the number of occurences of the path. Default: "count" |
value_cols |
Names of the other columns to keep. Default: NULL |
sep |
String used to separate the different steps. Default: "->" |
A data.table with the columns specified in count_col, value_cols and one column per step in the path
Build tooltip html function
tooltip_builder(type)
tooltip_builder(type)
type |
a tooltip type: 'basic' (i.e the variable value) or 'table'(i.e. a table with the variables names and value) |