1- # ' Visualize a Matrix with Group Boundaries
1+ # ' Plot Function for "grasps" ( Visualize a Matrix with Group Boundaries)
22# '
33# ' @description
4- # ' Visualize a matrix (e.g., a precision, covariance, or adjacency matrix) as a
5- # ' heatmap with optional group-based reordering and dashed boundary lines
4+ # ' Visualize a precision matrix as a heatmap with dashed boundary lines
65# ' separating group blocks.
76# '
8- # ' @param mat A p-by-p matrix.
9- # '
10- # ' @param membership An integer vector specifying the group membership.
11- # ' The length of \code{membership} must be consistent with the dimension p.
12- # '
13- # ' @param reorder_by_group A boolean (default = \code{FALSE}) specifying whether
14- # ' to reorder both rows and columns of \code{mat} according to \code{membership},
15- # ' such that variables from the same group are contiguous.
7+ # ' @param x An object with S3 class "grasps".
168# '
179# ' @param colors A vector of colors specifying an n-color gradient scale for
1810# ' the fill aesthetics.
3527# ' }
3628# '
3729# ' ## block-structured precision matrix based on SBM
38- # ' sim <- gen_prec_sbm(p = 100, K = 5,
30+ # ' sim <- gen_prec_sbm(d = 100, K = 5,
3931# ' within.prob = 0.5, between.prob = 0.05,
4032# ' weight.dists = list(my_gamma, "unif"),
4133# ' weight.paras = list(NULL, c(min = 0, max = 1)),
42- # ' min.eig = 0.1 )
34+ # ' cond.target = 100 )
4335# '
4436# ' ## visualization
45- # ' visualize (sim$Omega, sim$membership )
37+ # ' plot (sim)
4638# '
4739# ' @export
40+ # ' @noRd
4841
49- visualize <- function (mat , membership , reorder_by_group = FALSE ,
50- colors = colorRampPalette(
51- c(" #00007F" , " blue" , " #007FFF" , " cyan" , " #7FFF7F" ,
52- " yellow" , " #FF7F00" , " red" , " #7F0000" ))(256 )) {
42+ plot.grasps <- function (x , colors = NULL , ... ) {
5343
54- p <- ncol( mat )
55- if (length( membership ) != p ) {
56- stop(sprintf( " Length of 'membership' (%d) must equal the matrix dimension p (%d). " ,
57- length( membership ), p ))
44+ if (is.null( x $ hatOmega )) {
45+ mat <- x $ Omega
46+ } else {
47+ mat <- x $ hatOmega
5848 }
5949
60- # # optionally reorder rows/cols by group membership
61- if (reorder_by_group ) {
62- o <- order( membership )
63- mat <- mat [ o , o ]
64- membership <- membership [ o ]
50+ d <- ncol( mat )
51+ if (is.null( colors ) ) {
52+ colors <- colorRampPalette(
53+ c( " #00007F " , " blue " , " #007FFF " , " cyan " , " #7FFF7F " ,
54+ " yellow " , " #FF7F00 " , " red " , " #7F0000 " ))( 256 )
6555 }
6656
6757 # # compute group sizes and boundary positions for dashed lines
68- grp_sizes <- table(membership )
58+ grp_sizes <- table(x $ membership )
6959 cuts <- cumsum(grp_sizes )
7060 bnds <- cuts [- length(cuts )] + 0.5 # # boundaries between groups
71- y_bnds <- p - bnds + 1 # # flipped y, note: scale_y_discrete(limits = rev)
61+ y_bnds <- d - bnds + 1 # # flipped y, note: scale_y_discrete(limits = rev)
7262
7363 # # declare
7464 Col <- Row <- value <- NULL
7565
7666 # # plot data
77- labs <- paste0(" V" , seq_len(p ))
67+ labs <- paste0(" V" , seq_len(d ))
7868 plotData <- data.frame (
79- Row = factor (rep(labs , times = p ), levels = labs ),
80- Col = factor (rep(labs , each = p ), levels = labs ),
69+ Row = factor (rep(labs , times = d ), levels = labs ),
70+ Col = factor (rep(labs , each = d ), levels = labs ),
8171 value = as.vector(mat ),
8272 check.names = FALSE
8373 )
@@ -87,9 +77,6 @@ visualize <- function(mat, membership, reorder_by_group = FALSE,
8777 # pivot_longer(-Row, names_to = "Col", values_to = "value") %>%
8878 # mutate(Col = factor(Col, levels = levels(Row)))
8979
90- # # sparsity (proportion of zero entries)
91- sparsity <- round(sum(mat == 0 ) / length(mat ), 4 )
92-
9380 # # zero -> NA for better white rendering
9481 plotData $ value [plotData $ value == 0 ] <- NA
9582
@@ -101,15 +88,17 @@ visualize <- function(mat, membership, reorder_by_group = FALSE,
10188 coord_fixed() +
10289 geom_tile() +
10390 guides(fill = guide_colourbar(title = NULL , barwidth = 0.5 , barheight = 5 )) +
104- scale_y_discrete(limits = rev ) +
91+ scale_x_discrete(limits = labs , expand = c(0 , 0 )) +
92+ scale_y_discrete(limits = rev(labs ), expand = c(0 , 0 )) +
10593 scale_fill_gradientn(colours = colors ,
10694 values = rescale(c(vmin , 0 , vmax )),
10795 limits = c(vmin , vmax ),
10896 na.value = " white" ) +
10997 geom_vline(xintercept = bnds , linetype = " dashed" ) +
11098 geom_hline(yintercept = y_bnds , linetype = " dashed" ) +
11199 labs(x = NULL , y = NULL ,
112- title = sprintf(" p = %d, Sparsity = %s" , ncol(mat ), sparsity )) +
100+ title = sprintf(" Dimension = %d, Sparsity = %s" , d ,
101+ round(sum(mat == 0 ) / length(mat ), 4 ))) +
113102 theme_bw() +
114103 theme(axis.text = element_blank(),
115104 axis.ticks = element_blank(),
0 commit comments