Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directed metrics #22

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: sigmaNet
Title: Render Graphs Using 'Sigma.js'
Version: 1.1.0
Version: 1.2.0
Authors@R: person("Ian", "Kloo", email = "[email protected]", role = c("aut", "cre"))
Maintainer: Ian Kloo <[email protected]>
URL: https://github.com/iankloo/sigmaNet
Expand Down
28 changes: 19 additions & 9 deletions R/sigmaAttrs.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ addNodeColors <- function(sigmaObj, oneColor = NULL, colorAttr = NULL, colorPal

graphOut <- list(nodes, edges, directed)
names(graphOut) <- c('nodes','edges', 'directed')

sigmaObj$x$data <- jsonlite::toJSON(graphOut, pretty = TRUE)
return(sigmaObj)
}
Expand All @@ -76,6 +76,7 @@ addNodeColors <- function(sigmaObj, oneColor = NULL, colorAttr = NULL, colorPal
#' @param minSize The minimum node size on the graph (for scaling)
#' @param maxSize The maximum node size on the graph (for scaling)
#' @param sizeMetric The metric to use when sizing the nodes. Options are: degree, closeness, betweenness, pageRank, or eigenCentrality.
#' @param mode The mode to be used when calculating degree or closeness for directed networks. Options are: all, in, out and total. The default behaviour will match that of igraph.
#' @param sizeVector An optional vector with the sizes for each node (overrides sizeMetric and min/maxSize)
#' @param oneSize A single size to use for all nodes
#'
Expand Down Expand Up @@ -108,11 +109,11 @@ addNodeColors <- function(sigmaObj, oneColor = NULL, colorAttr = NULL, colorPal
#' sig
#'
#' @export
addNodeSize <- function(sigmaObj, minSize = 1, maxSize = 3, sizeMetric = 'degree', sizeVector = NULL, oneSize = NULL){
addNodeSize <- function(sigmaObj, minSize = 1, maxSize = 3, sizeMetric = 'degree', mode = NULL, sizeVector = NULL, oneSize = NULL){
edges <- jsonlite::fromJSON(sigmaObj$x$data)$edges
nodes <- jsonlite::fromJSON(sigmaObj$x$data)$nodes
directed <- jsonlite::fromJSON(sigmaObj$x$data)$directed

if(!is.null(oneSize)){
nodes$size <- oneSize
sigmaObj$x$options$minNodeSize <- oneSize
Expand All @@ -125,10 +126,19 @@ addNodeSize <- function(sigmaObj, minSize = 1, maxSize = 3, sizeMetric = 'degree
tmp_graph <- igraph::graph_from_data_frame(d = edges,
directed = directed,
vertices = nodes$id)
# set default mode to match those of igraph
if(is.null(mode)){
if(sizeMetric == 'degree'){
mode = "all"
} else if(sizeMetric == 'closeness'){
mode = "out"
}
}

if(sizeMetric == 'degree'){
nodes$size <- igraph::degree(tmp_graph)
nodes$size <- igraph::degree(tmp_graph, mode = mode)
} else if(sizeMetric == 'closeness'){
nodes$size <- igraph::closeness(tmp_graph)
nodes$size <- igraph::closeness(tmp_graph, mode = mode)
} else if(sizeMetric == 'betweenness'){
nodes$size <- igraph::betweenness(tmp_graph, directed = directed)
} else if(sizeMetric == 'pageRank'){
Expand All @@ -144,7 +154,7 @@ addNodeSize <- function(sigmaObj, minSize = 1, maxSize = 3, sizeMetric = 'degree

graphOut <- list(nodes, edges, directed)
names(graphOut) <- c('nodes','edges', 'directed')

sigmaObj$x$data <- jsonlite::toJSON(graphOut, pretty = TRUE)
return(sigmaObj)

Expand Down Expand Up @@ -242,7 +252,7 @@ addEdgeSize <- function(sigmaObj, sizeAttr = NULL, minSize = 1, maxSize = 5, one

graphOut <- list(nodes, edges, directed)
names(graphOut) <- c('nodes','edges', 'directed')

sigmaObj$x$data <- jsonlite::toJSON(graphOut, pretty = TRUE)
return(sigmaObj)
}
Expand Down Expand Up @@ -299,15 +309,15 @@ addEdgeColors <- function(sigmaObj, oneColor = NULL, colorAttr = NULL, colorPal

graphOut <- list(nodes, edges, directed)
names(graphOut) <- c('nodes','edges', 'directed')

sigmaObj$x$data <- jsonlite::toJSON(graphOut, pretty = TRUE)
return(sigmaObj)
}
#' Add edge arrows to a 'sigmaNet' object.
#'
#' Add edge arrows to a directed graph. Due to complexities of writing custom renderers in webGL, we are stuck with one
#' arrow:edge size ratio. In other words, you can only affect the edge arrow size by making the edges bigger.
#'
#'
#' This is only applicable for directed graphs - if you run this on an undirected graph, you'll get an error.
#'
#' @param sigmaObj A 'sigmaNet' object - created using the 'sigmaFromIgraph' function
Expand Down
2 changes: 1 addition & 1 deletion docs/CODE_OF_CONDUCT.html

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

2 changes: 1 addition & 1 deletion docs/CONTRIBUTING.html

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

2 changes: 1 addition & 1 deletion docs/ISSUE_TEMPLATE.html

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

2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

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

2 changes: 1 addition & 1 deletion docs/PULL_REQUEST_TEMPLATE.html

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

Loading