1

I am plotting a biological trasnport network in igraph (in R) where I want to show edges in a color gradient based on an edge attribute (which is a continuous variable called "width" in my case). I do not like the default color palette obtained in:

plot(graph, edge.color=E(graph)$width)

Looking at the igraph plot help I found a way to change this palette using cscale from the scales package:

plot(graph,
edge.color=cscale(E(graph)$width,palette = seq_gradient_pal(low = "yellow",high = "red")))

That works fine and may stick to it. However I was wondering whether I could use either viridis or colorbrewer palettes. I have not figured out how to do it. The main issue is that I cannot manage to assign color coding to all edges. For example if I just do this:

plot(graph,
 edge.color=cscale(E(graph)$width,palette = viridis_pal())

I get this warning:

In seq.default(begin, end, length.out = n) :
  first element used of 'length.out' argument

And indeed the first color is applied to all edges

If I try to specify the length of all edges:

plot(graph,
 edge.color=cscale(E(graph)$width,palette = viridis_pal()(ecount(graph))

I get this error message:

Error in palette(x) : invalid argument type)

Any ideas?

carlos_ArT
  • 11
  • 1
  • please provide a reproducible example https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – desval Oct 27 '20 at 09:26

1 Answers1

1

I had a similar problem --- I wanted to color edges using the viridis palette --- that I solved with SymbolixAU's colourvalues.

library(igraph)
library(viridis)
library(colourvalues)


# read in data frame with pairwise distances, 
# convert to lower triangular matrix, and 
# stash the column names for later use.  Then,

g <- graph_from_adjacency_matrix(m,mode="lower",weighted=TRUE,add.colnames=TRUE)
V(g)$name <- colnames(m)
E(g)$color <- colour_values(E(g)$weight,palette = "viridis")
plot(g, vertex.label=V(g)$name,edge.width = E(g)$weight/10, edge.label=E(g)$weight,edge.color=E(g)$color)

Warmest thanks, SymbolixAU!

foobar
  • 111
  • 2