Permalink
Browse files

Fixing viz attributes from igraph to gexf and vice versa

  • Loading branch information...
gvegayon committed Nov 27, 2017
1 parent d0ac072 commit 39d24409c7c641a1e62bdf518ff8ca46ea6172b5
Showing with 78 additions and 79 deletions.
  1. +1 −0 .Rbuildignore
  2. +2 −2 ChangeLog
  3. 0 {inst → }/LICENSE
  4. +5 −1 NEWS.md
  5. +28 −48 R/igraph.r
  6. +4 −2 R/read.gexf.r
  7. +1 −1 demo/gexfigraph.R
  8. +33 −22 inst/NEWS
  9. +2 −1 man/igraph.to.gexf.Rd
  10. +2 −2 tests/testthat/test-examples.r
@@ -21,3 +21,4 @@ demo/gexfplot\.R
^dependencies$
^makefile$
^LICENSE\.md$
^tests/rundemos\.R$
@@ -1,8 +1,8 @@
2017-11-27 George G. Vega Yon <g.vegayon@gmail.com>
* R/bugs.r: Function to map colors of a single node.

* R/igraph.r, R/read.gexf.r: Mapping positions, size and colors. Still a
bit buggy.
* R/igraph.r, R/read.gexf.r: Mapping positions, size and colors, ~~Still a
bit buggy.~~ and positions.


2017-11-13 George G. Vega Yon <g.vegayon@gmail.com>
File renamed without changes.
@@ -1,4 +1,4 @@
# Changes in rgexf version 0.16.0 (2017-11-06)
# Changes in rgexf version 0.16.0 (2017-11-27)

## New features and changes

@@ -20,6 +20,10 @@
* `gexf` has a new argument, `rescale.node.size`. When set to `TRUE`, the
`size` vector in `nodesVizAtt` is rescaled such that when calling the plot
method the largest node spans roughly 5% of the plot.

* `read.gexf` now parses viz attributes (color, position and size).

* `igraph.to.gexf` and vice versa now pass viz attributes and layout.

## New functions

