Giter Club home page Giter Club logo

netplot's Introduction

CRAN status CRAN R Build status USC’s Department of Preventive Medicine

netplot

An alternative graph visualization tool that emphasizes aesthetics, providing default parameters that deliver out-of-the-box lovely visualizations.

Some features:

  1. Auto-scaling of vertices using sizes relative to the plotting device.
  2. Embedded edge color mixer.
  3. True curved edges drawing.
  4. User-defined edge curvature.
  5. Nicer vertex frame color.
  6. Better use of space-filling the plotting device.

The package uses the grid plotting system (just like ggplot2).

Installation

You can install the released version of netplot from CRAN with:

install.packages("netplot")

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("USCCANA/netplot")

Example

This is a basic example which shows you how to solve a common problem:

library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
library(netplot)
#> Loading required package: grid
#> 
#> Attaching package: 'netplot'
#> The following object is masked from 'package:igraph':
#> 
#>     ego
set.seed(1)
data("UKfaculty", package = "igraphdata")
l <- layout_with_fr(UKfaculty)
#> This graph was created by an old(er) igraph version.
#>   Call upgrade_graph() on it to use with the current igraph version
#>   For now we convert it on the fly...

plot(UKfaculty, layout = l) # ala igraph

V(UKfaculty)$ss <- runif(vcount(UKfaculty))
nplot(UKfaculty, layout = l) # ala netplot

sna::gplot(intergraph::asNetwork(UKfaculty), coord=l)

UKfaculty

# Random names
set.seed(1)
nam <- sample(babynames::babynames$name, vcount(UKfaculty))

ans <- nplot(
  UKfaculty,
  layout                = l,
  vertex.color          = ~ Group,
  vertex.label          = nam,
  vertex.size.range     = c(.01, .04, 4),
  vertex.label.col      =  "black",
  vertex.label.fontface = "bold",
  bg.col                = "transparent",
  vertex.label.show     = .5,
  vertex.label.range    = c(10, 25),
  edge.width.range      = c(1, 4, 5)
  )


# Plot it!
ans

Starting version 0.2-0, we can use gradients!

ans |>
  set_vertex_gpar(
    element = "core",
    fill = lapply(get_vertex_gpar(ans, "frame", "col")$col, \(i) {
      radialGradient(c("white", i), cx1=.8, cy1=.8, r1=0)
      }))

USairports

# Loading the data
data(USairports, package="igraphdata")

# Generating a layout naively
layout   <- V(USairports)$Position
#> This graph was created by an old(er) igraph version.
#>   Call upgrade_graph() on it to use with the current igraph version
#>   For now we convert it on the fly...
layout   <- do.call(rbind, lapply(layout, function(x) strsplit(x, " ")[[1]]))
layout[] <- stringr::str_remove(layout, "^[a-zA-Z]+")
layout   <- matrix(as.numeric(layout[]), ncol=2)

# Some missingness
layout[which(!complete.cases(layout)), ] <- apply(layout, 2, mean, na.rm=TRUE)

# Have to rotate it (it doesn't matter the origin)
layout <- netplot:::rotate(layout, c(0,0), pi/2)

# Simplifying the network
net <- simplify(USairports, edge.attr.comb = list(
  weight = "sum",
  name   = "concat",
  Passengers = "sum",
  "ignore"
))

# Pretty graph
nplot(
  net,
  layout            = layout,
  edge.width        = ~ Passengers,
  edge.color        = 
    ~ ego(col = "white", alpha = 0) + alter(col = "yellow", alpha = .75),
  skip.vertex       = TRUE,
  skip.arrows       = TRUE,
  edge.width.range  = c(.75, 4, 4), 
  bg.col            = "black",
  edge.line.breaks  = 10
  )

netplot's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

netplot's Issues

Empty graph fails to plot

library(ergm)
library(netplot)

