1## ----------------------------------------------------------------------- 2## 3## IGraph R package 4## Copyright (C) 2014 Gabor Csardi <csardi.gabor@gmail.com> 5## 334 Harvard street, Cambridge, MA 02139 USA 6## 7## This program is free software; you can redistribute it and/or modify 8## it under the terms of the GNU General Public License as published by 9## the Free Software Foundation; either version 2 of the License, or 10## (at your option) any later version. 11## 12## This program is distributed in the hope that it will be useful, 13## but WITHOUT ANY WARRANTY; without even the implied warranty of 14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15## GNU General Public License for more details. 16## 17## You should have received a copy of the GNU General Public License 18## along with this program; if not, write to the Free Software 19## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 20## 02110-1301 USA 21## 22## ----------------------------------------------------------------------- 23 24 25#' Palette for categories 26#' 27#' This is a color blind friendly palette from 28#' \url{https://jfly.uni-koeln.de/color/}. It has 8 colors. 29#' 30#' This is the suggested palette for visualizations where vertex colors 31#' mark categories, e.g. community membership. 32#' 33#' @param n The number of colors in the palette. We simply take the first 34#' \code{n} colors from the total 8. 35#' @return A character vector of RGB color codes. 36#' 37#' @section Examples: 38#' \preformatted{ 39#' library(igraphdata) 40#' data(karate) 41#' karate <- karate %>% 42#' add_layout_(with_fr()) %>% 43#' set_vertex_attr("size", value = 10) 44#' 45#' cl_k <- cluster_optimal(karate) 46#' 47#' V(karate)$color <- membership(cl_k) 48#' karate$palette <- categorical_pal(length(cl_k)) 49#' plot(karate) 50#' } 51#' 52#' @family palettes 53#' @export 54 55categorical_pal <- function(n) { 56 57 stopifnot(n > 0) 58 59 x <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", 60 "#D55E00", "#CC79A7", "#999999") 61 62 if (n > length(x)) warning("Cannot make ", n, " categorical colors") 63 64 n <- min(n, length(x)) 65 66 x[seq_len(n)] 67} 68 69 70#' Sequential palette 71#' 72#' This is the \sQuote{OrRd} palette from \url{https://colorbrewer2.org/}. 73#' It has at most nine colors. 74#' 75#' Use this palette, if vertex colors mark some ordinal quantity, e.g. some 76#' centrality measure, or some ordinal vertex covariate, like the age of 77#' people, or their seniority level. 78#' 79#' @param n The number of colors in the palette. The maximum is nine 80#' currently. 81#' @return A character vector of RGB color codes. 82#' 83#' @family palettes 84#' @export 85#' @examples 86#' \dontrun{ 87#' library(igraphdata) 88#' data(karate) 89#' karate <- karate %>% 90#' add_layout_(with_kk()) %>% 91#' set_vertex_attr("size", value = 10) 92#' 93#' V(karate)$color <- scales::dscale(degree(karate) %>% cut(5), sequential_pal) 94#' plot(karate) 95#' } 96 97sequential_pal <- function(n) { 98 99 stopifnot(n >= 0) 100 101 x <- list( 102 "#FEE8C8", 103 c("#FEE8C8", "#FDBB84"), 104 c("#FEE8C8", "#FDBB84", "#E34A33"), 105 c("#FEF0D9", "#FDCC8A", "#FC8D59", "#D7301F"), 106 c("#FEF0D9", "#FDCC8A", "#FC8D59", "#E34A33", "#B30000"), 107 c("#FEF0D9", "#FDD49E", "#FDBB84", "#FC8D59", "#E34A33", "#B30000"), 108 c("#FEF0D9", "#FDD49E", "#FDBB84", "#FC8D59", "#EF6548", "#D7301F", 109 "#990000"), 110 c("#FFF7EC", "#FEE8C8", "#FDD49E", "#FDBB84", "#FC8D59", "#EF6548", 111 "#D7301F", "#990000"), 112 c("#FFF7EC", "#FEE8C8", "#FDD49E", "#FDBB84", "#FC8D59", "#EF6548", 113 "#D7301F", "#B30000", "#7F0000") 114 ) 115 116 if (n > length(x)) warning("Cannot make ", n, " sequential colors") 117 118 n <- min(n, length(x)) 119 120 if (n == 0) character() else x[[n]] 121} 122 123 124#' Diverging palette 125#' 126#' This is the \sQuote{PuOr} palette from \url{https://colorbrewer2.org/}. 127#' It has at most eleven colors. 128#' 129#' This is similar to \code{\link{sequential_pal}}, but it also puts 130#' emphasis on the mid-range values, plus the the two extreme ends. 131#' Use this palette, if you have such a quantity to mark with vertex 132#' colors. 133#' 134#' @param n The number of colors in the palette. The maximum is eleven 135#' currently. 136#' @return A character vector of RGB color codes. 137#' 138#' @family palettes 139#' @export 140#' @examples 141#' \dontrun{ 142#' library(igraphdata) 143#' data(foodwebs) 144#' fw <- foodwebs[[1]] %>% 145#' induced_subgraph(V(.)[ECO == 1]) %>% 146#' add_layout_(with_fr()) %>% 147#' set_vertex_attr("label", value = seq_len(gorder(.))) %>% 148#' set_vertex_attr("size", value = 10) %>% 149#' set_edge_attr("arrow.size", value = 0.3) 150#' 151#' V(fw)$color <- scales::dscale(V(fw)$Biomass %>% cut(10), diverging_pal) 152#' plot(fw) 153#' 154#' data(karate) 155#' karate <- karate %>% 156#' add_layout_(with_kk()) %>% 157#' set_vertex_attr("size", value = 10) 158#' 159#' V(karate)$color <- scales::dscale(degree(karate) %>% cut(5), diverging_pal) 160#' plot(karate) 161#' } 162 163diverging_pal <- function(n) { 164 165 stopifnot(n > 0) 166 167 x <- list( 168 "#F1A340", 169 c("#F1A340", "#F7F7F7"), 170 c("#F1A340", "#F7F7F7", "#998EC3"), 171 c("#E66101", "#FDB863", "#B2ABD2", "#5E3C99"), 172 c("#E66101", "#FDB863", "#F7F7F7", "#B2ABD2", "#5E3C99"), 173 c("#B35806", "#F1A340", "#FEE0B6", "#D8DAEB", "#998EC3", "#542788"), 174 c("#B35806", "#F1A340", "#FEE0B6", "#F7F7F7", "#D8DAEB", "#998EC3", 175 "#542788"), 176 c("#B35806", "#E08214", "#FDB863", "#FEE0B6", "#D8DAEB", "#B2ABD2", 177 "#8073AC", "#542788"), 178 c("#B35806", "#E08214", "#FDB863", "#FEE0B6", "#F7F7F7", "#D8DAEB", 179 "#B2ABD2", "#8073AC", "#542788"), 180 c("#7F3B08", "#B35806", "#E08214", "#FDB863", "#FEE0B6", "#D8DAEB", 181 "#B2ABD2", "#8073AC", "#542788", "#2D004B"), 182 c("#7F3B08", "#B35806", "#E08214", "#FDB863", "#FEE0B6", "#F7F7F7", 183 "#D8DAEB", "#B2ABD2", "#8073AC", "#542788", "#2D004B") 184 ) 185 186 if (n > length(x)) warning("Cannot make ", n, " divergent colors") 187 188 n <- min(n, length(x)) 189 190 if (n == 0) character() else x[[n]] 191} 192 193 194#' The default R palette 195#' 196#' This is the default R palette, to be able to reproduce the 197#' colors of older igraph versions. Its colors are appropriate 198#' for categories, but they are not very attractive. 199#' 200#' @param n The number of colors to use, the maximum is eight. 201#' @return A character vector of color names. 202#' 203#' @family palettes 204#' @export 205#' @importFrom grDevices palette 206 207r_pal <- function(n) { 208 x <- palette() 209 if (n > length(x)) warning("Cannot make ", n, " divergent colors") 210 n <- min(n, length(x)) 211 if (n == 0) character() else x[[n]] 212} 213