Skip to content

Commit 6249613

Browse files
committed
Preparing for CRAN
1 parent f0e00f2 commit 6249613

10 files changed

+290
-83
lines changed

R/grob_vertex.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,10 @@ grob_vertex <- function(netenv, v) {
114114
)
115115
# If the users is drawing text
116116
if (length(netenv$vertex.label) && !is.na(netenv$vertex.label[v])) {
117+
117118
# Only if it is big enough
118-
if (netenv$label_threshold <= netenv$vertex.size[v])
119+
if (netenv$label_threshold <= netenv$vertex.size[v]) {
120+
119121
ans <- grid::addGrob(
120122
ans,
121123
grid::textGrob(
@@ -130,9 +132,12 @@ grob_vertex <- function(netenv, v) {
130132
),
131133
vjust = 0,
132134
default.units = "native",
133-
name = "label"
135+
name = "label",
136+
check.overlap = TRUE
134137
)
135138
)
139+
140+
}
136141
}
137142
ans
138143
}

R/netplot.R

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ edge_color_mixer <- function(i, j, vcols, p = .5, alpha = .15) {
3030
#' @param vertex.nsides Numeric vector of length `vcount(x)`. Number of sizes of
3131
#' the vertex. E.g. three is a triangle, and 100 approximates a circle.
3232
#' @param vertex.color Vector of length `vcount(x)`. Vertex HEX or built in colors.
33-
#' @param vertex.size.range Vector of length `vcount(x)` from 0 to 1.
34-
#' @param vertex.frame.color Vector of length `vcount(x)`. Border of vertex in HEX or built in colors.
33+
#' @param vertex.size.range Numeric vector of length 3. Relative size for the
34+
#' minimum and maximum of the plot, and curvature of the scale. The third number
35+
#' is used as `size^rel[3]`.
36+
#' @param vertex.frame.color Vector of length `vcount(x)`. Border of vertex in
37+
#' HEX or built in colors.
3538
#' @param vertex.frame.prop Vector of length `vcount(x)`. What proportion of the
3639
#' vertex does the frame occupy (values between 0 and 1).
3740
#' @param vertex.rot Vector of length `vcount(x)` in Radians. Passed to [npolygon],
@@ -45,19 +48,24 @@ edge_color_mixer <- function(i, j, vcols, p = .5, alpha = .15) {
4548
#' top ranking according to `vertex.size`.
4649
#' @param vertex.label.range Numeric vector of size 2 or 3. Relative scale of
4750
#' `vertex.label.fontsize` in points (see [grid::gpar]).
48-
#' @param edge.color A vector of length `ecount(x)`. In HEX or built in colors. Can be `NULL` in which case
51+
#' @param edge.color A vector of length `ecount(x)`. In HEX or built in colors.
52+
#' Can be `NULL` in which case
4953
#' the color is picked as a mixture between ego and alters' `vertex.color` values.
50-
#' @param edge.width Vector of length `ecount(x)` from 0 to 1. All edges will be the same size.
51-
#' @param edge.width.range Vector of length `ecount(x)` from 0 to 1. Adjusting width according to weight.
54+
#' @param edge.width Vector of length `ecount(x)` from 0 to 1. All edges will be
55+
#' the same size.
56+
#' @param edge.width.range Vector of length `ecount(x)` from 0 to 1. Adjusting
57+
#' width according to weight.
5258
#' @param edge.arrow.size Vector of length `ecount(x)` from 0 to 1.
5359
#' @param edge.curvature Numeric vector of length `ecount(x)`. Curvature of edges
5460
#' in terms of radians.
55-
#' @param edge.line.lty Vector of length `ecount(x)`. Line types in R (e.g.- 1 = Solid, 2 = Dashed, etc)
61+
#' @param edge.line.lty Vector of length `ecount(x)`. Line types in R
62+
#' (e.g.- 1 = Solid, 2 = Dashed, etc).
5663
#' @param edge.line.breaks Vector of length `ecount(x)`. Number of vertices to
5764
#' draw (approximate) the arc (edge).
58-
#' @param sample.edges Numeric scalar between 0 and 1. Proportion of edges to sample.
59-
#' @param skip.vertex,skip.edges,skip.arrows Logical scalar. When `TRUE` the object
60-
#' is not plotted.
65+
#' @param sample.edges Numeric scalar between 0 and 1. Proportion of edges to
66+
#' sample.
67+
#' @param skip.vertex,skip.edges,skip.arrows Logical scalar. When `TRUE` the
68+
#' object is not plotted.
6169
#' @param add Logical scalar.
6270
#' @param zero.margins Logical scalar.
6371
#' @importFrom igraph layout_with_fr degree vcount ecount
@@ -98,15 +106,15 @@ nplot <- function(
98106
bg.col = "transparent",
99107
vertex.nsides = 10,
100108
vertex.color = grDevices::hcl.colors(1),
101-
vertex.size.range = c(.01, .03),
109+
vertex.size.range = c(.01, .03, 4),
102110
vertex.frame.color = NULL,
103111
vertex.rot = 0,
104112
vertex.frame.prop = .2,
105113
vertex.label = NULL,
106114
vertex.label.fontsize = NULL,
107-
vertex.label.color = "black",
108-
vertex.label.fontfamily = "HersheySans",
109-
vertex.label.fontface = "bold",
115+
vertex.label.color = adjustcolor("black", alpha.f = .80),
116+
vertex.label.fontfamily = "sans",
117+
vertex.label.fontface = "plain",
110118
vertex.label.show = .3,
111119
vertex.label.range = c(5, 15),
112120
edge.width = 1,
@@ -138,15 +146,15 @@ nplot.igraph <- function(
138146
bg.col = "transparent",
139147
vertex.nsides = 10,
140148
vertex.color = grDevices::hcl.colors(1),
141-
vertex.size.range = c(.01, .03),
149+
vertex.size.range = c(.01, .03, 4),
142150
vertex.frame.color = NULL,
143151
vertex.rot = 0,
144152
vertex.frame.prop = .2,
145153
vertex.label = igraph::vertex_attr(x, "name"),
146154
vertex.label.fontsize = NULL,
147-
vertex.label.color = "black",
148-
vertex.label.fontfamily = "HersheySans",
149-
vertex.label.fontface = "bold",
155+
vertex.label.color = adjustcolor("black", alpha.f = .80),
156+
vertex.label.fontfamily = "sans",
157+
vertex.label.fontface = "plain",
150158
vertex.label.show = .3,
151159
vertex.label.range = c(5, 15),
152160
edge.width = igraph::edge_attr(x, "weight"),
@@ -215,15 +223,15 @@ nplot.network <- function(
215223
bg.col = "transparent",
216224
vertex.nsides = 10,
217225
vertex.color = grDevices::hcl.colors(1),
218-
vertex.size.range = c(.01, .03),
226+
vertex.size.range = c(.01, .03, 4),
219227
vertex.frame.color = NULL,
220228
vertex.rot = 0,
221229
vertex.frame.prop = .2,
222230
vertex.label = network::get.vertex.attribute(x, "vertex.names"),
223231
vertex.label.fontsize = NULL,
224-
vertex.label.color = "black",
225-
vertex.label.fontfamily = "HersheySans",
226-
vertex.label.fontface = "bold",
232+
vertex.label.color = adjustcolor("black", alpha.f = .80),
233+
vertex.label.fontfamily = "sans",
234+
vertex.label.fontface = "plain",
227235
vertex.label.show = .3,
228236
vertex.label.range = c(5, 15),
229237
edge.width = 1,
@@ -288,15 +296,15 @@ nplot.matrix <- function(
288296
bg.col = "transparent",
289297
vertex.nsides = 10,
290298
vertex.color = grDevices::hcl.colors(1),
291-
vertex.size.range = c(.01, .03),
299+
vertex.size.range = c(.01, .03, 4),
292300
vertex.frame.color = NULL,
293301
vertex.rot = 0,
294302
vertex.frame.prop = .2,
295303
vertex.label = NULL,
296304
vertex.label.fontsize = NULL,
297-
vertex.label.color = "black",
298-
vertex.label.fontfamily = "HersheySans",
299-
vertex.label.fontface = "bold",
305+
vertex.label.color = adjustcolor("black", alpha.f = .80),
306+
vertex.label.fontfamily = "sans",
307+
vertex.label.fontface = "plain",
300308
vertex.label.show = .3,
301309
vertex.label.range = c(5, 15),
302310
edge.width = 1,
@@ -399,7 +407,7 @@ netplot_theme <- (function() {
399407
dev_size <- function(...) {
400408

401409
if (length(grDevices::dev.list())) {
402-
410+
403411
grDevices::dev.size(...)
404412

405413
} else
@@ -435,15 +443,15 @@ nplot.default <- function(
435443
bg.col = "transparent",
436444
vertex.nsides = 10,
437445
vertex.color = grDevices::hcl.colors(1),
438-
vertex.size.range = c(.01, .03),
446+
vertex.size.range = c(.01, .03, 4),
439447
vertex.frame.color = NULL,
440448
vertex.rot = 0,
441449
vertex.frame.prop = .2,
442450
vertex.label = NULL,
443451
vertex.label.fontsize = NULL,
444-
vertex.label.color = "black",
445-
vertex.label.fontfamily = "HersheySans",
446-
vertex.label.fontface = "bold",
452+
vertex.label.color = adjustcolor("black", alpha.f = .80),
453+
vertex.label.fontfamily = "sans",
454+
vertex.label.fontface = "plain",
447455
vertex.label.show = .3,
448456
vertex.label.range = c(5, 15),
449457
edge.width = 1,
@@ -649,7 +657,7 @@ nplot.default <- function(
649657
if (!skip.vertex) {
650658
grob.vertex <- vector("list", netenv$N)
651659
for (v in 1:netenv$N)
652-
grob.vertex[[v]] <- grob_vertex(netenv, v)
660+
grob.vertex[[v]] <- grob_vertex(netenv, v)
653661
} else
654662
grob.vertex <- NULL
655663

R/netplot_base.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ nplot_base <- function(
3535
bg.col = "transparent",
3636
vertex.nsides = 10,
3737
vertex.color = grDevices::hcl.colors(1),
38-
vertex.size.range = c(.01, .03),
38+
vertex.size.range = c(.01, .03, 4),
3939
vertex.frame.color = grDevices::adjustcolor(vertex.color, red.f = .75, green.f = .75, blue.f = .75),
4040
vertex.rot = 0,
4141
vertex.frame.prop = .1,

README.md

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
2+
3+
<!-- README.md is generated from README.Rmd. Please edit that file -->
4+
5+
[![CRAN
6+
status](https://www.r-pkg.org/badges/version/netplot.png)](https://cran.r-project.org/package=netplot)
7+
[![CRAN](https://cranlogs.r-pkg.org/badges/netplot.png)](https://cran.r-project.org/package=netplot)
8+
[![Downloads](https://cranlogs.r-pkg.org/badges/grand-total/rgexf.png)](https://cran.r-project.org/package=rgexf)
9+
[![R](https://github.com/USCCANA/netplot/actions/workflows/ci.yml/badge.svg)](https://github.com/USCCANA/netplot/actions/workflows/ci.yml)
10+
[![Build
11+
status](https://ci.appveyor.com/api/projects/status/3k2m3oq6o99qcs0r?svg=true.png)](https://ci.appveyor.com/project/gvegayon/netplot)
12+
[![USC’s Department of Preventive
13+
Medicine](https://raw.githubusercontent.com/USCbiostats/badges/master/tommy-uscprevmed-badge.svg)](https://preventivemedicine.usc.edu)
14+
15+
# netplot
16+
17+
An alternative graph visualization tool that emphasizes aesthetics,
18+
providing default parameters that deliver out-of-the-box lovely
19+
visualizations.
20+
21+
Some features:
22+
23+
1. Auto-scaling of vertices using sizes relative to the plotting
24+
device.
25+
2. Embedded edge color mixer.
26+
3. True curved edges drawing.
27+
4. User-defined edge curvature.
28+
5. Nicer vertex frame color.
29+
6. Better use of space-filling the plotting device.
30+
31+
The package uses the `grid` plotting system (just like `ggplot2`).
32+
33+
## Installation
34+
35+
You can install the released version of netplot from
36+
[CRAN](https://CRAN.R-project.org) with:
37+
38+
``` r
39+
install.packages("netplot")
40+
```
41+
42+
And the development version from [GitHub](https://github.com/) with:
43+
44+
``` r
45+
# install.packages("devtools")
46+
devtools::install_github("USCCANA/netplot")
47+
```
48+
49+
## Example
50+
51+
This is a basic example which shows you how to solve a common problem:
52+
53+
``` r
54+
library(igraph)
55+
#>
56+
#> Attaching package: 'igraph'
57+
#> The following objects are masked from 'package:stats':
58+
#>
59+
#> decompose, spectrum
60+
#> The following object is masked from 'package:base':
61+
#>
62+
#> union
63+
library(netplot)
64+
#> Loading required package: grid
65+
#>
66+
#> Attaching package: 'netplot'
67+
#> The following object is masked from 'package:igraph':
68+
#>
69+
#> ego
70+
set.seed(1)
71+
data("UKfaculty", package = "igraphdata")
72+
l <- layout_with_fr(UKfaculty)
73+
#> This graph was created by an old(er) igraph version.
74+
#> Call upgrade_graph() on it to use with the current igraph version
75+
#> For now we convert it on the fly...
76+
77+
plot(UKfaculty, layout = l) # ala igraph
78+
```
79+
80+
<img src="man/figures/README-example-1.png" style="width:85.0%" />
81+
82+
``` r
83+
84+
V(UKfaculty)$ss <- runif(vcount(UKfaculty))
85+
nplot(UKfaculty, layout = l) # ala netplot
86+
```
87+
88+
<img src="man/figures/README-example-2.png" style="width:85.0%" />
89+
90+
``` r
91+
sna::gplot(intergraph::asNetwork(UKfaculty), coord=l)
92+
```
93+
94+
<img src="man/figures/README-example-3.png" style="width:85.0%" />
95+
96+
### UKfaculty
97+
98+
``` r
99+
# Random names
100+
set.seed(1)
101+
nam <- sample(babynames::babynames$name, vcount(UKfaculty))
102+
103+
ans <- nplot(
104+
UKfaculty,
105+
layout = l,
106+
vertex.color = ~ Group,
107+
vertex.nsides = ~ Group,
108+
vertex.label = nam,
109+
vertex.size.range = c(.01, .03, 4),
110+
bg.col = "transparent",
111+
vertex.label.show = .25,
112+
vertex.label.range = c(10, 25),
113+
edge.width.range = c(1, 4, 5),
114+
vertex.label.fontfamily = "sans"
115+
)
116+
117+
# Plot it!
118+
ans
119+
```
120+
121+
<img src="man/figures/README-fig-uk-faculty-1.png" id="fig-uk-faculty"
122+
style="width:85.0%" />
123+
124+
Starting version 0.2-0, we can use gradients!
125+
126+
``` r
127+
ans |>
128+
set_vertex_gpar(
129+
element = "core",
130+
fill = lapply(get_vertex_gpar(ans, "frame", "col")$col, \(i) {
131+
radialGradient(c("white", i), cx1=.8, cy1=.8, r1=0)
132+
}))
133+
```
134+
135+
<img src="man/figures/README-fig-uk-faculty-gradient-1.png"
136+
id="fig-uk-faculty-gradient" style="width:85.0%" />
137+
138+
### USairports
139+
140+
``` r
141+
# Loading the data
142+
data(USairports, package="igraphdata")
143+
144+
# Generating a layout naively
145+
layout <- V(USairports)$Position
146+
#> This graph was created by an old(er) igraph version.
147+
#> Call upgrade_graph() on it to use with the current igraph version
148+
#> For now we convert it on the fly...
149+
layout <- do.call(rbind, lapply(layout, function(x) strsplit(x, " ")[[1]]))
150+
layout[] <- stringr::str_remove(layout, "^[a-zA-Z]+")
151+
layout <- matrix(as.numeric(layout[]), ncol=2)
152+
153+
# Some missingness
154+
layout[which(!complete.cases(layout)), ] <- apply(layout, 2, mean, na.rm=TRUE)
155+
156+
# Have to rotate it (it doesn't matter the origin)
157+
layout <- netplot:::rotate(layout, c(0,0), pi/2)
158+
159+
# Simplifying the network
160+
net <- simplify(USairports, edge.attr.comb = list(
161+
weight = "sum",
162+
name = "concat",
163+
Passengers = "sum",
164+
"ignore"
165+
))
166+
167+
# Pretty graph
168+
nplot(
169+
net,
170+
layout = layout,
171+
edge.width = ~ Passengers,
172+
edge.color = ~
173+
ego(col = "white", alpha = 0) +
174+
alter(col = "yellow", alpha = .75),
175+
skip.vertex = TRUE,
176+
skip.arrows = TRUE,
177+
edge.width.range = c(.75, 4, 4),
178+
bg.col = "black",
179+
edge.line.breaks = 10
180+
)
181+
```
182+
183+
<img src="man/figures/README-fig-us-airports-1.png" id="fig-us-airports"
184+
style="width:85.0%" />

0 commit comments

Comments
 (0)