View source: R/plot_diffnet2.r
diffusionMap | R Documentation |
Using bi-dimensional kernel smoothers, creates a heatmap based on a graph layout
and colored accordingly to x
. This visualization technique is intended
to be used with large graphs.
diffusionMap(graph, ...)
diffmap(graph, ...)
## Default S3 method:
diffusionMap(
graph,
x,
x.adj = round_to_seq,
layout = NULL,
jitter.args = list(),
kde2d.args = list(n = 100),
sharp.criter = function(x, w) {
wvar(x, w) > (max(x, na.rm = TRUE) - min(x, na.rm
= TRUE))^2/12
},
...
)
## S3 method for class 'diffnet'
diffusionMap(graph, slice = nslices(graph), ...)
## S3 method for class 'diffnet_diffmap'
image(x, ...)
## S3 method for class 'diffnet_diffmap'
print(x, ...)
## S3 method for class 'diffnet_diffmap'
plot(x, y = NULL, ...)
graph |
A square matrix of size |
... |
Arguments passed to method. |
x |
An vector of length |
x.adj |
Function to adjust |
layout |
Either a |
jitter.args |
A list including arguments to be passed to |
kde2d.args |
A list including arguments to be passed to |
sharp.criter |
A function choose whether to apply a weighted mean for each cell, or randomize over the values present in that cell (see details). |
slice |
Integer scalar. Slice of the network to be used as baseline for drawing the graph. |
y |
Ignored. |
The image is created using the function kde2d
from
the MASS package. The complete algorithm follows:
x
is coerced into integer and the range is adjusted to start from 1.
NA
are replaced by zero.
If no layout
is passed, layout is computed using
layout_nicely
from igraph
Then, a kde2d
map is computed for each level of x
. The
resulting matrices are added up as a weighted sum. This only holds if
at the cell level the function sharp.criter
returns FALSE
.
The jitter function is applied to the repeated coordinates.
2D kernel is computed using kde2d
over the coordinates.
The function sharp.criter
must take two values, a vector of levels and a
vector of weights. It must return a logical scalar with value equal to TRUE
when a randomization at the cell level must be done, in which case the final
value of the cell is chosen using sample(x, 1, prob=w)
.
The resulting matrix can be passed to image
or similar.
The argument x.adj
uses by default the function round_to_seq
which basically maps x
to a fix length sequence of numbers such that
x.adj(x)
resembles an integer sequence.
A list of class diffnet_diffmap
coords |
A matrix of size |
map |
Output from |
h |
Bandwidth passed to |
George G. Vega Yon
Vega Yon, George G., and Valente, Thomas W., Visualizing Large Annotated Networks as Heatmaps using Weighted Averages based on Kernel Smoothers (Working paper).
Other visualizations:
dgr()
,
drawColorKey()
,
grid_distribution()
,
hazard_rate()
,
plot_adopters()
,
plot_diffnet2()
,
plot_diffnet()
,
plot_infectsuscep()
,
plot_threshold()
,
rescale_vertex_igraph()
# Example with a random graph --------------------------------------------------
set.seed(1231)
# Random scale-free diffusion network
x <- rdiffnet(500, 4, seed.graph="scale-free", seed.p.adopt = .025,
rewire = FALSE, seed.nodes = "central",
rgraph.arg=list(self=FALSE, m=4),
threshold.dist = function(id) runif(1,.2,.4))
# Diffusion map (no random toa)
dm0 <- diffusionMap(x, kde2d.args=list(n=150, h=.5), layout=igraph::layout_with_fr)
# Random
diffnet.toa(x) <- sample(x$toa, size = nnodes(x))
# Diffusion map (random toa)
dm1 <- diffusionMap(x, layout = dm0$coords, kde2d.args=list(n=150, h=.5))
oldpar <- par(no.readonly = TRUE)
col <- colorRampPalette(blues9)(100)
par(mfrow=c(1,2), oma=c(1,0,0,0))
image(dm0, col=col, main="Non-random Times of Adoption\nAdoption from the core.")
image(dm1, col=col, main="Random Times of Adoption")
par(mfrow=c(1,1))
mtext("Both networks have the same distribution on times of adoption", 1,
outer = TRUE)
par(oldpar)
# Example with Brazilian Farmers --------------------------------------------
dn <- brfarmersDiffNet
# Setting last TOA as NA
diffnet.toa(dn)[dn$toa == max(dn$toa)] <-
NA
# Coordinates
coords <- sna::gplot.layout.fruchtermanreingold(
as.matrix(dn$graph[[1]]), layout.par=NULL
)
# Plotting diffusion
plot_diffnet2(dn, layout=coords, vertex.size = 300)
# Adding diffusion map
out <- diffusionMap(dn, layout=coords, kde2d.args=list(n=100, h=50))
col <- adjustcolor(colorRampPalette(c("white","lightblue", "yellow", "red"))(100),.5)
with(out$map, .filled.contour(x,y,z,pretty(range(z), 100),col))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.