-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.qmd
157 lines (124 loc) · 4.08 KB
/
README.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
---
format: markdown_github
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "85%"
)
```
[![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)
# 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 example}
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)
```
### UKfaculty
```{r}
#| label: fig-uk-faculty
#| fig-width: 14
#| fig-height: 14
# 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, .03, 4),
bg.col = "transparent",
vertex.label.show = .25,
vertex.label.range = c(10, 25),
edge.width.range = c(1, 4, 5),
vertex.label.fontfamily = "sans"
)
# Plot it!
ans
```
Starting version 0.2-0, we can use gradients!
```{r}
#| label: fig-uk-faculty-gradient
#| fig-width: 14
#| fig-height: 14
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}
#| label: fig-us-airports
# 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
)
```