@@ -27,7 +27,8 @@
#' igraph1 <- gexf.to.igraph(gexf1)
#' gexf2 <- igraph.to.gexf(igraph1)
#'
#' # Now, let's do it with a layout!
#' # Now, let's do it with a layout! (although we can just use
#' # the one that comes with lesmiserables :))
#' pos <- igraph::layout_nicely(igraph1)
#' plot(
#' igraph.to.gexf(igraph1, nodesVizAtt = list(position=cbind(pos, 0))),
@@ -42,56 +43,32 @@ igraph.to.gexf <- function(igraph.obj, ...) {

# Retrive elements from igraph object
gdata <- igraph::get.data.frame(g, what="both")
tmpedges <- gdata$edges
tmpnodes <- gdata$vertices
edges <- gdata$edges
nodes <- cbind(id = 1L:igraph::vcount(g), gdata$vertices)

# If nodes have no name
if (!("name" %in% colnames(tmpnodes)))
tmpnodes <- data.frame(tmpnodes, name=1:nrow(tmpnodes))

# Nodes and edges list
# these steps are dangerous as nodes that are not connected are dropped, creating a mismatched number of rows
# nodes <- edge.list(tmpedges[,c(1,2)])
# edges <- nodes$edges
# nodes <- nodes$nodes

nodes <- tmpnodes
# change name to label
names(nodes) <- 'label'
# add a column to nodes to hold the IDs
nodes$id <- as.integer(as.factor(nodes$label))
# put them in the right order
nodes <- nodes[, c('id', 'label')]

## replace from and to in edges with their IDs
# first create a named vector of ids
nodesID <- nodes$id
names(nodesID) <- gdata$vertices$name

edges <- tmpedges
# now in the edges create the id columns
edges$source <- nodesID[edges$from]
edges$target <- nodesID[edges$to]
# keep just source and target
edges <- edges[, c('source', 'target')]
namecol <- which(colnames(nodes) == "name")
if (!length(namecol))
nodes <- data.frame(nodes, label=nodes$id)
else
nodes$label <- nodes$name

# Building nodes
if (length(tmpnodes)) {
nodes <-merge(tmpnodes,nodes, by.x="name", by.y="label")
# nodes <- nodes[,!(colnames(nodes) %in% c("label"))]
}

edges$source <- nodes$id[match(edges$from, nodes$label)]
edges$target <- nodes$id[match(edges$to, nodes$label)]
edges <- edges[, c('source', 'target')]

# Nodes Attributes
x <- igraph::list.vertex.attributes(g)
x <- x[!(x %in% c("label","color","size"))]
x <- x[!(x %in% c("label","color","size", "name"))]
if (length(x))
dots$nodesAtt <- subset(nodes, select=x)

# Edges Attributes
x <- igraph::list.edge.attributes(g)
x <- x[!(x %in% c("weight","color","edgesLabel","width"))]
if (length(x))
dots$edgesAtt <- subset(tmpedges, select=x)
dots$edgesAtt <- subset(edges, select=x)

# Edges Weights
if (!length(dots$edgesWeight) && length(igraph::E(g)$weight))
@@ -101,14 +78,14 @@ igraph.to.gexf <- function(igraph.obj, ...) {

# Nodes Viz att
if (!length(dots$nodesVizAtt$color)) {
if (length(tmpnodes$color)) {
dots$nodesVizAtt$color <- tmpnodes$color
if (length(nodes$color)) {
dots$nodesVizAtt$color <- nodes$color
} else
dots$nodesVizAtt$color <- NULL
}

if (!length(dots$nodesVizAtt$size) && length(tmpnodes$size))
dots$nodesVizAtt$size <- tmpnodes$size
if (!length(dots$nodesVizAtt$size) && length(nodes$size))
dots$nodesVizAtt$size <- nodes$size

positions <- igraph::graph_attr(g, "layout")

@@ -119,8 +96,8 @@ igraph.to.gexf <- function(igraph.obj, ...) {

# Edges Viz att
if (!length(dots$edgesVizAtt$color)) {
if (length(tmpedges$color)) {
dots$edgesVizAtt$color <- tmpedges$color
if (length(edges$color)) {
dots$edgesVizAtt$color <- edges$color
} else
dots$edgexVizAtt$color <- NULL
}
@@ -137,8 +114,8 @@ igraph.to.gexf <- function(igraph.obj, ...) {
# Building graph
do.call(gexf, c(
list(
nodes = nodes[,c("id","name")],
edges = edges
nodes = nodes[, c("id","name")],
edges = edges[, c("source", "target")]
), dots)
)

@@ -171,7 +148,10 @@ gexf.to.igraph <- function(gexf.obj) {
igraph::V(g2)$size <- x$value

if (length(x <- g$nodesVizAtt$position))
g2 <- igraph::set_graph_attr(g2, "layout", unname(x))
g2 <- igraph::set_graph_attr(
g2, "layout",
unname(as.matrix(x))
)

# Nodes atts
if (length(x <- g$nodes[, !(colnames(g$nodes) %in% c("id", "label"))]))
@@ -161,11 +161,13 @@ read.gexf <- function(x) {

class(graph) <- "gexf"

order <- order(as.integer(graph$nodes$id))

build.and.validate.gexf(
nodes = graph$nodes,
nodes = graph$nodes[order, , drop=FALSE],
edges = graph$edges,
atts.definitions = graph$atts.definitions,
nodesVizAtt = nodesVizAtt,
nodesVizAtt = lapply(nodesVizAtt, "[", i=order, j=, drop=FALSE),
edgesVizAtt = edgesVizAtt,
graph = graph$graph
)
@@ -1,3 +1,3 @@
gexf1 <- read.gexf(system.file("gexf-graphs/lesmiserables.gexf", package="rgexf"))
igraph1 <- gexf.to.igraph(gexf1)
gexf2 <- igraph.to.gexf(igraph1)
gexf2 <- igraph.to.gexf(igraph1)
@@ -1,32 +1,34 @@


CHANGES IN RGEXF VERSION 0.16.0 (2017-11-06)
CHANGES IN RGEXF VERSION 0.16.0 (2017-11-27)


New features and changes

- Modernization of the project (roxygen, new CRAN standards, etc.)

- Updated emails.

- Remove broken links.

- Function write.gexf has a new argument for specifying the GEXF
version for now it only changes the header.

- plot.gexf method now uses gexf-js instead of sigma.js.

- Now igraph.to.gexf passes arguments to gexf. Before it was only
passing position. This way users have more flexibility specifying
attributes.
passing position. This way users have more flexibility
specifying attributes.

- gexf's nodesVizAtt now has defaults for color, size and position.
This is a requirement of gexf-js.

- gexf's nodesVizAtt color and size now support passing a default for
all the nodes. Also, color can be specified as a character scalar
(name of the) color, or as an integer (number in colors()).

- gexf has a new argument, rescale.node.size. When set to TRUE, the
size vector in nodesVizAtt is rescaled such that when calling the
plot method the largest node spans roughly 5% of the plot.

- read.gexf now parses viz attributes (color, position and size).

- igraph.to.gexf and vice versa now pass viz attributes and layout.


New functions

@@ -36,6 +38,15 @@ New functions
vers 0.17.0


Misc changes

- Modernization of the project (roxygen, new CRAN standards, etc.)

- Updated emails.

- Remove broken links.



CHANGES IN RGEXF VERSION 0.15.3 (2015-03-24)

@@ -70,9 +81,9 @@ Bug fixes

Development

- Option -keepFactors- default is now in -FALSE- (used to be in
-TRUE-). When set to -TRUE- and there are factors, a warning message
will appear (reported in issue 18, thanks Tim Smith!).
- Option -keepFactors- default is now in -FALSE- (used to be
in -TRUE-). When set to -TRUE- and there are factors, a warning
message will appear (reported in issue 18, thanks Tim Smith!).



@@ -81,8 +92,8 @@ CHANGES IN RGEXF VERSION 0.13.11 (2013-11-27)

Bug fixes

- Included class checks in -gexf- class functions (thanks to Samuel
Finegold).
- Included class checks in -gexf- class functions (thanks to
Samuel Finegold).

- write.gexf does not fails when dynamics different from double are
passed (thank you Samuel Finegold!).
@@ -120,8 +131,8 @@ CHANGES IN RGEXF VERSION 0.13.05 (2013-05-09)

Bug fixes

- sprintf error when using other formats rather than double (issue
10).
- sprintf error when using other formats rather than double
(issue 10).

- in .addNodesEdges, add support to case “!attributes &&
vizattributes” (issue 9).
@@ -163,8 +174,8 @@ Bug fixes
Development

- .addNodesEdges rewritten now works faster in most of CPUs (some of
them with very high speedups) (Thanks to Duncan Temple Lang, RXML
author)
them with very high speedups) (Thanks to Duncan Temple Lang,
RXML author)

- Several code routines have been extracted from “bigger functions”
and written as functions themselves.
@@ -180,8 +191,8 @@ New features and changes
rm.gexf.node and rm.gexf.edge allow to build and manipulate gexf
objects from scratch.

- New function read.gexf allows to import gexf files as gexf class
objects.
- New function read.gexf allows to import gexf files as gexf
class objects.

- gexf function now it is called write.gexf.

@@ -192,8 +203,8 @@ New features and changes

- Real-life datasets have been includded.

- New function edge.list builds a dataframe of nodes from an edge
list.
- New function edge.list builds a dataframe of nodes from an
edge list.

- New methods for gexf objects: print.gexf and summary.gexf.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -1,3 +1,3 @@
# context("Examples")
context("Examples")

# test_examples()
test_examples()

0 comments on commit 39d2440

Please sign in to comment.