n <- 50
n_egos <- 10
net <- as.network(matrix(0, ncol = n, nrow = n), directed = TRUE)
nplot(net)
# Error in quantile.default(netenv$vertex.size, 1 - netenv$vertex.label.show[1]) : 
#  missing values and NaN's not allowed if 'na.rm' is FALSE

remove arrows from edges for undirected graphs

Hi,

Thank you so much for building an incredible network visualization tool! I'm curious if I am missing something. I have an undirected network that I'd like to visualize without edge arrows. I have set skip.arrows=T, but the network plot still shows edge arrows. Is there a straightforward way to remove the arrows that I am missing?

I am sorry if I am overlooking something obvious.

Thank you so much.

Issue in making 'vertex.nsides' be determined by function

Hey @gvegayon! As discussed, here is the error we bumped into.

library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
library(netplot)
#> Loading required package: grid
#> 
#> Attaching package: 'netplot'
#> The following object is masked from 'package:igraph':
#> 
#>     ego

set.seed(123)
g <- erdos.renyi.game(50, 0.2)

teams <- c("Utah", "Wyoming", "Idaho", "Nevada")
V(g)$team <- sample(teams, vcount(g), replace=TRUE) 

# Map teams to sides
team_sides <- c(Utah=4, Wyoming=6, Idaho=3, Nevada=5)
sides <- team_sides[V(g)$team]

# Map teams to colors
team_colors <- c(Utah="red", Wyoming="darkgreen", Idaho="blue", Nevada="purple")

# Get color for each vertex 
vertex_colors <- team_colors[V(g)$team]

# Plot
# Create empty plot
plot(0,type="n", axes=FALSE) 

nplot(g,
      vertex.color = ~team,  
      vertex.nsides= ~team)
#> Error in eval(netenv$vertex.nsides, envir = data): invalid 'envir' argument of type 'closure'

Created on 2023-10-04 with reprex v2.0.2

Add option vertex.shape

Should be the following:

  • circle (15)
  • triangle (3 + rotation)
  • diamond (4)
  • square (4 + rotation)
  • line (1)
  • pentagon (5)
  • hexagon (6)

This will work as a shortcut to vertex.nbreaks.

Using formulae to set gpar attributes

Right now we do have a function to parse formulas for edge colors, but ideally, we could have a more comprehensive/flexible system to parse formulas and return various types of outcomes.

About the input

  • Formulas for mixing ego/alter attributes.
  • Formulas for using either ego or alter attributes.
  • Formulas using edge attributes.

Mixing these three should be relatively straightforward. Since at the edge level, we will only find one attribute of each.

The problem is when, for example, we would like to generate a statistic at the ego-network level.

About the output

  • Formulas should return numbers/factors:
    • Integers (factors)
    • Fractions
    • Strings
    • Etc.

This could follow a goal similar to what the scales R package does, this is, return palettes/functions for plots.

enable \n in vertex labels

Fantastic Package! Vertex labels with new lines are enabled in igraph plotting but not in this package. It would increase my use greatly if I could include new lines in my vertex labels like "Com 12\nMemmory T cells". Maybe a bug, maybe a enhancement... not sure.

Multiple edges

Correct the curvature level when more than 1 edge exists. For this I may need to use c++ so I can easily (fast) find multiple ties. The function which_multiple from igraph returns a count, but nothing about which.

Speedup the thing

One option for this is to perform some of the calculations using C++. The current version tries to vectorize as much as it can, but when dealing with, for example, 10 line breaks per edge calculations can become cumbersome.

Using other than scalars for `gpar` does not work

The way I wrote set_gpar, it will replicate arguments and turn them into a list. Unfortunately, when passing an argument that IS A LIST, like grid::radialGradient(), does not work. This should be an easy fix.This is the code that fails:

p <- nplot(...)
p <- set_vertex_gpar(p, "core", fill = radialGradient(c("white", "black"), cx1=.8, cy1=.8, r1=0))
print(p) # fails

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.