1 ```#' @title Plot rank intervals ``` 2 ```#' @description Compute rank intervals (minimal and maximal possible rank) and visualize them with ggplot. ``` 3 ```#' @param P A partial ranking as matrix object calculated with [neighborhood_inclusion] ``` 4 ```#' or [positional_dominance]. ``` 5 ```#' @param cent.df A data frame containing centrality scores of indices (optional). See Details. ``` 6 ```#' @param ties.method String specifying how ties are treated in the base \code{\link[base]{rank}} function. ``` 7 ```#' @details ``` 8 ```#' If a data frame of centrality scores is added, the respective ranks of nodes are ``` 9 ```#' shown in the intervals. Note that some points might fall outside of the ``` 10 ```#' intervals depending how ties are treated. ``` 11 ```#' ``` 12 ```#' @return a ggplot object. ``` 13 ```#' @author David Schoch ``` 14 ```#' @seealso [rank_intervals] ``` 15 ```#' @examples ``` 16 ```#' library(igraph) ``` 17 ```#' library(ggplot2) ``` 18 ```#' g <- graph.empty(n=11,directed = FALSE) ``` 19 ```#' g <- add_edges(g,c(1,11,2,4,3,5,3,11,4,8,5,9,5,11,6,7,6,8, ``` 20 ```#' 6,10,6,11,7,9,7,10,7,11,8,9,8,10,9,10)) ``` 21 ```#' P <- neighborhood_inclusion(g) ``` 22 ```#' \dontrun{plot_rank_intervals(P)} ``` 23 ```#' ``` 24 ```#' #adding index based rankings ``` 25 ```#' cent_scores <- data.frame( ``` 26 ```#' degree = degree(g), ``` 27 ```#' betweenness = round(betweenness(g),4), ``` 28 ```#' closeness = round(closeness(g),4), ``` 29 ```#' eigenvector = round(eigen_centrality(g)\$vector,4)) ``` 30 ```#' \dontrun{plot_rank_intervals(P,cent.df=cent_scores)} ``` 31 ```#' @export ``` 32 33 ```plot_rank_intervals <- function(P, cent.df,ties.method="min") { ``` 34 0 ``` if (!requireNamespace("ggplot2", quietly = TRUE)) { ``` 35 0 ``` stop("ggplot2 needed for this function to work. Please install it.", call. = FALSE) ``` 36 ``` } ``` 37 ``` ``` 38 0 ``` n <- nrow(P) ``` 39 0 ``` if (is.null(rownames(P))) { ``` 40 0 ``` names <- paste0("V",1:nrow(P)) ``` 41 ``` } else { ``` 42 0 ``` names <- rownames(P) ``` 43 ``` } ``` 44 0 ``` intervals <- netrankr::rank_intervals(P) ``` 45 0 ``` intervals\$node <- as.character(names) ``` 46 0 ``` df <- data.frame(interval = rep(c("max_rank", "min_rank", "mid_point"), each = n), ``` 47 0 ``` node = rep(names, 3), ``` 48 0 ``` rank = c(intervals\$max_rank, intervals\$min_rank, intervals\$mid_point), ``` 49 0 ``` order = rep(rank(intervals\$mid_point, ties.method = "first"), 3), ``` 50 0 ``` mid_point = rep(intervals\$mid_point, 3)) ``` 51 0 ``` if (missing(cent.df)) { ``` 52 0 ``` ggplot2::ggplot(df, ggplot2::aes_(x = ~stats::reorder(node, order), y = ~rank, group = ~node)) + ``` 53 0 ``` ggplot2::geom_line(col = "#8F8F8F") + ``` 54 0 ``` ggplot2::geom_point(col = "#8F8F8F", ``` 55 0 ``` shape = ifelse(df\$interval == "min_rank", 24, ``` 56 0 ``` ifelse(df\$interval == "max_rank", 25, 3)), fill = "#8F8F8F") + ``` 57 0 ``` ggplot2::scale_y_continuous(breaks = pretty(1:nrow(P))) + ``` 58 0 ``` ggplot2::theme_bw() + ``` 59 0 ``` ggplot2::theme(text = ggplot2::element_text(family = "Times", size = 14), ``` 60 0 ``` axis.text.x = ggplot2::element_text(angle = ifelse(any(nchar(names) > 2), 45, 0), hjust = 1), ``` 61 0 ``` legend.position = "bottom", ``` 62 0 ``` panel.border = ggplot2::element_blank(), ``` 63 0 ``` panel.grid = ggplot2::element_blank(), ``` 64 0 ``` axis.ticks = ggplot2::element_blank()) + ``` 65 0 ``` ggplot2::labs(x = "", y = "Rank", shape = "", colour = "") ``` 66 ``` } else { ``` 67 0 ``` no.indices <- ncol(cent.df) ``` 68 0 ``` cent.df.long <- data.frame(node = rep(names, no.indices), ``` 69 0 ``` mid_point = rep(intervals\$mid_point, no.indices), ``` 70 0 ``` index = rep(names(cent.df), each = n), ``` 71 0 ``` rank = c(apply(cent.df, 2, function(x) rank(x, ties.method = ties.method)))) ``` 72 0 ``` ggplot2::ggplot(df, ggplot2::aes_(x = ~stats::reorder(node, mid_point), ``` 73 0 ``` y = ~rank, group = ~node)) + ``` 74 0 ``` ggplot2::geom_line(col = "#8F8F8F") + ``` 75 0 ``` ggplot2::geom_point(col = "#8F8F8F", ``` 76 0 ``` shape = ifelse(df\$interval == "min_rank", 24, ``` 77 0 ``` ifelse(df\$interval == "max_rank", 25, 3)), fill = "#8F8F8F") + ``` 78 0 ``` ggplot2::geom_jitter(data = cent.df.long, ``` 79 0 ``` ggplot2::aes_(x = ~stats::reorder(node, mid_point), ``` 80 0 ``` y = ~rank, shape = ~index), size = 2, ``` 81 0 ``` width = 0.1, height = 0) + ``` 82 0 ``` ggplot2::scale_shape_manual(values = 3:(3 + no.indices)) + ``` 83 0 ``` ggplot2::scale_y_continuous(breaks = pretty(1:nrow(P))) + ``` 84 0 ``` ggplot2::theme_bw() + ``` 85 0 ``` ggplot2::theme(text = ggplot2::element_text(family = "Times", size = 14), ``` 86 0 ``` axis.text.x = ggplot2::element_text(angle = ifelse(any(nchar(names) > 2), 45, 0), hjust = 1), ``` 87 0 ``` legend.position = "bottom", ``` 88 0 ``` panel.border = ggplot2::element_blank(), ``` 89 0 ``` panel.grid = ggplot2::element_blank(), ``` 90 0 ``` axis.ticks = ggplot2::element_blank()) + ``` 91 0 ``` ggplot2::labs(x = "", y = "Rank", shape = "", colour = "") ``` 92 ``` } ``` 93 ```} ```

Read our documentation on viewing source code .