Skip to content

Instantly share code, notes, and snippets.

@rCarto
Created October 4, 2023 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rCarto/da5a4b0cd982a8c85cb847506fd2f415 to your computer and use it in GitHub Desktop.
Save rCarto/da5a4b0cd982a8c85cb847506fd2f415 to your computer and use it in GitHub Desktop.
library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
# a graph
set.seed(44)
g <- barabasi.game(6)
# with weighted vertex
V(g)$w <- c(10, 50, 100, 10, 50, 100)
# and weighted edges
E(g)$fij <- c(20, 20, 50, 40, 5)
# fix layout
l <- layout_nicely(g)


# get proportional & normalized cicrles
get_vertex_weight <- function(x, w_max = 15) {
  size <- sqrt((x * pi / max(x)) / pi)
  size * w_max
}
# get normalized lines
get_edge_width <- function(x, w_max = 10) {
  size <- (x / max(x))
  size * w_max
}

v_w <- get_vertex_weight(x = V(g)$w, w_max = 50)
e_w <- get_edge_width(x = E(g)$fij, w_max = 20)

plot(g,
     layout = l,
     vertex.size = v_w,
     edge.width = e_w,
     vertex.color = "lightgreen",
     edge.color = "lightblue",
     rescale = TRUE)

library(maplegend)
leg(type = "prop",
    val = c(10, 50, 100),
    inches = max(v_w) / 200 / xinch(1),
    lwd = 1,
    title = 'Des Noeuds',
    pos = "topleft" ,
    horiz = TRUE,
    col = "lightgreen")

leg(type = "prop_line",
    val = c(5, 20, 50),
    lwd =  max(e_w),
    title = "Des liens",
    pos = "left" ,
    col = "lightblue",
    box_cex = c(3, .5))

###

leg_comp(type = "prop",
         val = c(10, 50, 100),
         inches = max(v_w) / 200 / xinch(1),
         lwd = 1,
         title = 'Des Noeuds',
         horiz = FALSE,
         col = "lightgreen") |>
  leg_comp(type = "prop_line",
           val = c(5, 20, 50),
           lwd =  max(e_w),
           title = "Des liens",
           col = "lightblue",
           box_cex = c(3, .5)) |>
  leg_draw(pos = "topright", frame_border = NA)

Created on 2023-10-04 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment