edges_coords function

Compute ego/alter edge coordinates considering alter's size and aspect ratio

Compute ego/alter edge coordinates considering alter's size and aspect ratio

Given a graph, vertices' positions and sizes, calculates the absolute positions of the endpoints of the edges considering the plot's aspect ratio.

edges_coords( graph, toa, x, y, vertex_cex, undirected = TRUE, no_contemporary = TRUE, dev = as.numeric(c()), ran = as.numeric(c()), curved = as.logical(c()) )

Arguments

  • graph: A square matrix of size nn. Adjacency matrix.
  • toa: Integer vector of size nn. Times of adoption.
  • x: Numeric vector of size nn. x-coordinta of vertices.
  • y: Numeric vector of size nn. y-coordinta of vertices.
  • vertex_cex: Numeric vector of size nn. Vertices' sizes in terms of the x-axis (see symbols).
  • undirected: Logical scalar. Whether the graph is undirected or not.
  • no_contemporary: Logical scalar. Whether to return (compute) edges' coordiantes for vertices with the same time of adoption (see details).
  • dev: Numeric vector of size 2. Height and width of the device (see details).
  • ran: Numeric vector of size 2. Range of the x and y axis (see details).
  • curved: Logical vector.

Returns

A numeric matrix of size m5m * 5 with the following columns: - x0, y0: Edge origin

  • x1, y1: Edge target

  • alpha: Relative angle between (x0,y0) and (x1,y1) in terms of radians

With mm as the number of resulting edges.

Details

In order to make the plot's visualization more appealing, this function provides a straight forward way of computing the tips of the edges considering the aspect ratio of the axes range. In particular, the following corrections are made at the moment of calculating the egdes coords:

  • Instead of using the actual distance between ego and alter, a relative one is calculated as follows
d=[(x0x1)2+(y0y1)2]12d=sqrt[(x0x1)2+(y0y1)2] d'=\left[(x_0-x_1)^2 + (y_0' - y_1')^2\right]^\frac{1}{2}d'=sqrt[(x0-x1)^2 + (y0'-y1')^2]

where c("%", "\n\n", "yi=yi[max(x)min(x)]/[max(y)min(y)]yi' = yi * [max(x) - min(x)]/[max(y) - min(y)]")

  • Then, for the relative elevation angle, alpha, the relative distance dd'

    is used, α=acos[(x0x1)/d]\alpha' = acos[ (x0 - x1)/d' ]

  • Finally, the edge's endpoint's (alter) coordinates are computed as follows:

%x_1' = x_1 + \cos(\alpha')\times v_1%x1' = x1 + cos(\alpha') * v1 %y_1' = y_1 -+ \sin(\alpha')\times v_1 \times\frac{\max y - \min y}{\max x - \min x}%y1' = y1 -+ sin(\alpha')*[max(y) - min(y)]/[max(x) - min(x)]

Where v1v1 is alter's size in terms of the x-axis, and the sign of the second term in y1y1' is negative iff y0\<y1y0\<y1.

The same process (with sign inverted) is applied to the edge starting piont. The resulting values, x1,y1x1',y1' can be used with the function arrows. This is the workhorse function used in plot_threshold.

The dev argument provides a reference to rescale the plot accordingly to the device, and former, considering the size of the margins as well (this can be easily fetched via par("pin"), plot area in inches).

On the other hand, ran provides a reference for the adjustment according to the range of the data, this is range(x)[2] - range(x)[1]

and range(y)[2] - range(y)[1] respectively.

Examples

# -------------------------------------------------------------------------- data(medInnovationsDiffNet) library(sna) # Computing coordinates set.seed(79) coords <- sna::gplot(as.matrix(medInnovationsDiffNet$graph[[1]])) # Getting edge coordinates vcex <- rep(1.5, nnodes(medInnovationsDiffNet)) ecoords <- edges_coords( medInnovationsDiffNet$graph[[1]], diffnet.toa(medInnovationsDiffNet), x = coords[,1], y = coords[,2], vertex_cex = vcex, dev = par("pin") ) ecoords <- as.data.frame(ecoords) # Plotting symbols(coords[,1], coords[,2], circles=vcex, inches=FALSE, xaxs="i", yaxs="i") with(ecoords, arrows(x0,y0,x1,y1, length=.1))