knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "85%" )
An alternative graph visualization tool that emphasizes aesthetics, providing default parameters that deliver out-of-the-box lovely visualizations.
Some features:
The package uses the grid
plotting system (just like ggplot2
).
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")
This is a basic example which shows you how to solve a common problem:
library(igraph) library(netplot) set.seed(1) data("UKfaculty", package = "igraphdata") l <- layout_with_fr(UKfaculty) 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)
# 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) }))
# Loading the data data(USairports, package="igraphdata") # Generating a layout naively layout <- V(USairports)$Position 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 )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.