Title: | Visualization using Graph Traversal |
---|---|
Description: | Improving graphics by ameliorating order effects, using Eulerian tours and Hamiltonian decompositions of graphs. References for the methods presented here are C.B. Hurley and R.W. Oldford (2010) <doi:10.1198/jcgs.2010.09136> and C.B. Hurley and R.W. Oldford (2011) <doi:10.1007/s00180-011-0229-5>. |
Authors: | C.B. Hurley and R.W. Oldford |
Maintainer: | Catherine Hurley <[email protected]> |
License: | GPL-2 |
Version: | 1.3.6 |
Built: | 2024-11-08 03:13:02 UTC |
Source: | https://github.com/cbhurley/pairviz |
Re-orients a path/cycle, preserving adjacencies so that weights tend to decrease. From specifies the starting point, for cycles only.
best_orientation(path, d, cycle=FALSE, path_dir= path_cor, from=NULL)
best_orientation(path, d, cycle=FALSE, path_dir= path_cor, from=NULL)
path |
A vector giving a hamiltonian. |
d |
A |
cycle |
If |
path_dir |
A function used to evaluate a path start and orientation |
from |
Sepcifies the starting point, for cycles only. |
C.B. Hurley and R.W. Oldford
see overview
require(PairViz) rdist <- function(n) { d <- matrix(0,n,n) d[lower.tri(d)] <- runif(n*(n-1)/2) return(as.dist(d)) } r <- rdist(7) best_orientation(1:7,r) best_orientation(1:7,r,cycle=TRUE)
require(PairViz) rdist <- function(n) { d <- matrix(0,n,n) d[lower.tri(d)] <- runif(n*(n-1)/2) return(as.dist(d)) } r <- rdist(7) best_orientation(1:7,r) best_orientation(1:7,r,cycle=TRUE)
Patients with advanced cancers of the stomach, bronchus, colon, ovary or breast were treated with ascorbate. The purpose of the study was to determine if the survival times differ with respect to the organ affected by the cancer.
data(cancer)
data(cancer)
This data frame contains the following columns:
Survival
time in days
Organ
Organ affected by the cancer
Cameron, E. and Pauling, L. (1978) Supplemental ascorbate in the supportive treatment of cancer: re-evaluation of prolongation of survival times in terminal human cancer. Proceedings of the National Academy of Science USA, 75, 4538-4542.
Also found in: Manly, B.F.J. (1986) Multivariate Statistical Methods: A Primer, New York: Chapman and Hall, 11. Also found in: Hand, D.J., et al. (1994) A Handbook of Small Data Sets, London: Chapman and Hall, 255.
Desaturates colors
desaturate_color(cols, frac = 0.8)
desaturate_color(cols, frac = 0.8)
cols |
Colors |
frac |
Fraction to desaturate by. |
Desaturated version of orginal colors
Constructs an eulerian
on the complete graph where nodes are integers 1..n. The result in an euler tour for odd n
. For even n
the result is not exactly an euler tour or path because (n-2)/2 edges must be visited twice.
eseq(n) eseqa(n) kntour_drop(e) kntour_add(e)
eseq(n) eseqa(n) kntour_drop(e) kntour_add(e)
n |
a positive integer. |
e |
an euler tour on Kn where n is odd |
The algorithm used for eseq builds up a path on 1..n by appending extra edges on to the path on nodes 1..(n-2).
The function eseqa constructs paths on 1..n using an alternative algorithm. For odd n, the tour starts at 1, then takes steps of size 1,2,..m repeatedly, where m is (n-1)/2, For even n, the path constructed is formed as eseqa(n+1), followed by dropping node n+1.
The function kntour_drop removes instances of n from the tour, creating an open approximately eulerian path on the complete graph with n-1 nodes.
The function kntour_add inserts an extra node n+1 into a tour on nodes 1, ..n. It adds a detour to the tour visiting all edges joining nodes 1..n to n+1. The result is an open approximately eulerian path on the complete graph with n+1 nodes.
a numeric vector.
C.B. Hurley and R.W. Oldford
see overview
require(PairViz) eseq(5) eseq(6)
require(PairViz) eseq(5) eseq(6)
etour
– Constructs an eulerian tour on a graph using Hierholzer's algorithm. Returns a vector of node labels. If weighted
is TRUE
constructs a weight-decreasing eulerian using the modified Hierholzer's algorithm. Usually etour
is not called directly, rather the generic function eulerian
is used.
etour(g, start=NULL,weighted=TRUE)
etour(g, start=NULL,weighted=TRUE)
g |
a graph satisfying |
start |
an optional starting node for the tour. |
weighted |
whether tour uses weights |
The supplied graph should satisfyis_even_graph
. If weighted
is TRUE
the lowest weight edge is found, and the tour starts at the
one of its nodes, picking the node with the bigger second-smallest edge weight.
After that the tour follows weight-increasing edges.
If weighted
is FALSE
weights are ignored. The returned tour is typically a closed path. However, if the last edge is a duplicated edge added to make the graph even, this edge is omitted and the result is an open path.
C.B. Hurley and R.W. Oldford
see overview
require(PairViz) g <- mk_even_graph(5) etour(g) g <- mk_even_graph(6) # adds 3 extra edges to g, so all nodes are even etour(g) etour(g, start= "4") # modifies the starting node eulerian(6) # The eulerian wrapper looks after making even graph, #also returns numbers rather than nodes # On a general graph. v <- LETTERS[1:4] g <- new("graphNEL",nodes=v) g <- addEdge(v[1],v[3:4],g,1:2) g <- addEdge(v[2],v[3:4],g,3:4) etour(g) eulerian(g) # Equivalently, use eulerian wrapper n <- LETTERS[1:5] g <- new("graphNEL",nodes=n) g <- addEdge(n[1],n[2:3],g) g <-addEdge(n[2],n[3:5],g) g <-addEdge(n[4],n[3],g) is_even_graph(g) etour(mk_even_graph(g)) eulerian(g) # Equivalently, use eulerian wrapper
require(PairViz) g <- mk_even_graph(5) etour(g) g <- mk_even_graph(6) # adds 3 extra edges to g, so all nodes are even etour(g) etour(g, start= "4") # modifies the starting node eulerian(6) # The eulerian wrapper looks after making even graph, #also returns numbers rather than nodes # On a general graph. v <- LETTERS[1:4] g <- new("graphNEL",nodes=v) g <- addEdge(v[1],v[3:4],g,1:2) g <- addEdge(v[2],v[3:4],g,3:4) etour(g) eulerian(g) # Equivalently, use eulerian wrapper n <- LETTERS[1:5] g <- new("graphNEL",nodes=n) g <- addEdge(n[1],n[2:3],g) g <-addEdge(n[2],n[3:5],g) g <-addEdge(n[4],n[3],g) is_even_graph(g) etour(mk_even_graph(g)) eulerian(g) # Equivalently, use eulerian wrapper
A generic function that returns an eulerian (or nearly eulerian)
path based on self
.
eulerian(self, start=NULL,weighted=TRUE)
eulerian(self, start=NULL,weighted=TRUE)
self |
– see below |
start |
– see below |
weighted |
– see below |
A vector representing the eulerian- a character vector of node names for a graph, otherwise a numeric vector. If the graph is not connected, the result is a list of eulerians for each connected component.
Uses etour
to construct the eulerian. If weighted
is TRUE a weighted eulerian is constructed, otherwise weights are ignored. A non-null start
is the eulerian starting point.
Augments the graph using mk_euler_graph
, then invokes eulerian again on the augmented verion. If self is not connected, (approximate) eulerians are formed for each connected component, which are returned as a list.
Builds a graph using mk_euler_graph
, then invokes eulerian again on the result.
Builds a graph with self nodes using mk_euler_graph
, then invokes eulerian again on the result.
Builds a graph using mk_euler_graph
, then invokes eulerian again on the result.
C.B. Hurley and R.W. Oldford
C. Hierholzer (1873). Uber die Moglichkeit, einen Linienzug ohne Wiederholung und ohne Unterbrechung zu umfahren. Math. Annalen VI, pp. 30-32.
Also, see overview
require(PairViz) d <- as.matrix(eurodist)[1:8,1:8] # pick the first 8 cities eulerian(d) eulerian(d, weighted=FALSE) # In this case, starts at city 1 and ends at city 8
require(PairViz) d <- as.matrix(eurodist)[1:8,1:8] # pick the first 8 cities eulerian(d) eulerian(d, weighted=FALSE) # In this case, starts at city 1 and ends at city 8
This class is an extension of graphNEL-class
. For graphs of this class, euler tours may always be constructed. Objects of this class should be created by mk_even_graph
This class has all slots from graphNEL-class
plus:
dummy_node
:Object of class "character"
extra_edges
:Object of class "character"
weighted
:Object of class "logical"
Class graphNEL-class
, directly.
Class graph-class
, by class "graphNEL", distance 2.
signature(g = "graphNEL")
: checks whether a graph has all nodes of even degree.
signature(g = "even_graph")
: always TRUE.
C.B. Hurley and R.W. Oldford
see overview
showClass("even_graph")
showClass("even_graph")
Returns a path, constructed by applying the function in path
to the edge weights. If each edge has many weights, i.e if edgew
is a matrix, these weights are first reduced by the function combine
applied to the rows. If path
is NULL, the returned path defaults to 1..nnodes(edgew)
find_path(edgew, path=NULL, combine=sum, edge.index=edge_index(edgew),...)
find_path(edgew, path=NULL, combine=sum, edge.index=edge_index(edgew),...)
edgew |
Matrix (or vector) whose ith row (or element) has weights for pair indexed by pair in row i of edge.index. |
path |
a function used to construct the index path. |
combine |
A function that combines the row of weights for an edge into a single numeric value. |
edge.index |
A 2-column matrix with each row giving indices for
corresponding weight in |
... |
passed to path construction function. |
C.B. Hurley and R.W. Oldford
Draws a parallel coordinate plot, with an accompanying barchart showing an index (eg correlation, scagnostics) levels for each panel. An index legend is optional.
guided_pcp(data, edgew=NULL, path = NULL, pathw=NULL,zoom=NULL,pcpfn=pcp, pcp.col = 1,lwd=0.5, panel.colors=NULL, pcp.mar=c(1.5,2,2,2), pcp.scale=TRUE, bar.col=1:9,bar.axes=FALSE, bar.mar=NULL,bar.ylim=NULL, reorder.weights=TRUE, layout.heights=NULL, layout.widths=c(10,1), main=NULL,legend=FALSE,cex.legend = 1,legend.mar=c(1,4,1,1),...)
guided_pcp(data, edgew=NULL, path = NULL, pathw=NULL,zoom=NULL,pcpfn=pcp, pcp.col = 1,lwd=0.5, panel.colors=NULL, pcp.mar=c(1.5,2,2,2), pcp.scale=TRUE, bar.col=1:9,bar.axes=FALSE, bar.mar=NULL,bar.ylim=NULL, reorder.weights=TRUE, layout.heights=NULL, layout.widths=c(10,1), main=NULL,legend=FALSE,cex.legend = 1,legend.mar=c(1,4,1,1),...)
data |
A data frame or matrix. |
edgew |
Matrix (or vector) whose rows give index values for each pair of variables. |
path |
an index vector specifying variable order, or a function. If a function, |
pathw |
Matrix (or vector) whose rows give index values for each adjacent pair of variables in path. Usually this argument is NULL and |
zoom |
If provided, a numeric vector specifying a subsequence of path to display. |
pcpfn |
Function to draw the parallel coordinates. |
pcp.col |
Line colors. |
lwd |
Line widths. |
panel.colors |
Background panel colors, passed to the |
pcp.mar |
Controls PCP margin size. |
pcp.scale |
If TRUE, the variables will be scaled to 0-1 range, otherwise the data is not scaled. |
bar.col |
Bar colors. |
bar.axes |
Draw barplot axes, if TRUE. |
bar.mar |
Controls barplot margin size. |
bar.ylim |
Vertical limits of bar plot. |
reorder.weights |
If TRUE, reorder barplot indices so large values are drawn at the bottom. |
layout.heights |
Controls the layout. |
layout.widths |
Controls the layout. |
main |
Main title for PCP. |
legend |
If TRUE, draws the barplot index legend. |
cex.legend |
Controls legend text size. |
legend.mar |
Legend margin size. |
... |
Optional arguments |
C.B. Hurley and R.W. Oldford
see overview
require(PairViz) data <- mtcars[,c(1,3:6)] cols <- c("red","green")[mtcars[,9]+1 ] # transmission type, red=automatic # add a correlation guide and find "better" hamiltonians... # add a correlation guide... corw <- dist2edge(as.dist(cor(data))) edgew <- cbind(corw*(corw>0), corw*(corw<0)) # add a correlation guide to a PCP, positive cors shown in blue, negative in purple... ## Not run: dev.new(width=3,height=3) par(cex.axis=.65) guided_pcp(data,edgew, pcp.col=cols, main="Correlation guided PCP",bar.col = c("blue","purple")) dev.new(width=7,height=3) par(cex.axis=.65) guided_pcp(data,edgew, path=eulerian, pcp.col=cols,lwd=2, main="Correlation guided Eulerian PCP",bar.col = c("blue","purple"),bar.axes=TRUE) ## End(Not run) # Scagnostic guides are useful here- see the demos for more examples.
require(PairViz) data <- mtcars[,c(1,3:6)] cols <- c("red","green")[mtcars[,9]+1 ] # transmission type, red=automatic # add a correlation guide and find "better" hamiltonians... # add a correlation guide... corw <- dist2edge(as.dist(cor(data))) edgew <- cbind(corw*(corw>0), corw*(corw<0)) # add a correlation guide to a PCP, positive cors shown in blue, negative in purple... ## Not run: dev.new(width=3,height=3) par(cex.axis=.65) guided_pcp(data,edgew, pcp.col=cols, main="Correlation guided PCP",bar.col = c("blue","purple")) dev.new(width=7,height=3) par(cex.axis=.65) guided_pcp(data,edgew, path=eulerian, pcp.col=cols,lwd=2, main="Correlation guided Eulerian PCP",bar.col = c("blue","purple"),bar.axes=TRUE) ## End(Not run) # Scagnostic guides are useful here- see the demos for more examples.
zigzag
- Constructs hamiltonian paths where each pair (i,j)
appears in at least one of the hamiltonians.
hpaths
- Returns a hamiltonian decomposition on the complete graph with n nodes. See Details.
permute_hpaths
- Returns a modified version of paths
, where vertices
are re-labelled so that the first hamiltonian is path1
.
zigzag(n) hpaths(n, matrix=TRUE,cycle=NULL,...) permute_hpaths(path1,paths= hpaths(length(path1)), matrix=TRUE,...)
zigzag(n) hpaths(n, matrix=TRUE,cycle=NULL,...) permute_hpaths(path1,paths= hpaths(length(path1)), matrix=TRUE,...)
n |
a positive integer. For |
matrix |
if |
cycle |
If |
path1 |
A vector- This will be the first hamiltonian of the returned hamiltonian decomposition. |
paths |
A matrix where each row is a hamiltonian. |
... |
Ignored. |
hpaths
-
From graph theory we know that for odd n, the complete graph decomposes into (n-1)/2 edge distinct hamiltonian
cycles, while for even n the graph decomposes into n/2 edge distinct hamiltonian paths.
The default behaviour of the function hpaths
is to produce the cycle decomposition for odd n and the path
decomposition for even n.
However, if a TRUE
value is supplied as argument cycle, the returned paths are cycles, and the result is a true decomposition for odd n, but for even n the last hamiltonian has some duplicate edges.
If a FALSE
value is supplied as argument cycle, the returned paths are open, and the result is a true decomposition
for even n, but for odd n the last hamiltonian has some duplicate edges.
A numeric matrix where each row contains a permutation of 1..n, or these rows concatenated into a vector if matrix=FALSE
.
C.B. Hurley and R.W. Oldford
D.E. Lucas (1892), Recreations Matematiques, Vol II. Gauthier Villars, Paris.
Also see overview
require(PairViz) zigzag(7) hpaths(7) # the rows form a decomp. into hamiltonian cycles # Now concatenate the rows and close the path hpaths(7,matrix=FALSE) # Form a decomposition into hamiltonian cycles- # this decomposition is not exact, as the last row duplicates edges hpaths(7,cycle=FALSE) # For even n, the default is a decomposition into hamiltonian paths, not cycles. hpaths(6) # If cycles are required for even n, # the decomposition will not be exact and the last row duplicates edges hpaths(6,cycle=TRUE) # If you want to specify the first hamiltonian of the decomposition, use hpaths(1:7)
require(PairViz) zigzag(7) hpaths(7) # the rows form a decomp. into hamiltonian cycles # Now concatenate the rows and close the path hpaths(7,matrix=FALSE) # Form a decomposition into hamiltonian cycles- # this decomposition is not exact, as the last row duplicates edges hpaths(7,cycle=FALSE) # For even n, the default is a decomposition into hamiltonian paths, not cycles. hpaths(6) # If cycles are required for even n, # the decomposition will not be exact and the last row duplicates edges hpaths(6,cycle=TRUE) # If you want to specify the first hamiltonian of the decomposition, use hpaths(1:7)
Functions to construct graphs- see details below.
knn_graph(g, k = 2) dn_graph(g, d = 1, test=`<=`) mk_binary_graph(n,sep="",delta=1,test=`==`) mk_hypercube_graph(n,sep="") mk_line_graph(g,sep="-") kspace_graph(n,m, link=NULL,sep="-") graph_product(g,h, type="cartesian",sep="-") graph_compose(g,h,sep="-") graph_sum(g,h,combineWeight=`+`) bipartite_graph(n1,n2) iterated_line_graph(g,sep="-")
knn_graph(g, k = 2) dn_graph(g, d = 1, test=`<=`) mk_binary_graph(n,sep="",delta=1,test=`==`) mk_hypercube_graph(n,sep="") mk_line_graph(g,sep="-") kspace_graph(n,m, link=NULL,sep="-") graph_product(g,h, type="cartesian",sep="-") graph_compose(g,h,sep="-") graph_sum(g,h,combineWeight=`+`) bipartite_graph(n1,n2) iterated_line_graph(g,sep="-")
g |
a graph |
h |
a graph |
n |
a positive integer, or a character vector. |
k |
a positive integer |
d |
an edge weight |
test |
used to select edges. |
sep |
used to form node names of new graph. |
m |
subsets of size m are nodes of kneser graph. |
link |
A positive number or NULL. If NULL, the returned graph is complete. Otherwise edges for subsets sharing link elements. |
type |
the type of graph product, one of "cartesian", "strong" or "tensor" |
n1 |
a character vector. |
n2 |
a character vector. |
delta |
used to select edges. |
combineWeight |
used to combine weights. |
knn_graph- returns a symmetric k nearest neighbour graph
dn_graph- returns a graph formed from g where edges of satisfy test(weight, d). The default retains edges whose weight are 1 are less. Nodes with no edges are also removed.
mk_hypercube_graph- returns a hypercube graph with $2^n$ nodes
mk_binary_graph(n,sep="",delta=1,test='==') - returns a graph with nodes. Undirected edges join nodes A and B whose binary vectors satisfy
and test(
, delta) is true.
mk_line_graph- returns the line graph of g
kspace_graph- returns a graph where nodes are subsets of size m from n. Edges are connect nodes whose subsets share link elements. The standard kneser graph has link=0. When link is NULL, returned graph is complete.
graph_product(g,h, type="cartesian",sep="-")- returns the graph product of g and h.
graph_compose(g,h,sep="-")- returns the graph composition of g and h.
bipartite_graph(n1,n2)- returns the complete bipartite graph with node sets n1 and n2.
graph_sum(g,h,combineWeight='+')- returns a graph whose nodes and edges are the union of those in g and h. Weights of common edges are combined using combineWeight.
iterated_line_graph- returns the iterated line graph of g, with compression of nodes as described in the reference Hurley and Oldford(2008) given below.
C.B. Hurley and R.W. Oldford
See any Graph Theory text. Also C.B. Hurley and R.W. Oldford, Graphs as navigational infrastructure for high dimensional data spaces. 2008 submitted.
# See the demo file nav.R
# See the demo file nav.R
For grouped data. Draws boxplots for each group and overlays with confidence intervals for pairwise comparison of means.
mc_plot(data, fit, path = eulerian, col = rainbow(length(data), s = 0.4), levels = c(0.9, 0.95, 0.99), varwidth = TRUE, frame.plot = FALSE, boxwex = 0.3, cex=0.75, zoom=NULL,ci.yusr=NULL,ci.pos=FALSE,...)
mc_plot(data, fit, path = eulerian, col = rainbow(length(data), s = 0.4), levels = c(0.9, 0.95, 0.99), varwidth = TRUE, frame.plot = FALSE, boxwex = 0.3, cex=0.75, zoom=NULL,ci.yusr=NULL,ci.pos=FALSE,...)
data |
A list of vectors, such as that returned by |
fit |
Either an |
path |
an index vector or a function. If a vector, groups are plotted in order |
col |
A vector of colours, one per group. |
levels |
Vector of increasing confidence levels. |
varwidth |
Passed to |
frame.plot |
Passed to |
boxwex |
Passed to |
cex |
Passed to |
zoom |
If provided, a numeric vector specifying a subsequence of path to display. |
ci.yusr |
Specifies the vertical |
ci.pos |
If TRUE, all CIs are mu(max) - mu(min), otherwise mu(right) - mu(left). |
... |
Optional arguments, passed to |
C.B. Hurley and R.W. Oldford
see overview
See also overlayCI
require(PairViz) data(cancer) bx <- with(cancer, split(sqrt(Survival),Organ)) a <- aov(sqrt(Survival) ~ Organ,data=cancer) ## Not run: dev.new(height=4.5, width=9.5) op <- par(no.readonly = TRUE) par(cex.axis=.75, cex.main = 1.0, cex.lab=1) par(mar=c(3,5,3,5)) mc_plot(bx,a,main="Pairwise comparisons of cancer types", ylab="Sqrt Survival") par(op) ## End(Not run)
require(PairViz) data(cancer) bx <- with(cancer, split(sqrt(Survival),Organ)) a <- aov(sqrt(Survival) ~ Organ,data=cancer) ## Not run: dev.new(height=4.5, width=9.5) op <- par(no.readonly = TRUE) par(cex.axis=.75, cex.main = 1.0, cex.lab=1) par(mar=c(3,5,3,5)) mc_plot(bx,a,main="Pairwise comparisons of cancer types", ylab="Sqrt Survival") par(op) ## End(Not run)
Constructs a complete graph, actually an instance of graph-NEL
mk_complete_graph(d)
mk_complete_graph(d)
d |
an integer vector of length 1 which specified the number of nodes, a character vector of nodes names, a |
- a graph-NEL
C.B. Hurley and R.W. Oldford
require(PairViz) d <- dist(rnorm(5)) g <- mk_complete_graph(d)
require(PairViz) d <- dist(rnorm(5)) g <- mk_complete_graph(d)
~~ Methods for function mk_even_graph
. Each of these return an instance of even_graph
, where all nodes are of even degree. The result satisfies is_even_graph
.
The resulting graph yields an euler tour.
This is the workhorse method. If self
does not satisfy is_even_graph
, the graph is forced to be even by one of the folowing. If add_edges is TRUE, the odd nodes are paired off and a new edge added between each pair, possibly duplicating an existing edge. If add_edges is a vector of the odd nodes, they are paired off in this order.
If add_edges is FALSE a new dummy node is added with
edges going to all odd nodes.
first constructs a complete graph using mk_complete_graph
, which is then augmented to be even.
first constructs a complete graph using mk_complete_graph
, which is then augmented to be even.
first constructs a complete graph using mk_complete_graph
, which is then augmented to be even.
returns self.
see overview
mk_complete_graph
, is_even_graph
Returns the best hamiltonian
order_best(d, maxexact=9,nsamples=50000,path_weight=sum, cycle=FALSE,path_dir = path_cor,...)
order_best(d, maxexact=9,nsamples=50000,path_weight=sum, cycle=FALSE,path_dir = path_cor,...)
d |
A |
maxexact |
If the sequence length is |
nsamples |
If the sequence length is |
cycle |
If |
path_weight |
Combines edge weights into a single path/cycle weight. |
path_dir |
If a function is provided, used to re-orient the cycle/path. Default function is |
... |
Ignored. |
Requires package gtools.
Currently it is possible to find the best hamiltonian by complete enumeration for up to 10 nodes.
When path_dir
is non NULL, the returned hamiltonian is also optimally oriented using best_orientation
, which compares orientations via path_dir
.
A vector containing a permutation of 1..n
C.B. Hurley and R.W. Oldford
see overview
require(PairViz) order_best(eurodist)
require(PairViz) order_best(eurodist)
Returns shortest cycle or path via tsp solver from package TSP
order_tsp(d, method = "nearest", cycle=FALSE,improve=FALSE,path_dir = path_cor,...)
order_tsp(d, method = "nearest", cycle=FALSE,improve=FALSE,path_dir = path_cor,...)
d |
A |
method |
Options are |
improve |
if |
cycle |
If |
path_dir |
If a function is provided, used to re-orient the cycle/path. Default function is |
... |
passed to |
Requires package TSP.
When path_dir
is non NULL, the returned hamiltonian is also optimally oriented using best_orientation
, which compares orientations via path_dir
.
A vector containing a permutation of 1..n
C.B. Hurley and R.W. Oldford
See package TSP.
order_best
, solve_TSP
in TSP.
require(PairViz) rdist <- function(n) { d <- matrix(0,n,n) d[lower.tri(d)] <- runif(n*(n-1)/2) return(as.dist(d)) } order_tsp(rdist(7)) edist <- as.dist(as.matrix(eurodist)) order_tsp(edist)
require(PairViz) rdist <- function(n) { d <- matrix(0,n,n) d[lower.tri(d)] <- runif(n*(n-1)/2) return(as.dist(d)) } order_tsp(rdist(7)) edist <- as.dist(as.matrix(eurodist)) order_tsp(edist)
Overlays confidence intervals on the current plot. Also draws a right hand axis, a horizontal broken line at zero, and marks the significant comparisons with an arrow, i.e. the CIs that do not intersect zero.
overlayCI(cis, xpos=NULL,ci.cols = NULL, ci.ex = 2, ci.ocol = "grey40", p.col = "grey40", pch = 1, sig.col = "red", sig.lwd = 1, yusr = NULL, ci.label="Differences",ci.cex=0.5,arrow.length=0.1,...)
overlayCI(cis, xpos=NULL,ci.cols = NULL, ci.ex = 2, ci.ocol = "grey40", p.col = "grey40", pch = 1, sig.col = "red", sig.lwd = 1, yusr = NULL, ci.label="Differences",ci.cex=0.5,arrow.length=0.1,...)
cis |
A matrix containing the confidence intervals. Each row corresponds to a different comparison, the first column is the estimated mean, and succesive pairs of columns give the lower and upper limits for different confidence levels. |
ci.cols |
A vector of colours, one colour for each confidence level. Defaults to shades of grey. |
ci.ex |
Controls confidence interval line width. |
xpos |
Horizonal positions where CIs are drawn. Defaults to 1.5,2.5,3.5,.. |
ci.ocol |
Colour of zero line. |
p.col |
Colour of point used for CI centre. |
pch |
Symbol used for CI centre. |
sig.col |
Colour of arrow marking significant comparisons. |
sig.lwd |
Width of arrow marking significant comparisons. |
yusr |
Specifies the vertical |
ci.label |
Label drawn on right margin. |
ci.cex |
Controls size of CI mean point symbol. |
arrow.length |
Controls size arrow at right hand axis. |
... |
Ignored |
This function is called by mc_plot
C.B. Hurley and R.W. Oldford
see overview
See Also as mc_plot
Implements methods described in Hurley and Oldford paper.
There are functions for constructing eulerian paths on complete graphs- see eseq
, hpaths
, and weighted_hpaths
, and eulerians on general graphs-
see etour
and eulerian
.
There are also functions for new types of graphics, mc_plot
, catpcp
and
guided_pcp
and a barchart/mosaic variant table_plot
.
C.B. Hurley and R.W. Oldford
C.B. Hurley and R.W. Oldford, Pairwise display of high dimensional information via Eulerian tours and Hamiltonian decompositions. Journal of Computational and Graphical Statistics. 19(10), pp. 861–886, 2010.
C.B. Hurley and R.W. Oldford, Eulerian tour algorithms for data visualization and the PairViz package. Computational Statistics, 26(4), pp 613–633, 2011.
Returns the (Kendalls tau) correlation of the edge weights with the vector 1.. (number of weights).
path_cor(edgew, method = "kendall")
path_cor(edgew, method = "kendall")
edgew |
A vector of edge weights. |
method |
passed to |
These functions perform calculations on edge matrices containing pairwise information.
path_weights(edgew, path, symmetric = TRUE,edge.index=edge_index(edgew),...) path_cis(edgew, path,edge.index=edge_index(edgew),ci.pos=FALSE) edge2dist(edgew, edge.index=edge_index(edgew)) dist2edge(d) edge_index(x, order="default")
path_weights(edgew, path, symmetric = TRUE,edge.index=edge_index(edgew),...) path_cis(edgew, path,edge.index=edge_index(edgew),ci.pos=FALSE) edge2dist(edgew, edge.index=edge_index(edgew)) dist2edge(d) edge_index(x, order="default")
edgew |
A Matrix (or vector) whose ith row (or element) has weights for pair indexed by pair in row i of edge.index.
For |
path |
Vector of indices into rows of |
symmetric |
If |
edge.index |
A 2-column matrix with each row giving indices for
corresponding weight in |
ci.pos |
If TRUE, all CIs are mu(max) - mu(min), otherwise mu(right) - mu(left). |
d |
A |
order |
If "low.order.first" or "scagnostics", lists lowest index pairs first, otherwise lists pairs starting with 1, then 2 etc. |
x |
An edgew matrix or vector, or a positive integer. |
... |
Ignored |
path_weights
- Returns matrix of path weights so that the ith row of result contains weights for indices path[i], path[i+1]
path_cis
- Returns matrix of path confidence intervals so that the ith row of result contains intervals for mean-path[i] - mean-path[i+1]
edge2dist
- Returns a dist
,
containing elements of edgew
.
dist2edge
- Returns a vector of edge weights.
edge_index
-A generic function. Returns a 2-column matrix with one row for
each edge. Each row contains an index pair i,j. If order
is "low.order.first" or "scagnostics", lists lowest index pairs first - this is the default ordering for class scagdf
, otherwise lists pairs
starting with 1, then 2 etc
nnodes
- Here edgew
contains edge weights for a complete graph; returns the number of nodes in this complete graph.
C.B. Hurley and R.W. Oldford
see overview
require(PairViz) s <- matrix(1:40,nrow=10,ncol=4) edge2dist(s[,1]) path_weights(s,1:4) path_weights(s,eseq(5)) fm1 <- aov(breaks ~ wool + tension, data = warpbreaks) tuk <- TukeyHSD(fm1, "tension")[[1]] # Here the first argument (weight matrix) can have number of columns path_weights(tuk,c(1:3,1)) # Here the first argument (weight matrix) should have an odd number of columns- # the first is the mean difference, other column pairs are endpoints of CIs path_cis(tuk[,-4],c(1:3,1))
require(PairViz) s <- matrix(1:40,nrow=10,ncol=4) edge2dist(s[,1]) path_weights(s,1:4) path_weights(s,eseq(5)) fm1 <- aov(breaks ~ wool + tension, data = warpbreaks) tuk <- TukeyHSD(fm1, "tension")[[1]] # Here the first argument (weight matrix) can have number of columns path_weights(tuk,c(1:3,1)) # Here the first argument (weight matrix) should have an odd number of columns- # the first is the mean difference, other column pairs are endpoints of CIs path_cis(tuk[,-4],c(1:3,1))
pcp draws a parallel coordinate plot.It is a modified
version of parcoord {MASS}
.
Variables
may be reordered and panels colored in the display.
catpcp draws a parallel coordinate plot variant for categorical data.
pcp(data, order = NULL, panel.colors = NULL, col = 1, lty = 1, horizontal = TRUE, mar = NULL, scale = TRUE, axis.width = 0, axis.grid.col="grey70",connect=TRUE, ...) catpcp(data, order = NULL, pcpbars, barvars = 1:ncol(data), pcpbars.border = "black", pcpbars.col = NULL, pcpbars.labels = FALSE, pcpbars.axis.at = NULL, pcpbars.axis.labels = NULL, axis.width = 0.2,connect=TRUE, ...)
pcp(data, order = NULL, panel.colors = NULL, col = 1, lty = 1, horizontal = TRUE, mar = NULL, scale = TRUE, axis.width = 0, axis.grid.col="grey70",connect=TRUE, ...) catpcp(data, order = NULL, pcpbars, barvars = 1:ncol(data), pcpbars.border = "black", pcpbars.col = NULL, pcpbars.labels = FALSE, pcpbars.axis.at = NULL, pcpbars.axis.labels = NULL, axis.width = 0.2,connect=TRUE, ...)
data |
A data frame or matrix. |
order |
an index vector specifying variable order. If NULL, all variables are used. |
panel.colors |
either a vector or a matrix of panel colors. If a vector is supplied, the ith color is used for the ith panel. If a matrix, dimensions should match those of the variables. Diagonal entries are ignored. |
col |
a vector of colours, recycled as necessary for each observation. |
lty |
a vector of line types, recycled as necessary for each observation. |
horizontal |
If TRUE, orientation is horizontal. |
mar |
margin parameters, passed to |
scale |
If TRUE, the variables are scaled to the unit interval. |
axis.width |
Width of each of the parallel axes. |
axis.grid.col |
Color of variable axes. Use NULL for no axes. |
connect |
If FALSE, line segments are not connected. Points are drawn if axis.width=0. |
pcpbars |
A list, with one component per barvar. Component i is a matrix with the bottom and top of the bars for that variable. |
barvars |
Categorical variables where overlayed bars show the level frequency. |
pcpbars.border |
Border colour of the bars. |
pcpbars.col |
Colour of the bars. |
pcpbars.labels |
Labels for the bars. |
pcpbars.axis.at |
Axis label positions for the bars. |
pcpbars.axis.labels |
Axis label text for the bars. |
... |
other parameters, passed to pcp by catpcp |
require(PairViz) y <- as.data.frame(as.table(HairEyeColor)) colvar <- 3 # any of 1:3 will do y <- y[order(y[,colvar]),] # ensures that cases are ordered by colour within each factor level ylong <- apply(y[,-4],2, function(x) rep(x,times=y[,4])) cols <- desaturate_color(rainbow(4,alpha=0.3),.5) cols <- cols[as.numeric(as.factor(ylong[,colvar]))] ds <- factor_spreadout(ylong) dev.new(width=5,height=2.5) par(mar=c(2,1,2,1)) par( cex.axis=.8,cex=.8) catpcp(ds$data,col=cols,lwd=2,pcpbars=ds$bars,pcpbars.labels=TRUE,main="Hair Eye data")
require(PairViz) y <- as.data.frame(as.table(HairEyeColor)) colvar <- 3 # any of 1:3 will do y <- y[order(y[,colvar]),] # ensures that cases are ordered by colour within each factor level ylong <- apply(y[,-4],2, function(x) rep(x,times=y[,4])) cols <- desaturate_color(rainbow(4,alpha=0.3),.5) cols <- cols[as.numeric(as.factor(ylong[,colvar]))] ds <- factor_spreadout(ylong) dev.new(width=5,height=2.5) par(mar=c(2,1,2,1)) par( cex.axis=.8,cex=.8) catpcp(ds$data,col=cols,lwd=2,pcpbars=ds$bars,pcpbars.labels=TRUE,main="Hair Eye data")
factor_spreadout spreads out the data at each factor level. rater_spreadout spreads out the data at each rating level. The rater version is appropriate when the variables (factors) have all the same levels.
factor_spreadout(d) rater_spreadout(d, levs, minspace = NULL,scale=FALSE)
factor_spreadout(d) rater_spreadout(d, levs, minspace = NULL,scale=FALSE)
d |
A data frame where each variable can be interpreted as a factor. |
levs |
The rating levels. Specifying this controls the order of rating levels on each axis. |
minspace |
The minimum amount of space between the bars. |
scale |
If scale=FALSE, the ith rater values are spreadout about the value i. If scale=TRUE, all values are scaled to 0-1. |
factor_spreadout spreads out the data at each factor level. It returns a list with two components. The first is data, containing the spreadout data, scaled to 0-1. The second is bars, which is a list whose ith component gives the bottom and top of the bars for the ith variable of d.
rater_spreadout spreads out the data at each rater level. It returns a list with two components. The first is data, containing the spreadout data. If scale=FALSE, the ith rater values are spreadout about the value i. If scale=TRUE, all values are scaled to 0-1. The second component is bars, which is a list whose ith component gives the bottom and top of the bars for the ith variable of d.
Plots rectangles on a grid- a barchart/mosaic variant which facilitates pairwise comparisons.
table_plot(rectw, recth, col="grey50", gapx = NULL, gapy = NULL, spacex = 0.03, spacey = 0.03, xjust = "center", yjust = "center", xruler = NULL, yruler = NULL, color.ruler = "grey32", pch0=1,xlab=NULL,ylab=NULL, plot=TRUE,...)
table_plot(rectw, recth, col="grey50", gapx = NULL, gapy = NULL, spacex = 0.03, spacey = 0.03, xjust = "center", yjust = "center", xruler = NULL, yruler = NULL, color.ruler = "grey32", pch0=1,xlab=NULL,ylab=NULL, plot=TRUE,...)
rectw |
An n*m matrix of rectangle widths, or a vector of m column widths. |
recth |
An n*m matrix of rectangle heights, or a vector of n row heights. |
col |
Rectangle fill colours. |
gapx |
Gaps in the x-direction. If provided should be a vector of length m-1. |
gapy |
Gaps in the x-direction. If provided should be a vector of length n-1. |
spacex |
A single value- extra space between columns as a fraction of maximum row total of rectw . |
spacey |
A single value- extra space between rows as a fraction of maximum column total of recth . |
xjust |
Horizontal justification of rectangles- "center", "left", or "right". |
yjust |
Vertical justification of rectangles- "center", "bottom", or "top". |
xruler |
Specifies position of rulers drawn parallel to x-axis. Values are a subset of ("top","center","bottom") |
yruler |
Specifies position of rulers drawn parallel to y-axis. Values are a subset of ("left","center","right") |
color.ruler |
Color for the rulers. |
pch0 |
Symbol for zero cell size. May be NULL. |
xlab |
X label |
ylab |
Y label |
plot |
If TRUE, draw tge plot. Otherwise returns a matrix where each row is the coordinates of a the calculated rectangle. |
... |
Passed to plot. |
Catherine Hurley
See overview
See also barplot
, mosaicplot
## Not run: require(PairViz) tab <- apply(HairEyeColor, c(1, 2), sum) dev.new() par(mar=c(3,3,1,1)) par(cex=.6,mgp=c(2, -.5, 0)) table_plot(sqrt(tab),sqrt(tab)) # this table plot has cells with widths and heights proportional to the square root of cell counts. tabp <- prop.table(tab,2) table_plot(apply(tab,2,sum),tabp) # make cell widths proportional to #margin totals, heights to conditional prob cols <- 2:5 table_plot(apply(tab,2,sum),tabp, yjust="bottom",col=cols,yruler=c("left","right")) # add colours, rulers and bottom-justify # The result is similar to the mosaic, without the mosaic effect of equalizing gaps. #In the table version the rectangles line up across rows, #so comparing heights, ie. conditional probs is easier. o <- hpaths(1:4)[2,] table_plot(apply(tab,2,sum)[o],tabp[,o], yjust="bottom",col=cols,yruler=c("left","right")) # Permutes the columns so all pairs of columns can be compared. #In the second permutation can easily see that #p(black|blue eyes)> p(black|green eyes) dev.new() par(mar=c(3,3,1,1)) par(mgp=c(2, -.5, 0)) mosaicplot(t(tab)[,nrow(tab):1],col=rev(cols),main="") # mosaic- good for seeing deviations from independence. hard to compare conditional probs, # except for those in the bottom and top rows. ## End(Not run)
## Not run: require(PairViz) tab <- apply(HairEyeColor, c(1, 2), sum) dev.new() par(mar=c(3,3,1,1)) par(cex=.6,mgp=c(2, -.5, 0)) table_plot(sqrt(tab),sqrt(tab)) # this table plot has cells with widths and heights proportional to the square root of cell counts. tabp <- prop.table(tab,2) table_plot(apply(tab,2,sum),tabp) # make cell widths proportional to #margin totals, heights to conditional prob cols <- 2:5 table_plot(apply(tab,2,sum),tabp, yjust="bottom",col=cols,yruler=c("left","right")) # add colours, rulers and bottom-justify # The result is similar to the mosaic, without the mosaic effect of equalizing gaps. #In the table version the rectangles line up across rows, #so comparing heights, ie. conditional probs is easier. o <- hpaths(1:4)[2,] table_plot(apply(tab,2,sum)[o],tabp[,o], yjust="bottom",col=cols,yruler=c("left","right")) # Permutes the columns so all pairs of columns can be compared. #In the second permutation can easily see that #p(black|blue eyes)> p(black|green eyes) dev.new() par(mar=c(3,3,1,1)) par(mgp=c(2, -.5, 0)) mosaicplot(t(tab)[,nrow(tab):1],col=rev(cols),main="") # mosaic- good for seeing deviations from independence. hard to compare conditional probs, # except for those in the bottom and top rows. ## End(Not run)
Returns a modified version of paths
, where
component paths/cycles are re-oriented so low weight edges occur first, and the component paths/cycles are then permuted so low-weight paths are first.
weighted_hpaths(d, path1 = NULL, paths=NULL, matrix=TRUE, cycle=NULL, path_weight=sum, path_dir = path_cor,...)
weighted_hpaths(d, path1 = NULL, paths=NULL, matrix=TRUE, cycle=NULL, path_weight=sum, path_dir = path_cor,...)
d |
A |
path1 |
A vector giving a hamiltonian.
This will be the first path of the returned hamiltonian. The default is obtained from |
paths |
A matrix where each row is a hamiltonian. Default comes from |
matrix |
if |
cycle |
If |
path_weight |
A function used combine path weights into a single value. Default function is |
path_dir |
A function used to evaluate a path start and orientation. |
... |
passed to |
If path
is not provided, find the hamiltonian (path for even n, cycle for odd n) with the smallest total
weight. Applying path_dir
to edge weights, pick the starting and point orientation for path1 giving the largest path_dir
value. (For open paths, there are only two possible starts, for cycles there are n).
Apply this node labelling to the hamiltonians in the rows of paths.
Use criterion path_dir
again to find the best orientation for each of rows 2... of paths and
permute these rows in order of increasing path_weight
.
C.B. Hurley and R.W. Oldford
see overview
require(PairViz) weighted_hpaths(dist(rnorm(6))) weighted_hpaths(dist(rnorm(7)))
require(PairViz) weighted_hpaths(dist(rnorm(6))) weighted_hpaths(dist(rnorm(7)))