Skip to content

Commit

Permalink
update: sankeyPlot()
Browse files Browse the repository at this point in the history
- fix: iterative node indexing
- fix: data.frame method now enforces conversion to `data.table`
- add: method for lists of `data.frames` that will allow sankey generation stepwise so that identical node values can be considered independent if they originate from different data.tables in the list.

contrast with simply using `rbind()` which puts two sets of data.tables together into the same sankey generation.
  • Loading branch information
jiajic committed Oct 23, 2023
1 parent 1b282e2 commit d20be68
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 11 deletions.
70 changes: 64 additions & 6 deletions R/plot_sankey.R
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,14 @@ sankey_relation_pair = function(g, gsp, rel_idx, node_idx_start = 0) {
#' @title Create a sankey plot
#' @name sankeyPlot
#' @description
#' Create a sankey plot from a giotto object. Pulls from information in the
#' metadata. Simple 1 to 1 sankeys can be generated from a single spatial unit
#' Create a sankey plot. Pulls from information metadata if giotto object is
#' provided. Simple 1 to 1 sankeys can be generated from a single spatial unit
#' and feature type using the `spat_unit`, `feat_type`, `meta_type`, `cols`,
#' and (optionally) `idx` params. More complex and cross spatial unit/feature
#' type sankeys can be set up using the `sankey_plan` param which accepts a
#' `giottoSankeyPlan` object.
#' `giottoSankeyPlan` object.\cr
#' Also possible to directly use data.frames or lists of data.frames and
#' giottoPolygon objects. See usage section and examples.
#' @param x data source (gobject, data.frame-like object with relations
#' between the first two cols provided, or giottoPolygon)
#' @param y giottoSankeyPlan object or character vector referring to source and
Expand All @@ -459,9 +461,24 @@ sankey_relation_pair = function(g, gsp, rel_idx, node_idx_start = 0) {
#' @inheritDotParams networkD3::sankeyNetwork -Links -Nodes -Source -Target -Value -NodeID
#' @examples
#' \dontrun{
#' x = data.table::data.table(col1 = c('a', 'a', 'b'),
#' col2 = c('x', 'y', 'y'))
#' x = data.frame(
#' col1 = c('a', 'a', 'b'),
#' col2 = c('1', '2', '2')
#' )
#' sankeyPlot(x)
#'
#' y = data.frame(
#' col1 = '1',
#' col2 = c('A', 'B', 'C')
#' )
#'
#' # combine data.frames of relations
#' # rbind: note that node "1" is mapped the same for x and y
#' sankeyPlot(rbind(x,y), fontSize = 20)
#'
#' # list: note that node "1" is now considered a different node between x and y
#' sankeyPlot(list(x,y), fontSize = 20)
#'
#' g = GiottoData::loadGiottoMini("vizgen")
#' # with giottoSankeyPlan
#' leiden = sankeySet(spat_unit = 'aggregate',
Expand Down Expand Up @@ -516,7 +533,7 @@ setMethod(
nodes = c(nodes, rel_data$nodes)

# update start index
node_idx_start = links_dt[, max(target)]
node_idx_start = links_dt[, max(source, target)] + 1

Check warning

Code scanning / lintr

no visible binding for global variable 'target' Warning

no visible binding for global variable 'target'

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
}

# create nodes table
Expand Down Expand Up @@ -611,6 +628,7 @@ setMethod(
setMethod('sankeyPlot', signature(x = 'data.frame', y = 'missing'), function(x, ...) {
GiottoUtils::package_check("networkD3")

x = data.table::as.data.table(x)

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
res = sankey_compare(data_dt = x)
links_dt = res$links

Expand All @@ -629,6 +647,46 @@ setMethod('sankeyPlot', signature(x = 'data.frame', y = 'missing'), function(x,

})

#' @rdname sankeyPlot
#' @export
setMethod('sankeyPlot', signature(x = 'list', y = 'missing'), function(x, ...) {

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.
checkmate::assert_list(x, types = 'data.frame')

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.
if (length(x) == 0L) stop('input is empty list')

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.

# iterate through sankey relations in the list
node_idx_start = 0

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
links_dt = data.table::data.table()

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
nodes = c()

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.

for (dt_i in seq_along(x)) {

rel_data = sankey_compare(

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
data_dt = data.table::as.data.table(x[[dt_i]]),
idx_start = node_idx_start
)

# append data
links_dt = rbind(links_dt, rel_data$links)

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
nodes = c(nodes, rel_data$nodes)

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.

# update start index
node_idx_start = links_dt[, max(source, target)] + 1

Check warning

Code scanning / lintr

no visible binding for global variable 'target' Warning

no visible binding for global variable 'target'

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.
}

# create nodes table
nodes_dt = data.table::data.table(name = nodes)

Check notice

Code scanning / lintr

Use <-, not =, for assignment. Note

Use <-, not =, for assignment.

sankey_networkd3(
Links = links_dt,
Nodes = nodes_dt,
Source = 'source',

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.
Target = 'target',

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.
Value = 'value',

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.
NodeID = 'name',

Check notice

Code scanning / lintr

Only use double-quotes. Note

Only use double-quotes.
...
)
})


#' @rdname sankeyPlot
#' @export
Expand Down
30 changes: 25 additions & 5 deletions man/sankeyPlot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d20be68

Please sign in to comment.