diff --git a/.Rbuildignore b/.Rbuildignore index dd8c5a4..4cb7506 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -2,7 +2,7 @@ ^\.Rproj\.user$ ^playground$ ^LICENSE\.md$ -^README\.Rmd$ +^README\.qmd$ ^docs$ ^docker$ ^\.travis\.yml$ diff --git a/.gitignore b/.gitignore index 16e3c0f..1f97331 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ /doc/ /Meta/ + +/.quarto/ diff --git a/NEWS.md b/NEWS.md index 2240524..492616b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,33 +1,33 @@ # netplot 0.2-9999 -* Invalid arguments passed to `nplot()` now raise an error. +* Invalid arguments passed to `nplot()` now raise an error. -* Figures with legends are not drawn twice. +* Figures with legends are not drawn twice. -* Values supported by `nplot()` are now included in all methods (helps with - argument completion). +* Values supported by `nplot()` are now included in all methods (helps with + argument completion). # netplot 0.2-0 -* The arguments `skip.vertex`, `skip.edges`, and `skip.arrows` now work as - documented. - -* New function `nplot_legend()` helps adding legends to the figure. +* The arguments `skip.vertex`, `skip.edges`, and `skip.arrows` now work as + documented. -* New `nplot()` method for matrices. +* New function `nplot_legend()` helps to add legends to the figure. -* New feature: Gradients. +* New `nplot()` method for matrices. -* The argument `sample.edges` now works as expected. +* New feature: Gradients. -* `vertex.color`, `vertex.size`, and `vertex.nsides` now accepts formulas. +* The argument `sample.edges` now work as expected. -* `edge.width` now accepts formulas. +* `vertex.color`, `vertex.size`, and `vertex.nsides` now accepts formulas. -* New function: `locate_vertex()`. +* `edge.width` now accepts formulas. + +* New function: `locate_vertex()`. # netplot 0.1-0 -* First CRAN release. +* First CRAN release. diff --git a/R/netplot.R b/R/netplot.R index 28bc77f..3f0a831 100644 --- a/R/netplot.R +++ b/R/netplot.R @@ -122,18 +122,9 @@ nplot <- function( skip.arrows = skip.edges, add = FALSE, zero.margins = TRUE, - ..., edgelist ) { - # Checking if the passed arguments match the default class - res <- sapply( - ...names(), - match.arg, - choices = methods::formalArgs(nplot.default), - several.ok = FALSE - ) - UseMethod("nplot") } @@ -171,7 +162,6 @@ nplot.igraph <- function( skip.arrows = !igraph::is_directed(x), add = FALSE, zero.margins = TRUE, - ..., edgelist ) { @@ -179,14 +169,37 @@ nplot.igraph <- function( edge.width <- 1L nplot.default( - x = x, - edgelist = igraph::as_edgelist(x, names = FALSE), - layout = layout, - vertex.size = vertex.size, + x = x, + layout = layout, + vertex.size = vertex.size, + bg.col = bg.col, + vertex.nsides = vertex.nsides, + vertex.color = vertex.color, + vertex.size.range = vertex.size.range, + vertex.frame.color = vertex.frame.color, + vertex.rot = vertex.rot, + vertex.frame.prop = vertex.frame.prop, vertex.label = vertex.label, - edge.width = edge.width, + vertex.label.fontsize = vertex.label.fontsize, + vertex.label.color = vertex.label.color, + vertex.label.fontfamily = vertex.label.fontfamily, + vertex.label.fontface = vertex.label.fontface, + vertex.label.show = vertex.label.show, + vertex.label.range = vertex.label.range, + edge.width = edge.width, + edge.width.range = edge.width.range, + edge.arrow.size = edge.arrow.size, + edge.color = edge.color, + edge.curvature = edge.curvature, + edge.line.lty = edge.line.lty, + edge.line.breaks = edge.line.breaks, + sample.edges = sample.edges, + skip.vertex = skip.vertex, + skip.edges = skip.edges, skip.arrows = skip.arrows, - ... + add = add, + zero.margins = zero.margins, + edgelist = igraph::as_edgelist(x, names = FALSE) ) } @@ -226,18 +239,41 @@ nplot.network <- function( skip.arrows = !network::is.directed(x), add = FALSE, zero.margins = TRUE, - ..., edgelist ) { nplot.default( - x = x, - layout = layout, - vertex.size = vertex.size, + x = x, + layout = layout, + vertex.size = vertex.size, + bg.col = bg.col, + vertex.nsides = vertex.nsides, + vertex.color = vertex.color, + vertex.size.range = vertex.size.range, + vertex.frame.color = vertex.frame.color, + vertex.rot = vertex.rot, + vertex.frame.prop = vertex.frame.prop, vertex.label = vertex.label, - skip.arrows = skip.arrows, - ..., - edgelist = network::as.edgelist(x) + vertex.label.fontsize = vertex.label.fontsize, + vertex.label.color = vertex.label.color, + vertex.label.fontfamily = vertex.label.fontfamily, + vertex.label.fontface = vertex.label.fontface, + vertex.label.show = vertex.label.show, + vertex.label.range = vertex.label.range, + edge.width = edge.width, + edge.width.range = edge.width.range, + edge.arrow.size = edge.arrow.size, + edge.color = edge.color, + edge.curvature = edge.curvature, + edge.line.lty = edge.line.lty, + edge.line.breaks = edge.line.breaks, + sample.edges = sample.edges, + skip.vertex = skip.vertex, + skip.edges = skip.edges, + skip.arrows = skip.arrows, + add = add, + zero.margins = zero.margins, + edgelist = network::as.edgelist(x) ) } @@ -276,13 +312,40 @@ nplot.matrix <- function( skip.arrows = skip.edges, add = FALSE, zero.margins = TRUE, - ..., edgelist ) { nplot.igraph( x = igraph::graph_from_adjacency_matrix(x), - ..., + layout = layout, + vertex.size = vertex.size, + bg.col = bg.col, + vertex.nsides = vertex.nsides, + vertex.color = vertex.color, + vertex.size.range = vertex.size.range, + vertex.frame.color = vertex.frame.color, + vertex.rot = vertex.rot, + vertex.frame.prop = vertex.frame.prop, + vertex.label = vertex.label, + vertex.label.fontsize = vertex.label.fontsize, + vertex.label.color = vertex.label.color, + vertex.label.fontfamily = vertex.label.fontfamily, + vertex.label.fontface = vertex.label.fontface, + vertex.label.show = vertex.label.show, + vertex.label.range = vertex.label.range, + edge.width = edge.width, + edge.width.range = edge.width.range, + edge.arrow.size = edge.arrow.size, + edge.color = edge.color, + edge.curvature = edge.curvature, + edge.line.lty = edge.line.lty, + edge.line.breaks = edge.line.breaks, + sample.edges = sample.edges, + skip.vertex = skip.vertex, + skip.edges = skip.edges, + skip.arrows = skip.arrows, + add = add, + zero.margins = zero.margins, edgelist = NULL ) diff --git a/README.md b/README.md deleted file mode 100644 index 5106cfc..0000000 --- a/README.md +++ /dev/null @@ -1,180 +0,0 @@ - - - -[![CRAN -status](https://www.r-pkg.org/badges/version/netplot)](https://cran.r-project.org/package=netplot) -[![CRAN](https://cranlogs.r-pkg.org/badges/netplot)](https://cran.r-project.org/package=netplot) -[![R](https://github.com/USCCANA/netplot/actions/workflows/ci.yml/badge.svg)](https://github.com/USCCANA/netplot/actions/workflows/ci.yml) -[![Build -status](https://ci.appveyor.com/api/projects/status/3k2m3oq6o99qcs0r?svg=true)](https://ci.appveyor.com/project/gvegayon/netplot) -[![USC’s Department of Preventive -Medicine](https://raw.githubusercontent.com/USCbiostats/badges/master/tommy-uscprevmed-badge.svg)](https://preventivemedicine.usc.edu) - -# 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](https://CRAN.R-project.org) with: - -``` r -install.packages("netplot") -``` - -And the development version from [GitHub](https://github.com/) with: - -``` r -# install.packages("devtools") -devtools::install_github("USCCANA/netplot") -``` - -## Example - -This is a basic example which shows you how to solve a common problem: - -``` r -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 -``` - - - -``` r - -V(UKfaculty)$ss <- runif(vcount(UKfaculty)) -nplot(UKfaculty, layout = l) # ala netplot -``` - - - -``` r -sna::gplot(intergraph::asNetwork(UKfaculty), coord=l) -``` - - - -### UKfaculty - -``` r -# Random names -set.seed(1) -nam <- sample(babynames::babynames$name, vcount(UKfaculty)) - -ans <- nplot( - UKfaculty, - layout = l, - vertex.color = ~ Group, - vertex.nsides = ~ 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! - -``` r -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 - -``` r -# 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 - ) -``` - - diff --git a/README.Rmd b/README.qmd similarity index 90% rename from README.Rmd rename to README.qmd index 15f28aa..0957de8 100644 --- a/README.Rmd +++ b/README.qmd @@ -1,7 +1,5 @@ --- -output: - github_document: - html_preview: false +format: gfm --- @@ -18,6 +16,7 @@ knitr::opts_chunk$set( [![CRAN status](https://www.r-pkg.org/badges/version/netplot)](https://cran.r-project.org/package=netplot) [![CRAN](https://cranlogs.r-pkg.org/badges/netplot)](https://cran.r-project.org/package=netplot) +[![Downloads](https://cranlogs.r-pkg.org/badges/grand-total/rgexf)](https://cran.r-project.org/package=rgexf) [![R](https://github.com/USCCANA/netplot/actions/workflows/ci.yml/badge.svg)](https://github.com/USCCANA/netplot/actions/workflows/ci.yml) [![Build status](https://ci.appveyor.com/api/projects/status/3k2m3oq6o99qcs0r?svg=true)](https://ci.appveyor.com/project/gvegayon/netplot) [![USC's Department of Preventive Medicine](https://raw.githubusercontent.com/USCbiostats/badges/master/tommy-uscprevmed-badge.svg)](https://preventivemedicine.usc.edu) @@ -73,7 +72,10 @@ sna::gplot(intergraph::asNetwork(UKfaculty), coord=l) ### UKfaculty -```{r, fig.width=14, fig.height=14} +```{r} +#| label: fig-uk-faculty +#| fig-width: 14 +#| fig-height: 14 # Random names set.seed(1) nam <- sample(babynames::babynames$name, vcount(UKfaculty)) @@ -100,7 +102,10 @@ ans Starting version 0.2-0, we can use gradients! -```{r uk-faculty-gradient, fig.width=14, fig.height=14} +```{r} +#| label: fig-uk-faculty-gradient +#| fig-width: 14 +#| fig-height: 14 ans |> set_vertex_gpar( element = "core", @@ -111,7 +116,8 @@ ans |> ### USairports -```{r us-airports, cache=FALSE} +```{r} +#| label: fig-us-airports # Loading the data data(USairports, package="igraphdata") @@ -140,8 +146,9 @@ nplot( net, layout = layout, edge.width = ~ Passengers, - edge.color = - ~ ego(col = "white", alpha = 0) + alter(col = "yellow", alpha = .75), + 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), diff --git a/man/figures/README-unnamed-chunk-1-2.png b/man/figures/README-fig-uk-faculty-1.png similarity index 100% rename from man/figures/README-unnamed-chunk-1-2.png rename to man/figures/README-fig-uk-faculty-1.png diff --git a/man/figures/README-fig-uk-faculty-gradient-1.png b/man/figures/README-fig-uk-faculty-gradient-1.png new file mode 100644 index 0000000..68a9902 Binary files /dev/null and b/man/figures/README-fig-uk-faculty-gradient-1.png differ diff --git a/man/figures/README-us-airports-1.png b/man/figures/README-fig-us-airports-1.png similarity index 100% rename from man/figures/README-us-airports-1.png rename to man/figures/README-fig-us-airports-1.png diff --git a/man/figures/README-pressure-1.png b/man/figures/README-pressure-1.png deleted file mode 100644 index ab1c85f..0000000 Binary files a/man/figures/README-pressure-1.png and /dev/null differ diff --git a/man/figures/README-uk-faculty-gradient-1.png b/man/figures/README-uk-faculty-gradient-1.png deleted file mode 100644 index e041043..0000000 Binary files a/man/figures/README-uk-faculty-gradient-1.png and /dev/null differ diff --git a/man/figures/README-unnamed-chunk-1-1.png b/man/figures/README-unnamed-chunk-1-1.png deleted file mode 100644 index 04ce257..0000000 Binary files a/man/figures/README-unnamed-chunk-1-1.png and /dev/null differ diff --git a/man/figures/README-unnamed-chunk-2-1.png b/man/figures/README-unnamed-chunk-2-1.png deleted file mode 100644 index 3583dfe..0000000 Binary files a/man/figures/README-unnamed-chunk-2-1.png and /dev/null differ